Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' | ngfbStorageUrl | async" />`
})
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
13 changes: 13 additions & 0 deletions src/storage/pipes/storageUrl.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Pipe } from '@angular/core';
import { AngularFireStorage } from '../storage';

/** to be used with in combination with | async */
@Pipe({
name: 'ngfbStorageUrl'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could call this downloadURL, getDownloadURL, storageDownloadURL or something to that effect, @davideast any thoughts?

Could we make it so this was an async pipe itself so it didn't have to be combined with async? So the developer could just do 'someref.jpeg' | storageDownloadURL... or maybe then we call it asyncDownloadURL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experience and recommendation is to avoid being too generic as to avoid name collisions as this type of functionality is common to convert a string to a url.

Also, if you removed the need for the async pipe, appending a dollar sign ngfbStorageUrl$ is a standard I’ve read to be clear that an in template variable is or is going through an observable process. It’s a practice to make clear something async is at play. I actually recommend against removing the need for the async pipe OR offer both (one pipe with a trailing $ sign and one without)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both would be cool, but we could scope as a separate PR if you felt so inclined.

How about getFirebaseDownloadURL and firebaseDownloadURL$ for naming then? That way it's super clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that a lot. Great collaboration. I’d like to have this code in my project, how can I help move this along?

I can make the changes but perhaps @davideast would like throw an opinion in the hat?

Thank you very kindly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ps, I don’t know how to have a pipe that has to be async not require the async pipe.

The end all be all maybe that the async pipe is actually a requirement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to “spam”

Here is dollar sign suffix docs:
https://angular.io/guide/rx-library#naming-conventions-for-observables

}) export class AngularFirestoreStorageUrl implements PipeTransform {
constructor(public storage: AngularFireStorage) {}

transform(path) {
return this.storage.ref(path).getDownloadURL();
}
}
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 { AngularFireStorage } from './storage';
import { AngularFirestoreStorageUrl } from './pipes/storageUrl.pipe';

@NgModule({
declarations: [ AngularFirestoreStorageUrl ],
providers: [ AngularFireStorage ]
})
export class AngularFireStorageModule { }
24 changes: 24 additions & 0 deletions src/storage/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AngularFireStorage, AngularFireStorageModule, AngularFireUploadTask, BU
import { COMMON_CONFIG } from '../test-config';
import 'firebase/storage';
import { rando } from '../firestore/utils.spec';
import { AngularFirestoreStorageUrl } from './pipes/storageUrl.pipe';

describe('AngularFireStorage', () => {
let app: FirebaseApp;
Expand Down Expand Up @@ -135,6 +136,29 @@ describe('AngularFireStorage', () => {

}

describe('pipes', () => {
it('ngfbStorageUrl should download a url', (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);
const ngfbStorageUrl = new AngularFirestoreStorageUrl( afStorage );

// Wait for the upload
task.then(() => {
ngfbStorageUrl.transform('af.json')
.subscribe(
url => {
expect(url).toBeDefined();
done();
},
done.fail
);
})
.catch( done.fail );
});
});

});

describe('AngularFireStorage w/options', () => {
Expand Down