2019-10-07 20:56:30 +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';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { headers } from '../../utils/fetch';
|
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-10-07 20:56:30 +00:00
|
|
|
export async function cancelUpload(item) {
|
|
|
|
if (uploadQueue[item.path]) {
|
|
|
|
uploadQueue[item.path].abort();
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
await db.action(async() => {
|
|
|
|
await item.destroyPermanently();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
delete uploadQueue[item.path];
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
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-10-07 20:56:30 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
2019-07-15 17:24:48 +00:00
|
|
|
try {
|
2019-07-29 20:44:39 +00:00
|
|
|
const { id, token } = user;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2020-02-19 19:43:47 +00:00
|
|
|
const uploadUrl = `${ server }/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-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
|
|
|
const uploadsCollection = db.collections.get('uploads');
|
|
|
|
let uploadRecord;
|
|
|
|
try {
|
|
|
|
uploadRecord = await uploadsCollection.find(fileInfo.path);
|
|
|
|
} catch (error) {
|
2019-07-15 17:24:48 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +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-07-15 17:24:48 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
return log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
2019-10-07 20:56:30 +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);
|
2019-12-11 23:01:12 +00:00
|
|
|
xhr.setRequestHeader('User-Agent', headers['User-Agent']);
|
2019-07-15 17:24:48 +00:00
|
|
|
|
2019-10-07 20:56:30 +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-10-07 20:56:30 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
xhr.onload = async() => {
|
|
|
|
if (xhr.status >= 200 && xhr.status < 400) { // If response is all good...
|
2019-07-15 17:24:48 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.destroyPermanently();
|
|
|
|
});
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
resolve(response);
|
2019-09-03 19:27:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-07-15 17:24:48 +00:00
|
|
|
}
|
2019-10-07 20:56:30 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.update((u) => {
|
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
reject(response);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = async(error) => {
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await uploadRecord.update((u) => {
|
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
reject(error);
|
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
|
|
|
}
|