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

149 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-02-14 11:31:42 +00:00
/* @flow */
import { NativeModules } from 'react-native';
2017-02-14 11:31:42 +00:00
import StorageRef from './reference';
import ModuleBase from './../../utils/ModuleBase';
2017-02-14 11:31:42 +00:00
2017-03-02 13:09:41 +00:00
const FirebaseStorage = NativeModules.RNFirebaseStorage;
2017-02-14 11:31:42 +00:00
export default class Storage extends ModuleBase {
2017-03-23 00:54:19 +00:00
/**
*
* @param firebaseApp
2017-03-23 00:54:19 +00:00
* @param options
*/
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options, 'Storage', true);
this._subscriptions = {};
this._successListener = this._eventEmitter.addListener(
2017-02-14 11:31:42 +00:00
'storage_event',
event => this._handleStorageEvent(event),
2017-02-14 11:31:42 +00:00
);
this._errorListener = this._eventEmitter.addListener(
2017-02-14 11:31:42 +00:00
'storage_error',
err => this._handleStorageError(err),
2017-02-14 11:31:42 +00:00
);
}
2017-03-23 00:54:19 +00:00
/**
* Returns a reference for the given path in the default bucket.
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#ref
* @param path
* @returns {StorageReference}
*/
2017-02-14 11:31:42 +00:00
ref(path: string): StorageRef {
return new StorageRef(this, path);
}
2017-03-23 00:54:19 +00:00
/**
* Returns a reference for the given absolute URL.
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#refFromURL
* @param url
* @returns {StorageReference}
*/
2017-02-14 11:31:42 +00:00
refFromURL(url: string): Promise<StorageRef> {
2017-03-23 00:54:19 +00:00
// TODO don't think this is correct?
2017-02-14 11:31:42 +00:00
return new StorageRef(this, `url::${url}`);
}
2017-03-23 00:54:19 +00:00
/**
* setMaxOperationRetryTime
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#setMaxOperationRetryTime
* @param time The new maximum operation retry time in milliseconds.
*/
2017-02-14 11:31:42 +00:00
setMaxOperationRetryTime(time: number) {
this._native.setMaxOperationRetryTime(time);
2017-02-14 11:31:42 +00:00
}
2017-03-23 00:54:19 +00:00
/**
* setMaxUploadRetryTime
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#setMaxUploadRetryTime
* @param time The new maximum upload retry time in milliseconds.
*/
2017-02-14 11:31:42 +00:00
setMaxUploadRetryTime(time: number) {
this._native.setMaxUploadRetryTime(time);
2017-02-14 11:31:42 +00:00
}
2017-03-23 00:54:19 +00:00
/**
* setMaxDownloadRetryTime
* @url N/A
* @param time The new maximum download retry time in milliseconds.
*/
2017-02-14 11:31:42 +00:00
setMaxDownloadRetryTime(time: number) {
this._native.setMaxDownloadRetryTime(time);
2017-02-14 11:31:42 +00:00
}
2017-03-23 00:54:19 +00:00
/** **********
* INTERNALS
********** **/
2017-02-14 11:31:42 +00:00
_handleStorageEvent(event: Object) {
const { path, eventName } = event;
const body = event.body || {};
this.log.debug('_handleStorageEvent: ', path, eventName, body);
if (this._subscriptions[path] && this._subscriptions[path][eventName]) {
this._subscriptions[path][eventName].forEach((cb) => {
2017-02-14 11:31:42 +00:00
cb(body);
});
}
}
_handleStorageError(err: Object) {
this.log.debug('_handleStorageError ->', err);
}
_addListener(path: string, eventName: string, cb: (evt: Object) => Object) {
if (!this._subscriptions[path]) this._subscriptions[path] = {};
if (!this._subscriptions[path][eventName]) this._subscriptions[path][eventName] = [];
this._subscriptions[path][eventName].push(cb);
2017-02-14 11:31:42 +00:00
}
_removeListener(path: string, eventName: string, origCB: (evt: Object) => Object) {
if (!this._subscriptions[path] || (eventName && !this._subscriptions[path][eventName])) {
2017-02-14 11:31:42 +00:00
this.log.warn('_removeListener() called, but not currently listening at that location (bad path)', path, eventName);
return;
}
if (eventName && origCB) {
const i = this._subscriptions[path][eventName].indexOf(origCB);
2017-02-14 11:31:42 +00:00
if (i === -1) {
this.log.warn('_removeListener() called, but the callback specified is not listening at this location (bad path)', path, eventName);
} else {
this._subscriptions[path][eventName].splice(i, 1);
2017-02-14 11:31:42 +00:00
}
} else if (eventName) {
this._subscriptions[path][eventName] = [];
2017-02-14 11:31:42 +00:00
} else {
this._subscriptions[path] = {};
2017-02-14 11:31:42 +00:00
}
}
}
export const statics = {
TaskEvent: {
STATE_CHANGED: 'state_changed',
},
TaskState: {
RUNNING: 'running',
PAUSED: 'paused',
SUCCESS: 'success',
CANCELLED: 'cancelled',
ERROR: 'error',
},
Native: FirebaseStorage ? {
MAIN_BUNDLE_PATH: FirebaseStorage.MAIN_BUNDLE_PATH,
CACHES_DIRECTORY_PATH: FirebaseStorage.CACHES_DIRECTORY_PATH,
DOCUMENT_DIRECTORY_PATH: FirebaseStorage.DOCUMENT_DIRECTORY_PATH,
EXTERNAL_DIRECTORY_PATH: FirebaseStorage.EXTERNAL_DIRECTORY_PATH,
EXTERNAL_STORAGE_DIRECTORY_PATH: FirebaseStorage.EXTERNAL_STORAGE_DIRECTORY_PATH,
TEMP_DIRECTORY_PATH: FirebaseStorage.TEMP_DIRECTORY_PATH,
LIBRARY_DIRECTORY_PATH: FirebaseStorage.LIBRARY_DIRECTORY_PATH,
FILETYPE_REGULAR: FirebaseStorage.FILETYPE_REGULAR,
FILETYPE_DIRECTORY: FirebaseStorage.FILETYPE_DIRECTORY,
} : {},
};