UNPKG

11.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('rxjs/operators'), require('@angular/core'), require('@angular/fire'), require('firebase/storage'), require('@angular/common')) :
3 typeof define === 'function' && define.amd ? define('@angular/fire/storage', ['exports', 'rxjs', 'rxjs/operators', '@angular/core', '@angular/fire', 'firebase/storage', '@angular/common'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.angular = global.angular || {}, global.angular.fire = global.angular.fire || {}, global.angular.fire.storage = {}), global.rxjs, global.rxjs.operators, global.ng.core, global.angular.fire, null, global.ng.common));
5}(this, (function (exports, rxjs, operators, i0, i1, storage, common) { 'use strict';
6
7 function _interopNamespace(e) {
8 if (e && e.__esModule) return e;
9 var n = Object.create(null);
10 if (e) {
11 Object.keys(e).forEach(function (k) {
12 if (k !== 'default') {
13 var d = Object.getOwnPropertyDescriptor(e, k);
14 Object.defineProperty(n, k, d.get ? d : {
15 enumerable: true,
16 get: function () {
17 return e[k];
18 }
19 });
20 }
21 });
22 }
23 n['default'] = e;
24 return Object.freeze(n);
25 }
26
27 var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
28 var i1__namespace = /*#__PURE__*/_interopNamespace(i1);
29
30 // Things aren't working great, I'm having to put in a lot of work-arounds for what
31 // appear to be Firebase JS SDK bugs https://github.com/firebase/firebase-js-sdk/issues/4158
32 function fromTask(task) {
33 return new rxjs.Observable(function (subscriber) {
34 var progress = function (snap) { return subscriber.next(snap); };
35 var error = function (e) { return subscriber.error(e); };
36 var complete = function () { return subscriber.complete(); };
37 // emit the current snapshot, so they don't have to wait for state_changes
38 // to fire next... this is stale if the task is no longer running :(
39 progress(task.snapshot);
40 var unsub = task.on('state_changed', progress);
41 // it turns out that neither task snapshot nor 'state_changed' fire the last
42 // snapshot before completion, the one with status 'success" and 100% progress
43 // so let's use the promise form of the task for that
44 task.then(function (snapshot) {
45 progress(snapshot);
46 complete();
47 }, function (e) {
48 // TODO investigate, again this is stale, we never fire a canceled or error it seems
49 progress(task.snapshot);
50 error(e);
51 });
52 // on's type if Function, rather than () => void, need to wrap
53 return function unsubscribe() {
54 unsub();
55 };
56 }).pipe(
57 // deal with sync emissions from first emitting `task.snapshot`, this makes sure
58 // that if the task is already finished we don't emit the old running state
59 operators.debounceTime(0));
60 }
61
62 /**
63 * Create an AngularFireUploadTask from a regular UploadTask from the Storage SDK.
64 * This method creates an observable of the upload and returns on object that provides
65 * multiple methods for controlling and monitoring the file upload.
66 */
67 function createUploadTask(task) {
68 var inner$ = fromTask(task);
69 return {
70 task: task,
71 then: task.then.bind(task),
72 catch: task.catch.bind(task),
73 pause: task.pause.bind(task),
74 cancel: task.cancel.bind(task),
75 resume: task.resume.bind(task),
76 snapshotChanges: function () { return inner$; },
77 percentageChanges: function () { return inner$.pipe(operators.map(function (s) { return s.bytesTransferred / s.totalBytes * 100; })); }
78 };
79 }
80
81 /**
82 * Create an AngularFire wrapped Storage Reference. This object
83 * creates observable methods from promise based methods.
84 */
85 function createStorageRef(ref, schedulers, keepUnstableUntilFirst) {
86 return {
87 getDownloadURL: function () { return rxjs.of(undefined).pipe(operators.observeOn(schedulers.outsideAngular), operators.switchMap(function () { return ref.getDownloadURL(); }), keepUnstableUntilFirst); },
88 getMetadata: function () { return rxjs.of(undefined).pipe(operators.observeOn(schedulers.outsideAngular), operators.switchMap(function () { return ref.getMetadata(); }), keepUnstableUntilFirst); },
89 delete: function () { return rxjs.from(ref.delete()); },
90 child: function (path) { return createStorageRef(ref.child(path), schedulers, keepUnstableUntilFirst); },
91 updateMetadata: function (meta) { return rxjs.from(ref.updateMetadata(meta)); },
92 put: function (data, metadata) {
93 var task = ref.put(data, metadata);
94 return createUploadTask(task);
95 },
96 putString: function (data, format, metadata) {
97 var task = ref.putString(data, format, metadata);
98 return createUploadTask(task);
99 },
100 listAll: function () { return rxjs.from(ref.listAll()); }
101 };
102 }
103
104 var BUCKET = new i0.InjectionToken('angularfire2.storageBucket');
105 var MAX_UPLOAD_RETRY_TIME = new i0.InjectionToken('angularfire2.storage.maxUploadRetryTime');
106 var MAX_OPERATION_RETRY_TIME = new i0.InjectionToken('angularfire2.storage.maxOperationRetryTime');
107 /**
108 * AngularFireStorage Service
109 *
110 * This service is the main entry point for this feature module. It provides
111 * an API for uploading and downloading binary files from Cloud Storage for
112 * Firebase.
113 */
114 var AngularFireStorage = /** @class */ (function () {
115 function AngularFireStorage(options, nameOrConfig, storageBucket,
116 // tslint:disable-next-line:ban-types
117 platformId, zone, maxUploadRetryTime, maxOperationRetryTime) {
118 this.schedulers = new i1.ɵAngularFireSchedulers(zone);
119 this.keepUnstableUntilFirst = i1.ɵkeepUnstableUntilFirstFactory(this.schedulers);
120 var app = i1.ɵfirebaseAppFactory(options, zone, nameOrConfig);
121 this.storage = i1.ɵfetchInstance(app.name + ".storage." + storageBucket, 'AngularFireStorage', app, function () {
122 var storage = zone.runOutsideAngular(function () { return app.storage(storageBucket || undefined); });
123 if (maxUploadRetryTime) {
124 storage.setMaxUploadRetryTime(maxUploadRetryTime);
125 }
126 if (maxOperationRetryTime) {
127 storage.setMaxOperationRetryTime(maxOperationRetryTime);
128 }
129 return storage;
130 }, [maxUploadRetryTime, maxOperationRetryTime]);
131 }
132 AngularFireStorage.prototype.ref = function (path) {
133 return createStorageRef(this.storage.ref(path), this.schedulers, this.keepUnstableUntilFirst);
134 };
135 AngularFireStorage.prototype.refFromURL = function (path) {
136 return createStorageRef(this.storage.refFromURL(path), this.schedulers, this.keepUnstableUntilFirst);
137 };
138 AngularFireStorage.prototype.upload = function (path, data, metadata) {
139 var storageRef = this.storage.ref(path);
140 var ref = createStorageRef(storageRef, this.schedulers, this.keepUnstableUntilFirst);
141 return ref.put(data, metadata);
142 };
143 return AngularFireStorage;
144 }());
145 /** @nocollapse */ AngularFireStorage.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function AngularFireStorage_Factory() { return new AngularFireStorage(i0__namespace.ɵɵinject(i1__namespace.FIREBASE_OPTIONS), i0__namespace.ɵɵinject(i1__namespace.FIREBASE_APP_NAME, 8), i0__namespace.ɵɵinject(BUCKET, 8), i0__namespace.ɵɵinject(i0__namespace.PLATFORM_ID), i0__namespace.ɵɵinject(i0__namespace.NgZone), i0__namespace.ɵɵinject(MAX_UPLOAD_RETRY_TIME, 8), i0__namespace.ɵɵinject(MAX_OPERATION_RETRY_TIME, 8)); }, token: AngularFireStorage, providedIn: "any" });
146 AngularFireStorage.decorators = [
147 { type: i0.Injectable, args: [{
148 providedIn: 'any'
149 },] }
150 ];
151 /** @nocollapse */
152 AngularFireStorage.ctorParameters = function () { return [
153 { type: undefined, decorators: [{ type: i0.Inject, args: [i1.FIREBASE_OPTIONS,] }] },
154 { type: undefined, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [i1.FIREBASE_APP_NAME,] }] },
155 { type: String, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [BUCKET,] }] },
156 { type: Object, decorators: [{ type: i0.Inject, args: [i0.PLATFORM_ID,] }] },
157 { type: i0.NgZone },
158 { type: undefined, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [MAX_UPLOAD_RETRY_TIME,] }] },
159 { type: undefined, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [MAX_OPERATION_RETRY_TIME,] }] }
160 ]; };
161
162 /** to be used with in combination with | async */
163 var GetDownloadURLPipe = /** @class */ (function () {
164 function GetDownloadURLPipe(storage, cdr) {
165 this.storage = storage;
166 this.asyncPipe = new common.AsyncPipe(cdr);
167 }
168 GetDownloadURLPipe.prototype.transform = function (path) {
169 if (path !== this.path) {
170 this.path = path;
171 this.downloadUrl$ = this.storage.ref(path).getDownloadURL();
172 }
173 return this.asyncPipe.transform(this.downloadUrl$);
174 };
175 GetDownloadURLPipe.prototype.ngOnDestroy = function () {
176 this.asyncPipe.ngOnDestroy();
177 };
178 return GetDownloadURLPipe;
179 }());
180 GetDownloadURLPipe.decorators = [
181 { type: i0.Pipe, args: [{
182 name: 'getDownloadURL',
183 pure: false,
184 },] }
185 ];
186 /** @nocollapse */
187 GetDownloadURLPipe.ctorParameters = function () { return [
188 { type: AngularFireStorage },
189 { type: i0.ChangeDetectorRef }
190 ]; };
191 var GetDownloadURLPipeModule = /** @class */ (function () {
192 function GetDownloadURLPipeModule() {
193 }
194 return GetDownloadURLPipeModule;
195 }());
196 GetDownloadURLPipeModule.decorators = [
197 { type: i0.NgModule, args: [{
198 declarations: [GetDownloadURLPipe],
199 exports: [GetDownloadURLPipe],
200 },] }
201 ];
202
203 var AngularFireStorageModule = /** @class */ (function () {
204 function AngularFireStorageModule() {
205 }
206 return AngularFireStorageModule;
207 }());
208 AngularFireStorageModule.decorators = [
209 { type: i0.NgModule, args: [{
210 exports: [GetDownloadURLPipeModule],
211 providers: [AngularFireStorage]
212 },] }
213 ];
214
215 /**
216 * Generated bundle index. Do not edit.
217 */
218
219 exports.AngularFireStorage = AngularFireStorage;
220 exports.AngularFireStorageModule = AngularFireStorageModule;
221 exports.BUCKET = BUCKET;
222 exports.GetDownloadURLPipe = GetDownloadURLPipe;
223 exports.GetDownloadURLPipeModule = GetDownloadURLPipeModule;
224 exports.MAX_OPERATION_RETRY_TIME = MAX_OPERATION_RETRY_TIME;
225 exports.MAX_UPLOAD_RETRY_TIME = MAX_UPLOAD_RETRY_TIME;
226 exports.createStorageRef = createStorageRef;
227 exports.createUploadTask = createUploadTask;
228 exports.fromTask = fromTask;
229
230 Object.defineProperty(exports, '__esModule', { value: true });
231
232})));
233//# sourceMappingURL=angular-fire-storage.umd.js.map