2018-07-17 19:10:27 +00:00
|
|
|
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) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
delete uploadQueue[path];
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 20:44:39 +00:00
|
|
|
export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
2019-07-15 17:24:48 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2019-07-29 20:44:39 +00:00
|
|
|
const { serversDB } = database.databases;
|
|
|
|
const { FileUpload_MaxFileSize, id: Site_Url } = serversDB.objectForPrimaryKey('servers', server);
|
|
|
|
const { id, token } = 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) {
|
2019-09-03 19:27:57 +00:00
|
|
|
return log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
});
|
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) {
|
2019-09-03 19:27:57 +00:00
|
|
|
return log(e);
|
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.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);
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
reject(response);
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
log(e);
|
2019-02-26 12:47:06 +00:00
|
|
|
}
|
|
|
|
});
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-03 19:27:57 +00:00
|
|
|
xhr.onerror = (error) => {
|
2019-07-15 17:24:48 +00:00
|
|
|
database.write(() => {
|
|
|
|
fileInfo.error = true;
|
|
|
|
try {
|
|
|
|
database.create('uploads', fileInfo, true);
|
2019-09-03 19:27:57 +00:00
|
|
|
reject(error);
|
|
|
|
} catch (e) {
|
2019-07-15 17:24:48 +00:00
|
|
|
reject(e);
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
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);
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|