2018-07-17 19:10:27 +00:00
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
|
|
|
|
|
|
|
import reduxStore from '../createStore';
|
|
|
|
import database from '../realm';
|
2019-02-25 16:23:17 +00:00
|
|
|
import log from '../../utils/log';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
|
|
|
const promises = {};
|
|
|
|
|
|
|
|
function _ufsCreate(fileInfo) {
|
2019-02-07 15:48:10 +00:00
|
|
|
return this.sdk.methodCall('ufsCreate', fileInfo);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _ufsComplete(fileId, store, token) {
|
2019-02-07 15:48:10 +00:00
|
|
|
return this.sdk.methodCall('ufsComplete', fileId, store, token);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _sendFileMessage(rid, data, msg = {}) {
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.22.0
|
2019-02-07 15:48:10 +00:00
|
|
|
return this.sdk.methodCall('sendFileMessage', rid, null, data, msg);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isUploadActive(path) {
|
|
|
|
return !!promises[path];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function cancelUpload(path) {
|
|
|
|
if (promises[path]) {
|
|
|
|
await promises[path].cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendFileMessage(rid, fileInfo) {
|
|
|
|
try {
|
|
|
|
const data = await RNFetchBlob.wrap(fileInfo.path);
|
|
|
|
if (!fileInfo.size) {
|
|
|
|
const fileStat = await RNFetchBlob.fs.stat(fileInfo.path);
|
|
|
|
fileInfo.size = fileStat.size;
|
|
|
|
fileInfo.name = fileStat.filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { FileUpload_MaxFileSize } = reduxStore.getState().settings;
|
|
|
|
|
|
|
|
// -1 maxFileSize means there is no limit
|
|
|
|
if (FileUpload_MaxFileSize > -1 && fileInfo.size > FileUpload_MaxFileSize) {
|
|
|
|
return Promise.reject({ error: 'error-file-too-large' }); // eslint-disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfo.rid = rid;
|
|
|
|
|
|
|
|
database.write(() => {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
} catch (e) {
|
2019-02-26 12:47:06 +00:00
|
|
|
return log('sendFileMessage -> create uploads 1', e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const result = await _ufsCreate.call(this, fileInfo);
|
|
|
|
|
|
|
|
promises[fileInfo.path] = RNFetchBlob.fetch('POST', result.url, {
|
|
|
|
'Content-Type': 'octet-stream'
|
|
|
|
}, data);
|
|
|
|
// Workaround for https://github.com/joltup/rn-fetch-blob/issues/96
|
|
|
|
setTimeout(() => {
|
2019-02-26 12:47:06 +00:00
|
|
|
if (promises[fileInfo.path] && promises[fileInfo.path].uploadProgress) {
|
|
|
|
promises[fileInfo.path].uploadProgress((loaded, total) => {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.progress = Math.floor((loaded / total) * 100);
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
} catch (e) {
|
|
|
|
return log('sendFileMessage -> create uploads 2', e);
|
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
2019-02-26 12:47:06 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
|
|
|
await promises[fileInfo.path];
|
|
|
|
|
|
|
|
const completeResult = await _ufsComplete.call(this, result.fileId, fileInfo.store, result.token);
|
|
|
|
|
|
|
|
await _sendFileMessage.call(this, completeResult.rid, {
|
|
|
|
_id: completeResult._id,
|
|
|
|
type: completeResult.type,
|
|
|
|
size: completeResult.size,
|
|
|
|
name: completeResult.name,
|
|
|
|
description: completeResult.description,
|
|
|
|
url: completeResult.path
|
|
|
|
});
|
|
|
|
|
|
|
|
database.write(() => {
|
|
|
|
const upload = database.objects('uploads').filtered('path = $0', fileInfo.path);
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.delete(upload);
|
|
|
|
} catch (e) {
|
|
|
|
log('sendFileMessage -> delete uploads', e);
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
} catch (err) {
|
|
|
|
log('sendFileMessage -> create uploads 3', err);
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|