| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 |  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2x
 
2x
2x
 
2x
2x
2x
2x
2x
2x
2x
2x
 
 
2x
 
 
 
 
 
 
 
 
 
 
 
 
2x
 
 
47x
47x
24x
 
23x
 
 
 
 
 
 
 
 
2x
30x
29x
 
 
2x
15x
 
 
2x
19x
 
 
 
 
 
 
 
2x
14x
11x
11x
11x
 
 
 
 
 
 
2x
3x
3x
1x
 
2x
2x
 
 
 
 
 
 
2x
2x
2x
 
 
2x
1x
 
 
2x
1x
 
 
2x
2x
 
 
2x
 
 
 
 
 
 
 
 
 
2x
 
20x
 
12x
 
 
 
 
6x
5x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2x
 
13x
 
 
12x
 
 
 
 
 
 
 
 
8x
7x
7x
7x
2x
 
7x
 
 
 
 
 
 
 
 
 
 
 
 
 
2x
3x
2x
1x
1x
1x
1x
 
 
 
 
 
 
 
 
2x
8x
7x
6x
6x
6x
 
 
 
 
6x
 
 
 
 
 
 
 
 
 
 
 
 
2x
5x
2x
1x
1x
1x
 
 
 
 
 
1x
 
 
 
 
 
 
 
2x
3x
2x
1x
 
 
 
 
 
 
 
 
 
2x
27x
6x
 
 
2x
  | /**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @fileoverview Defines the Firebase Storage Reference class.
 */
import * as args from './implementation/args';
import { AuthWrapper } from './implementation/authwrapper';
import { FbsBlob } from './implementation/blob';
import * as errorsExports from './implementation/error';
import { errors } from './implementation/error';
import { Location } from './implementation/location';
import * as metadata from './implementation/metadata';
import * as object from './implementation/object';
import * as path from './implementation/path';
import * as requests from './implementation/requests';
import * as fbsString from './implementation/string';
import { StringFormat } from './implementation/string';
import * as type from './implementation/type';
import { Metadata } from './metadata';
import { Service } from './service';
import { UploadTask } from './task';
 
/**
 * Provides methods to interact with a bucket in the Firebase Storage service.
 * @param location An fbs.location, or the URL at
 *     which to base this object, in one of the following forms:
 *         gs://<bucket>/<object-path>
 *         http[s]://firebasestorage.googleapis.com/
 *                     <api-version>/b/<bucket>/o/<object-path>
 *     Any query or fragment strings will be ignored in the http[s]
 *     format. If no value is passed, the storage object will use a URL based on
 *     the project ID of the base firebase.App instance.
 */
export class Reference {
  protected location: Location;
 
  constructor(protected authWrapper: AuthWrapper, location: string | Location) {
    if (location instanceof Location) {
      this.location = location;
    } else {
      this.location = Location.makeFromUrl(location);
    }
  }
 
  /**
   * @return The URL for the bucket and path this object references,
   *     in the form gs://<bucket>/<object-path>
   * @override
   */
  toString(): string {
    args.validate('toString', [], arguments);
    return 'gs://' + this.location.bucket + '/' + this.location.path;
  }
 
  protected newRef(authWrapper: AuthWrapper, location: Location): Reference {
    return new Reference(authWrapper, location);
  }
 
  protected mappings(): metadata.Mappings {
    return metadata.getMappings();
  }
 
  /**
   * @return A reference to the object obtained by
   *     appending childPath, removing any duplicate, beginning, or trailing
   *     slashes.
   */
  child(childPath: string): Reference {
    args.validate('child', [args.stringSpec()], arguments);
    let newPath = path.child(this.location.path, childPath);
    let location = new Location(this.location.bucket, newPath);
    return this.newRef(this.authWrapper, location);
  }
 
  /**
   * @return A reference to the parent of the
   *     current object, or null if the current object is the root.
   */
  get parent(): Reference | null {
    let newPath = path.parent(this.location.path);
    if (newPath === null) {
      return null;
    }
    let location = new Location(this.location.bucket, newPath);
    return this.newRef(this.authWrapper, location);
  }
 
  /**
   * @return An reference to the root of this
   *     object's bucket.
   */
  get root(): Reference {
    let location = new Location(this.location.bucket, '');
    return this.newRef(this.authWrapper, location);
  }
 
  get bucket(): string {
    return this.location.bucket;
  }
 
  get fullPath(): string {
    return this.location.path;
  }
 
  get name(): string {
    return path.lastComponent(this.location.path);
  }
 
  get storage(): Service {
    return this.authWrapper.service();
  }
 
  /**
   * Uploads a blob to this object's location.
   * @param data The blob to upload.
   * @return An UploadTask that lets you control and
   *     observe the upload.
   */
  put(
    data: Blob | Uint8Array | ArrayBuffer,
    metadata: Metadata | null = null
  ): UploadTask {
    args.validate(
      'put',
      [args.uploadDataSpec(), args.metadataSpec(true)],
      arguments
    );
    this.throwIfRoot_('put');
    return new UploadTask(
      this,
      this.authWrapper,
      this.location,
      this.mappings(),
      new FbsBlob(data),
      metadata
    );
  }
 
  /**
   * Uploads a string to this object's location.
   * @param string The string to upload.
   * @param opt_format The format of the string to upload.
   * @return An UploadTask that lets you control and
   *     observe the upload.
   */
  putString(
    string: string,
    format: StringFormat = StringFormat.RAW,
    opt_metadata?: Metadata
  ): UploadTask {
    args.validate(
      'putString',
      [
        args.stringSpec(),
        args.stringSpec(fbsString.formatValidator, true),
        args.metadataSpec(true)
      ],
      arguments
    );
    this.throwIfRoot_('putString');
    let data = fbsString.dataFromString(format, string);
    let metadata = object.clone<Metadata>(opt_metadata);
    if (!type.isDef(metadata['contentType']) && type.isDef(data.contentType)) {
      metadata['contentType'] = data.contentType;
    }
    return new UploadTask(
      this,
      this.authWrapper,
      this.location,
      this.mappings(),
      new FbsBlob(data.data, true),
      metadata
    );
  }
 
  /**
   * Deletes the object at this location.
   * @return A promise that resolves if the deletion succeeds.
   */
  delete(): Promise<void> {
    args.validate('delete', [], arguments);
    this.throwIfRoot_('delete');
    let self = this;
    return this.authWrapper.getAuthToken().then(function(authToken) {
      let requestInfo = requests.deleteObject(self.authWrapper, self.location);
      return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
    });
  }
 
  /**
   *     A promise that resolves with the metadata for this object. If this
   *     object doesn't exist or metadata cannot be retreived, the promise is
   *     rejected.
   */
  getMetadata(): Promise<Metadata> {
    args.validate('getMetadata', [], arguments);
    this.throwIfRoot_('getMetadata');
    let self = this;
    return this.authWrapper.getAuthToken().then(function(authToken) {
      let requestInfo = requests.getMetadata(
        self.authWrapper,
        self.location,
        self.mappings()
      );
      return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
    });
  }
 
  /**
   * Updates the metadata for this object.
   * @param metadata The new metadata for the object.
   *     Only values that have been explicitly set will be changed. Explicitly
   *     setting a value to null will remove the metadata.
   * @return A promise that resolves
   *     with the new metadata for this object.
   *     @see firebaseStorage.Reference.prototype.getMetadata
   */
  updateMetadata(metadata: Metadata): Promise<Metadata> {
    args.validate('updateMetadata', [args.metadataSpec()], arguments);
    this.throwIfRoot_('updateMetadata');
    let self = this;
    return this.authWrapper.getAuthToken().then(function(authToken) {
      let requestInfo = requests.updateMetadata(
        self.authWrapper,
        self.location,
        metadata,
        self.mappings()
      );
      return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
    });
  }
 
  /**
   * @return A promise that resolves with the download
   *     URL for this object.
   */
  getDownloadURL(): Promise<string> {
    args.validate('getDownloadURL', [], arguments);
    this.throwIfRoot_('getDownloadURL');
    return this.getMetadata().then(function(metadata) {
      let url = (metadata['downloadURLs'] as string[])[0];
      if (type.isDef(url)) {
        return url;
      } else {
        throw errorsExports.noDownloadURL();
      }
    });
  }
 
  private throwIfRoot_(name: string) {
    if (this.location.path === '') {
      throw errorsExports.invalidRootOperation(name);
    }
  }
}
  |