import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import FileUpload from '../../utils/fileUpload';
import database from '../database';
import log from '../../utils/log';
const uploadQueue = {};
export function isUploadActive(path) {
return !!uploadQueue[path];
}
export async function cancelUpload(item) {
if (uploadQueue[item.path]) {
try {
await uploadQueue[item.path].cancel();
} catch {
// Do nothing
const db = database.active;
await db.action(async() => {
await item.destroyPermanently();
});
} catch (e) {
log(e);
delete uploadQueue[item.path];
export function sendFileMessage(rid, fileInfo, tmid, server, user) {
return new Promise(async(resolve, reject) => {
const { id, token } = user;
const uploadUrl = `${ server }/api/v1/rooms.upload/${ rid }`;
fileInfo.rid = rid;
const uploadsCollection = db.collections.get('uploads');
let uploadRecord;
uploadRecord = await uploadsCollection.find(fileInfo.path);
} catch (error) {
uploadRecord = await uploadsCollection.create((u) => {
u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema);
Object.assign(u, fileInfo);
u.subscription.id = rid;
return log(e);
const formData = [];
formData.push({
name: 'file',
type: fileInfo.type,
filename: fileInfo.name || 'fileMessage',
uri: fileInfo.path
if (fileInfo.description) {
name: 'description',
data: fileInfo.description
if (tmid) {
name: 'tmid',
data: tmid
const headers = {
...RocketChatSettings.customHeaders,
'Content-Type': 'multipart/form-data',
'X-Auth-Token': token,
'X-User-Id': id
};
uploadQueue[fileInfo.path] = FileUpload.fetch('POST', uploadUrl, headers, formData);
uploadQueue[fileInfo.path].uploadProgress(async(loaded, total) => {
await uploadRecord.update((u) => {
u.progress = Math.floor((loaded / total) * 100);
uploadQueue[fileInfo.path].then(async(response) => {
if (response.respInfo.status >= 200 && response.respInfo.status < 400) { // If response is all good...
await uploadRecord.destroyPermanently();
resolve(response);
} else {
u.error = true;
reject(response);
reject(e);
uploadQueue[fileInfo.path].catch(async(error) => {
reject(error);