2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* StorageReference representation wrapper
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
import ReferenceBase from '../../utils/ReferenceBase';
|
2017-02-14 11:31:42 +00:00
|
|
|
import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-12-04 12:07:41 +00:00
|
|
|
import type Storage from './';
|
2017-02-14 11:31:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference
|
|
|
|
*/
|
|
|
|
export default class StorageReference extends ReferenceBase {
|
2017-12-04 12:07:41 +00:00
|
|
|
_storage: Storage;
|
|
|
|
|
2017-02-14 11:31:42 +00:00
|
|
|
constructor(storage: Storage, path: string) {
|
2018-01-05 17:20:02 +00:00
|
|
|
super(path);
|
2017-08-14 13:06:37 +00:00
|
|
|
this._storage = storage;
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 09:33:18 +00:00
|
|
|
get fullPath(): string {
|
2017-02-14 11:31:42 +00:00
|
|
|
return this.path;
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
toString(): string {
|
2017-08-14 13:11:17 +00:00
|
|
|
return `gs://${this._storage.app.options.storageBucket}${this.path}`;
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#child
|
|
|
|
* @param path
|
|
|
|
* @returns {StorageReference}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
child(path: string): StorageReference {
|
|
|
|
return new StorageReference(this._storage, `${this.path}/${path}`);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#delete
|
|
|
|
* @returns {Promise.<T>|*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
delete(): Promise<void> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._storage).delete(this.path);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getDownloadURL
|
|
|
|
* @returns {Promise.<T>|*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getDownloadURL(): Promise<string> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._storage).getDownloadURL(this.path);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getMetadata
|
|
|
|
* @returns {Promise.<T>|*}
|
|
|
|
*/
|
|
|
|
getMetadata(): Promise<Object> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._storage).getMetadata(this.path);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#updateMetadata
|
|
|
|
* @param metadata
|
|
|
|
* @returns {Promise.<T>|*}
|
|
|
|
*/
|
|
|
|
updateMetadata(metadata: Object = {}): Promise<Object> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this._storage).updateMetadata(this.path, metadata);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads a reference to the device
|
|
|
|
* @param {String} filePath Where to store the file
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
downloadFile(filePath: string): Promise<Object> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return new StorageTask(DOWNLOAD_TASK, getNativeModule(this._storage).downloadFile(this.path, filePath), this);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 15:15:53 +00:00
|
|
|
/**
|
|
|
|
* Alias to putFile
|
|
|
|
* @returns {StorageReference.putFile}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
get put(): (Object, Object) => Promise<Object> {
|
2017-04-07 15:15:53 +00:00
|
|
|
return this.putFile;
|
|
|
|
}
|
|
|
|
|
2017-02-14 11:31:42 +00:00
|
|
|
/**
|
|
|
|
* Upload a file path
|
|
|
|
* @param {string} filePath The local path of the file
|
|
|
|
* @param {object} metadata An object containing metadata
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
putFile(filePath: Object, metadata: Object = {}): Promise<Object> {
|
|
|
|
const _filePath = filePath.replace('file://', '');
|
2018-01-05 17:20:02 +00:00
|
|
|
return new StorageTask(UPLOAD_TASK, getNativeModule(this._storage).putFile(this.path, _filePath, metadata), this);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
}
|