1 | import { Observable, of, from } from 'rxjs';
|
2 | import { debounceTime, map, observeOn, switchMap } from 'rxjs/operators';
|
3 | import * as i0 from '@angular/core';
|
4 | import { InjectionToken, Injectable, Inject, Optional, PLATFORM_ID, NgZone, Pipe, ChangeDetectorRef, NgModule } from '@angular/core';
|
5 | import * as i1 from '@angular/fire';
|
6 | import { ɵAngularFireSchedulers, ɵkeepUnstableUntilFirstFactory, ɵfirebaseAppFactory, ɵfetchInstance, FIREBASE_OPTIONS, FIREBASE_APP_NAME } from '@angular/fire';
|
7 | import 'firebase/storage';
|
8 | import { AsyncPipe } from '@angular/common';
|
9 |
|
10 |
|
11 |
|
12 | function fromTask(task) {
|
13 | return new Observable(subscriber => {
|
14 | const progress = (snap) => subscriber.next(snap);
|
15 | const error = e => subscriber.error(e);
|
16 | const complete = () => subscriber.complete();
|
17 |
|
18 |
|
19 | progress(task.snapshot);
|
20 | const unsub = task.on('state_changed', progress);
|
21 |
|
22 |
|
23 |
|
24 | task.then(snapshot => {
|
25 | progress(snapshot);
|
26 | complete();
|
27 | }, e => {
|
28 |
|
29 | progress(task.snapshot);
|
30 | error(e);
|
31 | });
|
32 |
|
33 | return function unsubscribe() {
|
34 | unsub();
|
35 | };
|
36 | }).pipe(
|
37 |
|
38 |
|
39 | debounceTime(0));
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function createUploadTask(task) {
|
48 | const inner$ = fromTask(task);
|
49 | return {
|
50 | task,
|
51 | then: task.then.bind(task),
|
52 | catch: task.catch.bind(task),
|
53 | pause: task.pause.bind(task),
|
54 | cancel: task.cancel.bind(task),
|
55 | resume: task.resume.bind(task),
|
56 | snapshotChanges: () => inner$,
|
57 | percentageChanges: () => inner$.pipe(map(s => s.bytesTransferred / s.totalBytes * 100))
|
58 | };
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function createStorageRef(ref, schedulers, keepUnstableUntilFirst) {
|
66 | return {
|
67 | getDownloadURL: () => of(undefined).pipe(observeOn(schedulers.outsideAngular), switchMap(() => ref.getDownloadURL()), keepUnstableUntilFirst),
|
68 | getMetadata: () => of(undefined).pipe(observeOn(schedulers.outsideAngular), switchMap(() => ref.getMetadata()), keepUnstableUntilFirst),
|
69 | delete: () => from(ref.delete()),
|
70 | child: (path) => createStorageRef(ref.child(path), schedulers, keepUnstableUntilFirst),
|
71 | updateMetadata: (meta) => from(ref.updateMetadata(meta)),
|
72 | put: (data, metadata) => {
|
73 | const task = ref.put(data, metadata);
|
74 | return createUploadTask(task);
|
75 | },
|
76 | putString: (data, format, metadata) => {
|
77 | const task = ref.putString(data, format, metadata);
|
78 | return createUploadTask(task);
|
79 | },
|
80 | listAll: () => from(ref.listAll())
|
81 | };
|
82 | }
|
83 |
|
84 | const BUCKET = new InjectionToken('angularfire2.storageBucket');
|
85 | const MAX_UPLOAD_RETRY_TIME = new InjectionToken('angularfire2.storage.maxUploadRetryTime');
|
86 | const MAX_OPERATION_RETRY_TIME = new InjectionToken('angularfire2.storage.maxOperationRetryTime');
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | class AngularFireStorage {
|
95 | constructor(options, nameOrConfig, storageBucket,
|
96 | // tslint:disable-next-line:ban-types
|
97 | platformId, zone, maxUploadRetryTime, maxOperationRetryTime) {
|
98 | this.schedulers = new ɵAngularFireSchedulers(zone);
|
99 | this.keepUnstableUntilFirst = ɵkeepUnstableUntilFirstFactory(this.schedulers);
|
100 | const app = ɵfirebaseAppFactory(options, zone, nameOrConfig);
|
101 | this.storage = ɵfetchInstance(`${app.name}.storage.${storageBucket}`, 'AngularFireStorage', app, () => {
|
102 | const storage = zone.runOutsideAngular(() => app.storage(storageBucket || undefined));
|
103 | if (maxUploadRetryTime) {
|
104 | storage.setMaxUploadRetryTime(maxUploadRetryTime);
|
105 | }
|
106 | if (maxOperationRetryTime) {
|
107 | storage.setMaxOperationRetryTime(maxOperationRetryTime);
|
108 | }
|
109 | return storage;
|
110 | }, [maxUploadRetryTime, maxOperationRetryTime]);
|
111 | }
|
112 | ref(path) {
|
113 | return createStorageRef(this.storage.ref(path), this.schedulers, this.keepUnstableUntilFirst);
|
114 | }
|
115 | refFromURL(path) {
|
116 | return createStorageRef(this.storage.refFromURL(path), this.schedulers, this.keepUnstableUntilFirst);
|
117 | }
|
118 | upload(path, data, metadata) {
|
119 | const storageRef = this.storage.ref(path);
|
120 | const ref = createStorageRef(storageRef, this.schedulers, this.keepUnstableUntilFirst);
|
121 | return ref.put(data, metadata);
|
122 | }
|
123 | }
|
124 | AngularFireStorage.ɵprov = i0.ɵɵdefineInjectable({ factory: function AngularFireStorage_Factory() { return new AngularFireStorage(i0.ɵɵinject(i1.FIREBASE_OPTIONS), i0.ɵɵinject(i1.FIREBASE_APP_NAME, 8), i0.ɵɵinject(BUCKET, 8), i0.ɵɵinject(i0.PLATFORM_ID), i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(MAX_UPLOAD_RETRY_TIME, 8), i0.ɵɵinject(MAX_OPERATION_RETRY_TIME, 8)); }, token: AngularFireStorage, providedIn: "any" });
|
125 | AngularFireStorage.decorators = [
|
126 | { type: Injectable, args: [{
|
127 | providedIn: 'any'
|
128 | },] }
|
129 | ];
|
130 |
|
131 | AngularFireStorage.ctorParameters = () => [
|
132 | { type: undefined, decorators: [{ type: Inject, args: [FIREBASE_OPTIONS,] }] },
|
133 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [FIREBASE_APP_NAME,] }] },
|
134 | { type: String, decorators: [{ type: Optional }, { type: Inject, args: [BUCKET,] }] },
|
135 | { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
|
136 | { type: NgZone },
|
137 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAX_UPLOAD_RETRY_TIME,] }] },
|
138 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAX_OPERATION_RETRY_TIME,] }] }
|
139 | ];
|
140 |
|
141 |
|
142 | class GetDownloadURLPipe {
|
143 | constructor(storage, cdr) {
|
144 | this.storage = storage;
|
145 | this.asyncPipe = new AsyncPipe(cdr);
|
146 | }
|
147 | transform(path) {
|
148 | if (path !== this.path) {
|
149 | this.path = path;
|
150 | this.downloadUrl$ = this.storage.ref(path).getDownloadURL();
|
151 | }
|
152 | return this.asyncPipe.transform(this.downloadUrl$);
|
153 | }
|
154 | ngOnDestroy() {
|
155 | this.asyncPipe.ngOnDestroy();
|
156 | }
|
157 | }
|
158 | GetDownloadURLPipe.decorators = [
|
159 | { type: Pipe, args: [{
|
160 | name: 'getDownloadURL',
|
161 | pure: false,
|
162 | },] }
|
163 | ];
|
164 |
|
165 | GetDownloadURLPipe.ctorParameters = () => [
|
166 | { type: AngularFireStorage },
|
167 | { type: ChangeDetectorRef }
|
168 | ];
|
169 | class GetDownloadURLPipeModule {
|
170 | }
|
171 | GetDownloadURLPipeModule.decorators = [
|
172 | { type: NgModule, args: [{
|
173 | declarations: [GetDownloadURLPipe],
|
174 | exports: [GetDownloadURLPipe],
|
175 | },] }
|
176 | ];
|
177 |
|
178 | class AngularFireStorageModule {
|
179 | }
|
180 | AngularFireStorageModule.decorators = [
|
181 | { type: NgModule, args: [{
|
182 | exports: [GetDownloadURLPipeModule],
|
183 | providers: [AngularFireStorage]
|
184 | },] }
|
185 | ];
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | export { AngularFireStorage, AngularFireStorageModule, BUCKET, GetDownloadURLPipe, GetDownloadURLPipeModule, MAX_OPERATION_RETRY_TIME, MAX_UPLOAD_RETRY_TIME, createStorageRef, createUploadTask, fromTask };
|
192 |
|