2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
|
|
|
import database from '../database';
|
2019-02-25 16:23:17 +00:00
|
|
|
import log from '../../utils/log';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
const uploadQueue = {};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
export function isUploadActive(path) {
|
|
|
|
return !!uploadQueue[path];
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export async function cancelUpload(item) {
|
|
|
|
if (uploadQueue[item.path]) {
|
|
|
|
uploadQueue[item.path].abort();
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
await db.database.action(async() => {
|
|
|
|
await item.destroyPermanently();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
delete uploadQueue[item.path];
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
2019-09-16 20:26:32 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
2019-06-28 19:07:20 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
const serverInfo = await serversCollection.find(server);
|
|
|
|
const { FileUpload_MaxFileSize, id: Site_Url } = serverInfo;
|
2019-07-29 16:33:28 +00:00
|
|
|
const { id, token } = user;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +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-06-28 19:07:20 +00:00
|
|
|
const uploadUrl = `${ Site_Url }/api/v1/rooms.upload/${ rid }`;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const formData = new FormData();
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
fileInfo.rid = rid;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
|
|
|
const uploadsCollection = db.collections.get('uploads');
|
|
|
|
let uploadRecord;
|
|
|
|
try {
|
|
|
|
uploadRecord = await uploadsCollection.find(fileInfo.path);
|
|
|
|
} catch (error) {
|
2019-06-28 19:07:20 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
uploadRecord = await uploadsCollection.create((u) => {
|
|
|
|
u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema);
|
|
|
|
Object.assign(u, fileInfo);
|
|
|
|
u.subscription.id = rid;
|
|
|
|
});
|
|
|
|
});
|
2019-06-28 19:07:20 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
return log(e);
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
uploadQueue[fileInfo.path] = xhr;
|
|
|
|
xhr.open('POST', uploadUrl);
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +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-06-28 19:07:20 +00:00
|
|
|
xhr.setRequestHeader('X-Auth-Token', token);
|
|
|
|
xhr.setRequestHeader('X-User-Id', id);
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
xhr.upload.onprogress = async({ total, loaded }) => {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.update((u) => {
|
|
|
|
u.progress = Math.floor((loaded / total) * 100);
|
|
|
|
});
|
2019-02-26 12:47:06 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
xhr.onload = async() => {
|
|
|
|
if (xhr.status >= 200 && xhr.status < 400) { // If response is all good...
|
2019-06-28 19:07:20 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.destroyPermanently();
|
|
|
|
});
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
resolve(response);
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.update((u) => {
|
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = async(error) => {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.update((u) => {
|
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
reject(error);
|
2019-06-28 19:07:20 +00:00
|
|
|
};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-06-28 19:07:20 +00:00
|
|
|
xhr.send(formData);
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|