|
| 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 | +} |
0 commit comments