react-native-firebase/lib/modules/storage/reference.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-02-14 11:31:42 +00:00
/* @flow */
import ReferenceBase from '../../utils/ReferenceBase';
2017-02-14 11:31:42 +00:00
import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task';
import Storage from './';
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference
*/
export default class StorageReference extends ReferenceBase {
constructor(storage: Storage, path: string) {
2017-05-30 11:28:53 +00:00
super(path);
2017-02-14 11:31:42 +00:00
this.storage = storage;
}
get fullPath(): string {
2017-02-14 11:31:42 +00:00
return this.path;
}
// todo return full gs://bucket/path
2017-02-14 11:31:42 +00:00
toString(): String {
return this.path;
}
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#child
* @param path
* @returns {StorageReference}
*/
child(path: string) {
return new StorageReference(this.storage, `${this.path}/${path}`);
}
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#delete
* @returns {Promise.<T>|*}
*/
2017-03-02 13:09:41 +00:00
delete(): Promise<*> {
return this.storate._native.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>|*}
*/
getDownloadURL(): Promise<String> {
return this.storate._native.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> {
return this.storate._native.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> {
return this.storate._native.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> {
return new StorageTask(DOWNLOAD_TASK, this.storate._native.downloadFile(this.path, filePath), this);
2017-02-14 11:31:42 +00:00
}
/**
* Alias to putFile
* @returns {StorageReference.putFile}
*/
2017-04-28 09:27:29 +00:00
get put(): Function {
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://', '');
return new StorageTask(UPLOAD_TASK, this.storate._native.putFile(this.path, _filePath, metadata), this);
2017-02-14 11:31:42 +00:00
}
}