2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
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;
|
2019-09-25 21:31:53 +00:00
|
|
|
await db.action(async() => {
|
2019-09-16 20:26:32 +00:00
|
|
|
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-07-29 16:33:28 +00:00
|
|
|
const { id, token } = user;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2020-02-11 14:09:14 +00:00
|
|
|
const uploadUrl = `${ server }/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);
|
2020-02-20 12:58:13 +00:00
|
|
|
const { customHeaders } = RocketChatSettings;
|
|
|
|
Object.keys(customHeaders).forEach((key) => {
|
|
|
|
xhr.setRequestHeader(key, customHeaders[key]);
|
|
|
|
});
|
2019-06-28 19:07:20 +00:00
|
|
|
|
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);
|
|
|
|
}
|
2019-09-26 16:53:04 +00:00
|
|
|
try {
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
reject(response);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|