2017-02-14 11:31:42 +00:00
|
|
|
/* @flow */
|
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
|
|
|
import { ReferenceBase } from './../base';
|
|
|
|
import StorageTask, { UPLOAD_TASK, DOWNLOAD_TASK } from './task';
|
|
|
|
import Storage from './';
|
|
|
|
|
2017-03-02 13:09:41 +00:00
|
|
|
const FirebaseStorage = NativeModules.RNFirebaseStorage;
|
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 {
|
|
|
|
constructor(storage: Storage, path: string) {
|
|
|
|
super(storage.firebase, path);
|
|
|
|
this.storage = storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fullPath() {
|
|
|
|
return this.path;
|
|
|
|
}
|
|
|
|
|
2017-03-22 19:46:08 +00:00
|
|
|
// 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<*> {
|
2017-03-22 19:46:08 +00:00
|
|
|
return FirebaseStorage.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> {
|
2017-03-22 19:46:08 +00:00
|
|
|
return FirebaseStorage.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> {
|
2017-03-22 19:46:08 +00:00
|
|
|
return FirebaseStorage.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> {
|
2017-03-22 19:46:08 +00:00
|
|
|
return FirebaseStorage.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> {
|
2017-03-22 19:46:08 +00:00
|
|
|
return new StorageTask(DOWNLOAD_TASK, FirebaseStorage.downloadFile(this.path, filePath), this);
|
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://', '');
|
2017-03-22 19:46:08 +00:00
|
|
|
return new StorageTask(UPLOAD_TASK, FirebaseStorage.putFile(this.path, _filePath, metadata), this);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
}
|