Skip to content

Commit

Permalink
feat(storage): getDownloadURL pipe (#2648)
Browse files Browse the repository at this point in the history
Co-authored-by: Admin Apple <adminapple@ackerlaptop.local>
Co-authored-by: Admin Apple <acker.dawn.apple@gmail.com>
  • Loading branch information
3 people authored Nov 12, 2020
1 parent 1bbd3e4 commit 0d799da
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 91 deletions.
10 changes: 10 additions & 0 deletions docs/storage/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ export class AppComponent {

### Downloading Files

A convenient pipe exists for simple in page references.

```ts
@Component({
selector: 'app-root',
template: `<img [src]="'users/davideast.jpg' | getDownloadURL" />`
})
export class AppComponent {}
```

To download a file you'll need to create a reference and call the `getDownloadURL()` method on an `AngularFireStorageReference`.

```ts
Expand Down
3 changes: 2 additions & 1 deletion sample/src/app/storage/storage.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const TRANSPARENT_PNG
template: `
<p>
Storage!
<img [src]="downloadUrl$ | async" width="64" height="64"/>
<img [src]="downloadUrl$ | async" width="64" height="64" />
<br><small>{{ 'google-g.png' | getDownloadURL | json }}</small>
</p>
`,
styles: []
Expand Down
39 changes: 39 additions & 0 deletions src/storage/pipes/storageUrl.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { AsyncPipe } from '@angular/common';
import { ChangeDetectorRef, NgModule, OnDestroy, Pipe, PipeTransform } from '@angular/core';
import { Observable } from 'rxjs';
import { AngularFireStorage } from '../storage';

/** to be used with in combination with | async */
@Pipe({
name: 'getDownloadURL',
pure: false,
})
export class GetDownloadURLPipe implements PipeTransform, OnDestroy {

private asyncPipe: AsyncPipe;
private path: string;
private downloadUrl$: Observable<any>;

constructor(private storage: AngularFireStorage, cdr: ChangeDetectorRef) {
this.asyncPipe = new AsyncPipe(cdr);
}

transform(path: string) {
if (path !== this.path) {
this.path = path;
this.downloadUrl$ = this.storage.ref(path).getDownloadURL();
}
return this.asyncPipe.transform(this.downloadUrl$);
}

ngOnDestroy() {
this.asyncPipe.ngOnDestroy();
}

}

@NgModule({
declarations: [ GetDownloadURLPipe ],
exports: [ GetDownloadURLPipe ],
})
export class GetDownloadURLPipeModule {}
1 change: 1 addition & 0 deletions src/storage/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './storage';
export * from './task';
export * from './observable/fromTask';
export * from './storage.module';
export * from './pipes/storageUrl.pipe';
2 changes: 2 additions & 0 deletions src/storage/storage.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NgModule } from '@angular/core';
import { GetDownloadURLPipeModule } from './pipes/storageUrl.pipe';
import { AngularFireStorage } from './storage';

@NgModule({
exports: [ GetDownloadURLPipeModule ],
providers: [ AngularFireStorage ]
})
export class AngularFireStorageModule { }
194 changes: 104 additions & 90 deletions src/storage/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { forkJoin } from 'rxjs';
import { forkJoin, from } from 'rxjs';
import { mergeMap, tap } from 'rxjs/operators';
import { TestBed } from '@angular/core/testing';
import { AngularFireModule, FIREBASE_APP_NAME, FIREBASE_OPTIONS, FirebaseApp } from '@angular/fire';
import { AngularFireStorage, AngularFireStorageModule, AngularFireUploadTask, BUCKET } from './public_api';
import { COMMON_CONFIG } from '../test-config';
import 'firebase/storage';
import { rando } from '../firestore/utils.spec';
import { GetDownloadURLPipe } from './pipes/storageUrl.pipe';
import { ChangeDetectorRef } from '@angular/core';
import 'firebase/storage';

if (typeof XMLHttpRequest === 'undefined') {
globalThis.XMLHttpRequest = require('xhr2');
}

const blobOrBuffer = (data: string, options: {}) => {
if (typeof Blob === 'undefined') {
return Buffer.from(data, 'utf8');
} else {
return new Blob([JSON.stringify(data)], options);
}
};

describe('AngularFireStorage', () => {
let app: FirebaseApp;
let afStorage: AngularFireStorage;
let cdr: ChangeDetectorRef;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, rando()),
AngularFireStorageModule
AngularFireStorageModule,
],
providers: [
ChangeDetectorRef
]
});

app = TestBed.inject(FirebaseApp);
afStorage = TestBed.inject(AngularFireStorage);
cdr = TestBed.inject(ChangeDetectorRef);
});

afterEach(() => {
Expand All @@ -39,101 +58,96 @@ describe('AngularFireStorage', () => {
expect(afStorage.storage.app).toBeDefined();
});

// TODO tests for node?
if (typeof Blob !== 'undefined') {

describe('upload task', () => {

it('should upload and delete a file', (done) => {
const data = { angular: 'fire' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
task.snapshotChanges()
.subscribe(
snap => {
expect(snap).toBeDefined();
},
done.fail,
() => {
ref.delete().subscribe(done, done.fail);
});
});

it('should upload a file and observe the download url', (done) => {
const data = { angular: 'fire' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const ref = afStorage.ref('af.json');
ref.put(blob).then(() => {
const url$ = ref.getDownloadURL();
url$.subscribe(
url => {
expect(url).toBeDefined();
},
done.fail,
() => {
ref.delete().subscribe(done, done.fail);
}
);
});
});
describe('upload task', () => {

it('should upload and delete a file', (done) => {
const data = { angular: 'fire' };
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
task.snapshotChanges()
.subscribe(
snap => {
expect(snap).toBeDefined();
},
done.fail,
() => {
ref.delete().subscribe(done, done.fail);
});
});

it('should resolve the task as a promise', (done) => {
const data = { angular: 'promise' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task: AngularFireUploadTask = ref.put(blob);
task.then(snap => {
expect(snap).toBeDefined();
done();
}).catch(done.fail);
it('should upload a file and observe the download url', (done) => {
const data = { angular: 'fire' };
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
ref.put(blob).then(() => {
const url$ = ref.getDownloadURL();
url$.subscribe(
url => {
expect(url).toBeDefined();
},
done.fail,
() => {
ref.delete().subscribe(done, done.fail);
}
);
});

});

describe('reference', () => {
it('should resolve the task as a promise', (done) => {
const data = { angular: 'promise' };
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task: AngularFireUploadTask = ref.put(blob);
task.then(snap => {
expect(snap).toBeDefined();
done();
}).catch(done.fail);
});

it('it should upload, download, and delete', (done) => {
const data = { angular: 'fire' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
// Wait for the upload
forkJoin([task.snapshotChanges()])
.pipe(
// get the url download
mergeMap(() => ref.getDownloadURL()),
// assert the URL
tap(url => expect(url).toBeDefined()),
// Delete the file
mergeMap(() => ref.delete())
)
// finish the test
.subscribe(done, done.fail);
});
});

it('should upload, get metadata, and delete', (done) => {
const data = { angular: 'fire' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob, { customMetadata: { blah: 'blah' } });
// Wait for the upload
forkJoin([task.snapshotChanges()])
.pipe(
// get the metadata download
mergeMap(() => ref.getMetadata()),
// assert the URL
tap(meta => expect(meta.customMetadata).toEqual({ blah: 'blah' })),
// Delete the file
mergeMap(() => ref.delete())
)
// finish the test
.subscribe(done, done.fail);
});
describe('reference', () => {

it('it should upload, download, and delete', (done) => {
const data = { angular: 'fire' };
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
// Wait for the upload
forkJoin([task.snapshotChanges()])
.pipe(
// get the url download
mergeMap(() => ref.getDownloadURL()),
// assert the URL
tap(url => expect(url).toBeDefined()),
// Delete the file
mergeMap(() => ref.delete())
)
// finish the test
.subscribe(done, done.fail);
});

it('should upload, get metadata, and delete', (done) => {
const data = { angular: 'fire' };
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob, { customMetadata: { blah: 'blah' } });
// Wait for the upload
forkJoin([task.snapshotChanges()])
.pipe(
// get the metadata download
mergeMap(() => ref.getMetadata()),
// assert the URL
tap(meta => expect(meta.customMetadata).toEqual({ blah: 'blah' })),
// Delete the file
mergeMap(() => ref.delete())
)
// finish the test
.subscribe(done, done.fail);
});

}
});

});

Expand Down Expand Up @@ -193,7 +207,7 @@ describe('AngularFireStorage w/options', () => {

it('it should upload, download, and delete', (done) => {
const data = { angular: 'fire' };
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
const blob = blobOrBuffer(JSON.stringify(data), { type: 'application/json' });
const ref = afStorage.ref('af.json');
const task = ref.put(blob);
// Wait for the upload
Expand Down

0 comments on commit 0d799da

Please sign in to comment.