Skip to content

Commit 742477f

Browse files
committed
Split StorageService and Reference compat versions
1 parent 1971dde commit 742477f

File tree

5 files changed

+666
-344
lines changed

5 files changed

+666
-344
lines changed

packages/storage/compat/reference.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {
19+
Reference,
20+
getChild,
21+
getParent,
22+
uploadBytes,
23+
uploadString,
24+
list,
25+
listAll,
26+
getDownloadURL,
27+
getMetadata,
28+
updateMetadata,
29+
deleteObject
30+
} from '../src/reference';
31+
import { validate, stringSpec } from '../src/implementation/args';
32+
import { Metadata } from '../src/metadata';
33+
import { UploadTask } from '../src/task';
34+
import { StringFormat } from '../src/implementation/string';
35+
import { ListResult, ListOptions } from '../src/list';
36+
37+
export class ReferenceCompat extends Reference {
38+
static fromReference(ref: Reference): ReferenceCompat {
39+
return new ReferenceCompat(ref.service, ref.location);
40+
}
41+
/**
42+
* @return A reference to the object obtained by
43+
* appending childPath, removing any duplicate, beginning, or trailing
44+
* slashes.
45+
*/
46+
child(childPath: string): ReferenceCompat {
47+
validate('child', [stringSpec()], arguments);
48+
const reference = getChild(this, childPath);
49+
return ReferenceCompat.fromReference(reference);
50+
}
51+
52+
/**
53+
* @return A reference to the parent of the
54+
* current object, or null if the current object is the root.
55+
*/
56+
get parent(): ReferenceCompat | null {
57+
const reference = getParent(this);
58+
if (reference == null) {
59+
return null;
60+
}
61+
return ReferenceCompat.fromReference(reference);
62+
}
63+
64+
/**
65+
* Uploads a blob to this object's location.
66+
* @param data The blob to upload.
67+
* @return An UploadTask that lets you control and
68+
* observe the upload.
69+
*/
70+
put(
71+
data: Blob | Uint8Array | ArrayBuffer,
72+
metadata: Metadata | null = null
73+
): UploadTask {
74+
return uploadBytes(this, data, metadata);
75+
}
76+
/**
77+
* Uploads a string to this object's location.
78+
* @param value The string to upload.
79+
* @param format The format of the string to upload.
80+
* @return An UploadTask that lets you control and
81+
* observe the upload.
82+
*/
83+
putString(
84+
value: string,
85+
format: StringFormat = StringFormat.RAW,
86+
metadata?: Metadata
87+
): UploadTask {
88+
return uploadString(this, value, format, metadata);
89+
}
90+
/**
91+
* List all items (files) and prefixes (folders) under this storage reference.
92+
*
93+
* This is a helper method for calling list() repeatedly until there are
94+
* no more results. The default pagination size is 1000.
95+
*
96+
* Note: The results may not be consistent if objects are changed while this
97+
* operation is running.
98+
*
99+
* Warning: listAll may potentially consume too many resources if there are
100+
* too many results.
101+
*
102+
* @return A Promise that resolves with all the items and prefixes under
103+
* the current storage reference. `prefixes` contains references to
104+
* sub-directories and `items` contains references to objects in this
105+
* folder. `nextPageToken` is never returned.
106+
*/
107+
listAll(): Promise<ListResult> {
108+
return listAll(this);
109+
}
110+
/**
111+
* List items (files) and prefixes (folders) under this storage reference.
112+
*
113+
* List API is only available for Firebase Rules Version 2.
114+
*
115+
* GCS is a key-blob store. Firebase Storage imposes the semantic of '/'
116+
* delimited folder structure.
117+
* Refer to GCS's List API if you want to learn more.
118+
*
119+
* To adhere to Firebase Rules's Semantics, Firebase Storage does not
120+
* support objects whose paths end with "/" or contain two consecutive
121+
* "/"s. Firebase Storage List API will filter these unsupported objects.
122+
* list() may fail if there are too many unsupported objects in the bucket.
123+
*
124+
* @param options See ListOptions for details.
125+
* @return A Promise that resolves with the items and prefixes.
126+
* `prefixes` contains references to sub-folders and `items`
127+
* contains references to objects in this folder. `nextPageToken`
128+
* can be used to get the rest of the results.
129+
*/
130+
list(options?: ListOptions | null): Promise<ListResult> {
131+
return list(this, options);
132+
}
133+
134+
/**
135+
* A promise that resolves with the metadata for this object. If this
136+
* object doesn't exist or metadata cannot be retreived, the promise is
137+
* rejected.
138+
*/
139+
getMetadata(): Promise<Metadata> {
140+
return getMetadata(this);
141+
}
142+
143+
/**
144+
* Updates the metadata for this object.
145+
* @param metadata The new metadata for the object.
146+
* Only values that have been explicitly set will be changed. Explicitly
147+
* setting a value to null will remove the metadata.
148+
* @return A promise that resolves
149+
* with the new metadata for this object.
150+
* @see firebaseStorage.Reference.prototype.getMetadata
151+
*/
152+
updateMetadata(metadata: Metadata): Promise<Metadata> {
153+
return updateMetadata(this, metadata);
154+
}
155+
156+
/**
157+
* @return A promise that resolves with the download
158+
* URL for this object.
159+
*/
160+
getDownloadURL(): Promise<string> {
161+
return getDownloadURL(this);
162+
}
163+
164+
/**
165+
* Deletes the object at this location.
166+
* @return A promise that resolves if the deletion succeeds.
167+
*/
168+
delete(): Promise<void> {
169+
return deleteObject(this);
170+
}
171+
}

packages/storage/compat/service.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { StorageService, pathValidator, urlValidator } from '../src/service';
19+
import { FirebaseApp } from '@firebase/app-types';
20+
import { Provider } from '@firebase/component';
21+
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
22+
import { XhrIoPool } from '../src/implementation/xhriopool';
23+
import { Reference } from '../src/reference';
24+
import * as args from '../src/implementation/args';
25+
import { ReferenceCompat } from './reference';
26+
27+
/**
28+
* A service that provides firebaseStorage.Reference instances.
29+
* @param opt_url gs:// url to a custom Storage Bucket
30+
*
31+
* @struct
32+
*/
33+
export class StorageServiceCompat extends StorageService {
34+
private readonly internals_: ServiceInternals;
35+
36+
constructor(
37+
app: FirebaseApp | null,
38+
authProvider: Provider<FirebaseAuthInternalName>,
39+
pool: XhrIoPool,
40+
url?: string
41+
) {
42+
super(app, authProvider, pool, url);
43+
this.internals_ = new ServiceInternals(this);
44+
}
45+
46+
/**
47+
* Returns a firebaseStorage.Reference for the given path in the default
48+
* bucket.
49+
*/
50+
ref(path?: string): ReferenceCompat {
51+
args.validate('ref', [args.stringSpec(pathValidator, true)], arguments);
52+
53+
const reference = new ReferenceCompat(this, this.bucket_!);
54+
if (path != null) {
55+
return reference.child(path);
56+
} else {
57+
return reference;
58+
}
59+
}
60+
61+
/**
62+
* Returns a firebaseStorage.Reference object for the given absolute URL,
63+
* which must be a gs:// or http[s]:// URL.
64+
*/
65+
refFromURL(url: string): Reference {
66+
args.validate(
67+
'refFromURL',
68+
[args.stringSpec(urlValidator, false)],
69+
arguments
70+
);
71+
return new Reference(this, url);
72+
}
73+
74+
get app(): FirebaseApp | null {
75+
return this.app_;
76+
}
77+
78+
get INTERNAL(): ServiceInternals {
79+
return this.internals_;
80+
}
81+
}
82+
83+
/**
84+
* @struct
85+
*/
86+
export class ServiceInternals {
87+
service_: StorageService;
88+
89+
constructor(service: StorageService) {
90+
this.service_ = service;
91+
}
92+
93+
/**
94+
* Called when the associated app is deleted.
95+
*/
96+
delete(): Promise<void> {
97+
this.service_.deleteApp();
98+
return Promise.resolve();
99+
}
100+
}

0 commit comments

Comments
 (0)