Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,10 @@ public void downloadFile(final String urlStr,
@Override
public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputStream inputStream) throws IOException {
int indexOfLastSlash = localFile.lastIndexOf("/");
String pathMinusFileName = localFile.substring(0, indexOfLastSlash) + "/";
String filename = localFile.substring(indexOfLastSlash+1);
String pathMinusFileName = indexOfLastSlash>0 ? localFile.substring(0, indexOfLastSlash) + "/" : "/";
String filename = indexOfLastSlash>0 ? localFile.substring(indexOfLastSlash+1) : localFile;
File fileWithJustPath = new File(pathMinusFileName);
if (!fileWithJustPath.mkdirs()) {
Log.e(TAG, "Directory not created");
WritableMap error = Arguments.createMap();
error.putString("message", "Directory not created");
callback.invoke(error);
return;
}
fileWithJustPath.mkdirs();
File fileWithFullPath = new File(pathMinusFileName, filename);
FileOutputStream output = new FileOutputStream(fileWithFullPath);
int bufferSize = 1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void todoNote(final String tag, final String name, final Callback
public static void sendEvent(final ReactContext context,
final String eventName,
final WritableMap params) {
if (context.hasActiveCatalystInstance()) {
if (context != null && context.hasActiveCatalystInstance()) {
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
Expand Down
3 changes: 1 addition & 2 deletions firestack.android.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @providesModule Firestack
* @flow
*/
import Firestack from './lib/firestack'

export default Firestack
export default Firestack
1 change: 0 additions & 1 deletion firestack.ios.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* @providesModule Firestack
* @flow
*/
import Firestack from './lib/firestack'
Expand Down
4 changes: 2 additions & 2 deletions lib/firestack.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Firestack extends Singleton {
* @param options
* @param name - TODO support naming multiple instances
*/
constructor(options: Object, name: string) {
constructor(options: Object, name?: string) {
const instance = super(options);

instance.options = options || {};
Expand Down Expand Up @@ -192,7 +192,7 @@ export default class Firestack extends Singleton {
/**
* Redux store
**/
get store(): Object {
get store(): ?Object {
return this._store;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/modules/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ export default class Auth extends Base {
* Sign the user in with a third-party authentication provider
* @return {Promise} A promise resolved upon completion
*/
signInWithCredential(credential): Promise<Object> {
signInWithCredential(credential: Object): Promise<Object> {
return promisify('signInWithProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
}

/**
* Re-authenticate a user with a third-party authentication provider
* @return {Promise} A promise resolved upon completion
*/
reauthenticateUser(credential): Promise<Object> {
reauthenticateUser(credential: Object): Promise<Object> {
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
}

Expand Down
31 changes: 15 additions & 16 deletions lib/modules/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);

const logs = {};

type FirestackOptions = {};
export class Base extends EventEmitter {
constructor(firestack, options = {}) {
constructor(firestack: Object, options: FirestackOptions = {}) {
super();
this.firestack = firestack;
this.eventHandlers = {};
Expand All @@ -22,7 +23,7 @@ export class Base extends EventEmitter {
}

// Logger
get log() {
get log(): Log {
if (!logs[this.namespace]) {
const debug = this.firestack._debug;
logs[this.namespace] = new Log(this.namespace, debug);
Expand All @@ -38,7 +39,7 @@ export class Base extends EventEmitter {
}

// TODO unused - do we need this anymore?
_addToFirestackInstance(...methods) {
_addToFirestackInstance(...methods: Array<string>) {
methods.forEach(name => {
this.firestack[name] = this[name].bind(this);
})
Expand All @@ -47,15 +48,17 @@ export class Base extends EventEmitter {
/**
* app instance
**/
get app() {
get app(): Object {
return this.firestack.app;
}

whenReady(fn) {
return this.firestack.configurePromise.then(fn);
whenReady(promise: Promise<*>): Promise<*> {
return this.firestack.configurePromise.then((result) => {
return promise;
});
}

get namespace() {
get namespace(): string {
return 'firestack:base';
}

Expand Down Expand Up @@ -88,25 +91,21 @@ export class Base extends EventEmitter {
}

export class ReferenceBase extends Base {
constructor(firestack, path) {
constructor(firestack: Object, path: Array<string> | string) {
super(firestack);

this.path = Array.isArray(path) ?
path :
(typeof path == 'string' ?
[path] : []);
this.path = Array.isArray(path) ? path : (typeof path == 'string' ? [path] : []);

// sanitize path, just in case
this.path = this.path
.filter(str => str !== "");
this.path = this.path.filter(str => str !== '');
}

get key() {
get key(): string {
const path = this.path;
return path.length === 0 ? '/' : path[path.length - 1];
}

pathToString() {
pathToString(): string {
let path = this.path;
let pathStr = (path.length > 0 ? path.join('/') : '/');
if (pathStr[0] != '/') {
Expand Down
61 changes: 37 additions & 24 deletions lib/modules/storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */

import {NativeModules, NativeEventEmitter} from 'react-native';
const FirestackStorage = NativeModules.FirestackStorage;
Expand All @@ -13,17 +14,22 @@ class StorageRef extends ReferenceBase {
this.storage = storage;
}

downloadUrl() {
downloadUrl(): Promise<Object> {
const path = this.pathToString();
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path);
this.log.debug('downloadUrl(', path, ')');
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path)
.catch(err => {
this.log.error('Error downloading URL for ', path, '. Error: ', err);
throw err;
});
}

/**
* Downloads a reference to the device
* @param {String} downloadPath Where to store the file
* @return {Promise}
*/
download (downloadPath, cb) {
download (downloadPath: string, cb: (evt: Object) => Object): Promise<Object> {
let callback = cb;
if (!callback || typeof callback !== 'function') {
callback = (evt) => {};
Expand All @@ -35,20 +41,25 @@ class StorageRef extends ReferenceBase {
listeners.push(this.storage._addListener('download_resumed', callback));

const path = this.pathToString();
this.log.debug('download(', path, ') -> ', downloadPath);
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
.then((res) => {
console.log('res --->', res);
listeners.forEach(this.storage._removeListener);
this.log.debug('res --->', res);
listeners.forEach(listener => listener.remove());
return res;
})
.catch(err => {
console.log('Got an error ->', err);
})
this.log.error('Error downloading ', path, ' to ', downloadPath, '. Error: ', err);
throw err;
});
}
}

type StorageOptionsType = {
storageBucket?: ?string,
};
export default class Storage extends Base {
constructor(firestack, options={}) {
constructor(firestack: Object, options:StorageOptionsType={}) {
super(firestack, options);

if (this.options.storageBucket) {
Expand All @@ -58,8 +69,8 @@ export default class Storage extends Base {
this.refs = {};
}

ref(...path) {
const key = this._pathKey(path);
ref(...path: Array<string>): StorageRef {
const key = this._pathKey(...path);
if (!this.refs[key]) {
const ref = new StorageRef(this, path);
this.refs[key] = ref;
Expand All @@ -74,43 +85,45 @@ export default class Storage extends Base {
* @param {object} metadata An object containing metadata
* @return {Promise}
*/
uploadFile(name, filepath, metadata={}, cb) {
uploadFile(name: string, filepath: string, metadata: Object={}, cb: (evt: Object) => Object): Promise<Object> {
this.log.debug('uploadFile(', filepath, ') -> ', name);
let callback = cb;
if (!callback || typeof callback !== 'function') {
callback = (evt) => {}
callback = (evt: Object) => ({});
}

filepath = filepath.replace("file://", "");
filepath = filepath.replace('file://', '');

const listeners = [];
listeners.push(this._addListener('upload_progress', callback));
listeners.push(this._addListener('upload_paused', callback));
listeners.push(this._addListener('upload_resumed', callback));
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, filepath, metadata)
.then((res) => {
listeners.forEach(this._removeListener);
listeners.forEach(listener => listener.remove());
return res;
})
.catch(err => {
this.log.error('Error uploading file ', name, ' to ', filepath, '. Error: ', err);
throw err;
});
}

getRealPathFromURI(uri) {
getRealPathFromURI(uri: string): Promise<string> {
return promisify('getRealPathFromURI', FirestackStorage)(uri);
}

_addListener(evt, cb) {
return FirestackStorageEvt.addListener(evt, cb);
}

_removeListener(evt) {
return FirestackStorageEvt.removeListener(evt);
_addListener(evt: string, cb: (evt: Object) => Object): {remove: () => void} {
let listener = FirestackStorageEvt.addListener(evt, cb);
return listener;
}

setStorageUrl(url) {
setStorageUrl(url: string): void {
// return promisify('setStorageUrl', FirestackStorage)(url);
this.storageUrl = `gs://${url}`;
}

_pathKey(...path) {
_pathKey(...path: Array<string>): string {
return path.join('-');
}

Expand All @@ -126,7 +139,7 @@ export default class Storage extends Base {
'FILETYPE_DIRECTORY': FirestackStorage.FILETYPE_DIRECTORY
};

get namespace() {
get namespace(): string {
return 'firestack:storage'
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/window-or-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// https://github.com/purposeindustries/window-or-global
module.exports = (typeof self === 'object' && self.self === self && self) ||
(typeof global === 'object' && global.global === global && global) ||
{}
this