2018-07-17 19:10:27 +00:00
|
|
|
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
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
const uploadQueue = {};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
export function isUploadActive(path) {
|
|
|
|
return !!uploadQueue[path];
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
export function cancelUpload(path) {
|
|
|
|
if (uploadQueue[path]) {
|
|
|
|
uploadQueue[path].abort();
|
|
|
|
database.write(() => {
|
|
|
|
const upload = database.objects('uploads').filtered('path = $0', path);
|
|
|
|
try {
|
|
|
|
database.delete(upload);
|
|
|
|
} catch (e) {
|
|
|
|
log('err_send_file_message_delete_upload', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
delete uploadQueue[path];
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
export function sendFileMessage(rid, fileInfo, tmid) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const { FileUpload_MaxFileSize, Site_Url } = reduxStore.getState().settings;
|
|
|
|
const { id, token } = reduxStore.getState().login.user;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
// -1 maxFileSize means there is no limit
|
|
|
|
if (FileUpload_MaxFileSize > -1 && fileInfo.size > FileUpload_MaxFileSize) {
|
|
|
|
return reject({ error: 'error-file-too-large' }); // eslint-disable-line
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
const uploadUrl = `${ Site_Url }/api/v1/rooms.upload/${ rid }`;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const formData = new FormData();
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
fileInfo.rid = rid;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
database.write(() => {
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
} catch (e) {
|
|
|
|
return log('err_send_file_message_create_upload_1', e);
|
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
uploadQueue[fileInfo.path] = xhr;
|
|
|
|
xhr.open('POST', uploadUrl);
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
formData.append('file', {
|
|
|
|
uri: fileInfo.path,
|
|
|
|
type: fileInfo.type,
|
|
|
|
name: fileInfo.name || 'fileMessage'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fileInfo.description) {
|
|
|
|
formData.append('description', fileInfo.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmid) {
|
|
|
|
formData.append('tmid', tmid);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
xhr.setRequestHeader('X-Auth-Token', token);
|
|
|
|
xhr.setRequestHeader('X-User-Id', id);
|
|
|
|
|
|
|
|
xhr.upload.onprogress = ({ total, loaded }) => {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.progress = Math.floor((loaded / total) * 100);
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
} catch (e) {
|
|
|
|
return log('err_send_file_message_create_upload_2', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status >= 200 && xhr.status < 400) { // If response is all good...
|
2019-02-26 12:47:06 +00:00
|
|
|
database.write(() => {
|
2019-07-15 17:24:48 +00:00
|
|
|
const upload = database.objects('uploads').filtered('path = $0', fileInfo.path);
|
2019-02-26 12:47:06 +00:00
|
|
|
try {
|
2019-07-15 17:24:48 +00:00
|
|
|
database.delete(upload);
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
resolve(response);
|
2019-02-26 12:47:06 +00:00
|
|
|
} catch (e) {
|
2019-07-15 17:24:48 +00:00
|
|
|
reject(e);
|
|
|
|
log('err_send_file_message_delete_upload', e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
reject(response);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
log('err_send_file_message_create_upload_3', err);
|
2019-02-26 12:47:06 +00:00
|
|
|
}
|
|
|
|
});
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = (e) => {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
reject(e);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
log('err_send_file_message_create_upload_3', err);
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
2019-07-15 17:24:48 +00:00
|
|
|
};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-07-15 17:24:48 +00:00
|
|
|
xhr.send(formData);
|
|
|
|
} catch (err) {
|
|
|
|
log('err_send_file_message_create_upload_4', err);
|
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|