2018-07-17 19:10:27 +00:00
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
|
|
|
|
|
|
|
import reduxStore from '../createStore';
|
|
|
|
import database from '../realm';
|
|
|
|
|
|
|
|
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(() => {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
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(() => {
|
|
|
|
promises[fileInfo.path].uploadProgress((loaded, total) => {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.progress = Math.floor((loaded / total) * 100);
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
database.delete(upload);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|