2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Storage representation wrapper
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2017-02-14 11:31:42 +00:00
|
|
|
|
|
|
|
import StorageRef from './reference';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
|
|
|
|
import { getLogger } from '../../utils/log';
|
2018-01-05 17:20:02 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-02-14 11:31:42 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
import type App from '../core/app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2017-03-02 13:09:41 +00:00
|
|
|
const FirebaseStorage = NativeModules.RNFirebaseStorage;
|
2017-02-14 11:31:42 +00:00
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
const NATIVE_EVENTS = ['storage_event', 'storage_error'];
|
2017-12-22 15:57:33 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseStorage';
|
|
|
|
export const NAMESPACE = 'storage';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export default class Storage extends ModuleBase {
|
2017-03-23 00:54:19 +00:00
|
|
|
/**
|
|
|
|
*
|
2018-01-05 17:20:02 +00:00
|
|
|
* @param app
|
2017-03-23 00:54:19 +00:00
|
|
|
* @param options
|
|
|
|
*/
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: true,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-05-25 22:39:06 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
getAppEventName(this, 'storage_event'),
|
2018-01-25 18:25:39 +00:00
|
|
|
this._handleStorageEvent.bind(this)
|
2017-02-14 11:31:42 +00:00
|
|
|
);
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
getAppEventName(this, 'storage_error'),
|
2018-01-25 18:25:39 +00:00
|
|
|
this._handleStorageEvent.bind(this)
|
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}
|
|
|
|
*/
|
2018-01-05 17:20:02 +00:00
|
|
|
refFromURL(url: string): 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-12-04 12:07:41 +00:00
|
|
|
setMaxOperationRetryTime(time: number): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).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-12-04 12:07:41 +00:00
|
|
|
setMaxUploadRetryTime(time: number): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).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-12-04 12:07:41 +00:00
|
|
|
setMaxDownloadRetryTime(time: number): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setMaxDownloadRetryTime(time);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
/**
|
2017-03-23 00:54:19 +00:00
|
|
|
* INTERNALS
|
2017-12-04 12:07:41 +00:00
|
|
|
*/
|
|
|
|
_getSubEventName(path: string, eventName: string) {
|
2017-12-22 15:24:31 +00:00
|
|
|
return getAppEventName(this, `${path}-${eventName}`);
|
2017-07-12 14:49:33 +00:00
|
|
|
}
|
2017-03-23 00:54:19 +00:00
|
|
|
|
2017-02-14 11:31:42 +00:00
|
|
|
_handleStorageEvent(event: Object) {
|
|
|
|
const { path, eventName } = event;
|
|
|
|
const body = event.body || {};
|
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).debug('_handleStorageEvent: ', path, eventName, body);
|
|
|
|
SharedEventEmitter.emit(this._getSubEventName(path, eventName), body);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleStorageError(err: Object) {
|
2017-12-04 12:07:41 +00:00
|
|
|
const { path, eventName } = err;
|
|
|
|
const body = err.body || {};
|
2017-07-12 14:49:33 +00:00
|
|
|
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).debug('_handleStorageError ->', err);
|
|
|
|
SharedEventEmitter.emit(this._getSubEventName(path, eventName), body);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
_addListener(
|
|
|
|
path: string,
|
|
|
|
eventName: string,
|
|
|
|
cb: (evt: Object) => Object
|
|
|
|
): void {
|
2017-12-22 15:24:31 +00:00
|
|
|
SharedEventEmitter.addListener(this._getSubEventName(path, eventName), cb);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
_removeListener(
|
|
|
|
path: string,
|
|
|
|
eventName: string,
|
|
|
|
origCB: (evt: Object) => Object
|
|
|
|
): void {
|
|
|
|
SharedEventEmitter.removeListener(
|
|
|
|
this._getSubEventName(path, eventName),
|
|
|
|
origCB
|
|
|
|
);
|
2017-02-14 11:31:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:15:02 +00:00
|
|
|
export const statics = {
|
|
|
|
TaskEvent: {
|
|
|
|
STATE_CHANGED: 'state_changed',
|
|
|
|
},
|
|
|
|
TaskState: {
|
2017-03-22 23:23:48 +00:00
|
|
|
RUNNING: 'running',
|
|
|
|
PAUSED: 'paused',
|
|
|
|
SUCCESS: 'success',
|
|
|
|
CANCELLED: 'cancelled',
|
|
|
|
ERROR: 'error',
|
2017-03-22 20:15:02 +00:00
|
|
|
},
|
2018-01-25 18:25:39 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
: {},
|
2017-03-22 20:15:02 +00:00
|
|
|
};
|