Skip to content

Commit 2a0a93a

Browse files
committed
feat(storage): download url observables
1 parent b0d4668 commit 2a0a93a

File tree

6 files changed

+69
-28
lines changed

6 files changed

+69
-28
lines changed

src/storage/observable/fromRef.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { storage } from 'firebase/app';
2+
import { Observable } from 'rxjs/Observable';
3+
import { from } from 'rxjs/observable/from';
4+
5+
export function getDownloadURL(ref: storage.Reference) {
6+
return from(ref.getDownloadURL());
7+
}
8+
9+
export function getMetadata(ref: storage.Reference) {
10+
return from(ref.getMetadata());
11+
}

src/storage/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from './ref';
22
export * from './storage';
33
export * from './task';
44
export * from './observable/fromTask';
5+
export * from './observable/fromRef';
56
export * from './storage.module';

src/storage/put.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { storage } from 'firebase/app';
2+
import { createUploadTask } from './task';
3+
import { Observable } from 'rxjs/Observable';
4+
5+
/**
6+
* A higher order function (function that makes a function) that
7+
* starts an upload and creates an AngularFire wrapped upload task.
8+
* @param ref
9+
*/
10+
export function createPut(ref: storage.Reference) {
11+
return function put(data: any, metadata?: storage.UploadMetadata) {
12+
const task = ref.put(data, metadata);
13+
return createUploadTask(task);
14+
}
15+
}

src/storage/ref.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { storage } from 'firebase/app';
2-
import { AngularFireUploadTask } from './task';
2+
import { createPut } from './put';
3+
import { Observable } from 'rxjs/Observable';
4+
import { from } from 'rxjs/observable/from';
5+
import { getDownloadURL, getMetadata } from './observable/fromRef';
36

4-
export class AngularFireStorageRef {
5-
constructor(public ref: storage.Reference) { }
6-
put(data: any, metadata?: storage.UploadMetadata) {
7-
const task = this.ref.put(data, metadata);
8-
return new AngularFireUploadTask(task);
7+
/**
8+
* Create an AngularFire wrapped Storage Reference. This object
9+
* creates observable methods from promise based methods.
10+
* @param ref
11+
*/
12+
export function createStorageRef(ref: storage.Reference) {
13+
return {
14+
getDownloadURL() { return getDownloadURL(ref); },
15+
getMetadata() { return getMetadata(ref); },
16+
put(data: any, metadata?: storage.UploadMetadata) {
17+
return createPut(ref)(data, metadata);
18+
}
919
}
1020
}
11-

src/storage/storage.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { Injectable } from '@angular/core';
22
import { storage } from 'firebase/app';
3-
import 'firebase/storage';
43
import { FirebaseApp } from 'angularfire2';
5-
import { AngularFireStorageRef } from './ref';
6-
import { AngularFireUploadTask } from './task';
4+
import { createStorageRef } from './ref';
5+
import { createUploadTask } from './task';
6+
import { Observable } from 'rxjs/Observable';
77

8+
/**
9+
* AngularFireStorage Service
10+
*
11+
* This service is the main entry point for this feature module. It provides
12+
* an API for uploading and downloading binary files from Cloud Storage for
13+
* Firebase.
14+
*
15+
*/
816
@Injectable()
917
export class AngularFireStorage {
1018
storage: storage.Storage;
@@ -14,12 +22,12 @@ export class AngularFireStorage {
1422
}
1523

1624
ref(path: string) {
17-
return new AngularFireStorageRef(this.storage.ref(path));
25+
return createStorageRef(this.storage.ref(path));
1826
}
1927

20-
upload(pathOrRef: string, data: any, metadata?: storage.UploadMetadata) {
21-
const storageRef = this.storage.ref(pathOrRef);
22-
const ref = new AngularFireStorageRef(storageRef);
28+
upload(path: string, data: any, metadata?: storage.UploadMetadata) {
29+
const storageRef = this.storage.ref(path);
30+
const ref = createStorageRef(storageRef);
2331
return ref.put(data, metadata);
2432
}
2533

src/storage/task.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import { storage } from 'firebase/app';
22
import { fromTask } from './observable/fromTask';
33
import { Observable } from 'rxjs/Observable';
44

5-
export class AngularFireUploadTask {
6-
constructor(public task: storage.UploadTask) {}
7-
8-
snapshotChanges() { return fromTask(this.task); }
9-
10-
pause() { return this.task.pause(); }
11-
12-
cancel() { return this.task.cancel(); }
13-
14-
resume() { return this.task.resume(); }
15-
16-
then() { return this.task.then(); }
17-
18-
catch(onRejected: (a: Error) => any) { return this.task.catch(onRejected); }
5+
export function createUploadTask(task: storage.UploadTask) {
6+
return {
7+
snapshotChanges() { return fromTask(task); },
8+
pause() { return task.pause(); },
9+
cancel() { return task.cancel(); },
10+
resume() { return task.resume(); },
11+
then() { return task.then(); },
12+
catch(onRejected: (a: Error) => any) {
13+
return task.catch(onRejected);
14+
}
15+
};
1916
}

0 commit comments

Comments
 (0)