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';
|
2022-02-21 16:06:57 +00:00
|
|
|
import { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob';
|
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-12-01 20:19:48 +00:00
|
|
|
import FileUpload from '../../utils/fileUpload';
|
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';
|
2022-02-21 16:06:57 +00:00
|
|
|
import { IUpload, IUser, TUploadModel } from '../../definitions';
|
|
|
|
import { IFileUpload } from '../../utils/fileUpload/interfaces';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
const uploadQueue: { [index: string]: StatefulPromise<FetchBlobResponse> } = {};
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
export function isUploadActive(path: string): boolean {
|
2019-06-28 19:07:20 +00:00
|
|
|
return !!uploadQueue[path];
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
export async function cancelUpload(item: TUploadModel): Promise<void> {
|
|
|
|
if (!isEmpty(uploadQueue[item.path])) {
|
2020-12-01 20:19:48 +00:00
|
|
|
try {
|
|
|
|
await uploadQueue[item.path].cancel();
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(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
|
|
|
}
|
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
export function sendFileMessage(
|
|
|
|
rid: string,
|
|
|
|
fileInfo: IUpload,
|
2022-03-02 14:18:01 +00:00
|
|
|
tmid: string | undefined,
|
2022-02-21 16:06:57 +00:00
|
|
|
server: string,
|
2022-03-02 14:18:01 +00:00
|
|
|
user: Partial<Pick<IUser, 'id' | 'token'>>
|
2022-02-21 16:06:57 +00:00
|
|
|
): Promise<FetchBlobResponse | void> {
|
2021-09-13 20:41:05 +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
|
|
|
|
2021-09-13 20:41:05 +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
|
|
|
fileInfo.rid = rid;
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const uploadsCollection = db.get('uploads');
|
2022-02-21 16:06:57 +00:00
|
|
|
let uploadRecord: TUploadModel;
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
uploadRecord = await uploadsCollection.find(fileInfo.path);
|
|
|
|
} catch (error) {
|
2019-06-28 19:07:20 +00:00
|
|
|
try {
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
uploadRecord = await uploadsCollection.create(u => {
|
2019-09-16 20:26:32 +00:00
|
|
|
u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema);
|
|
|
|
Object.assign(u, fileInfo);
|
2022-02-21 16:06:57 +00:00
|
|
|
if (u.subscription) {
|
|
|
|
u.subscription.id = rid;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
});
|
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
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
const formData: IFileUpload[] = [];
|
2020-12-01 20:19:48 +00:00
|
|
|
formData.push({
|
|
|
|
name: 'file',
|
2019-06-28 19:07:20 +00:00
|
|
|
type: fileInfo.type,
|
2020-12-01 20:19:48 +00:00
|
|
|
filename: fileInfo.name || 'fileMessage',
|
|
|
|
uri: fileInfo.path
|
2019-06-28 19:07:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (fileInfo.description) {
|
2020-12-01 20:19:48 +00:00
|
|
|
formData.push({
|
|
|
|
name: 'description',
|
|
|
|
data: fileInfo.description
|
|
|
|
});
|
2019-06-28 19:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tmid) {
|
2020-12-01 20:19:48 +00:00
|
|
|
formData.push({
|
|
|
|
name: 'tmid',
|
|
|
|
data: tmid
|
|
|
|
});
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2020-12-01 20:19:48 +00:00
|
|
|
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);
|
2019-06-28 19:07:20 +00:00
|
|
|
|
2022-02-21 16:06:57 +00:00
|
|
|
uploadQueue[fileInfo.path].uploadProgress(async (loaded: number, total: number) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await uploadRecord.update(u => {
|
2019-09-16 20:26:32 +00:00
|
|
|
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
|
|
|
}
|
2020-12-01 20:19:48 +00:00
|
|
|
});
|
2019-06-28 19:07:20 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
uploadQueue[fileInfo.path].then(async response => {
|
|
|
|
if (response.respInfo.status >= 200 && response.respInfo.status < 400) {
|
|
|
|
// If response is all good...
|
2019-06-28 19:07:20 +00:00
|
|
|
try {
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
await uploadRecord.destroyPermanently();
|
|
|
|
});
|
|
|
|
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 {
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await uploadRecord.update(u => {
|
2019-09-16 20:26:32 +00:00
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-09-26 16:53:04 +00:00
|
|
|
try {
|
|
|
|
reject(response);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2020-12-01 20:19:48 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
uploadQueue[fileInfo.path].catch(async error => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2022-02-21 16:06:57 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await uploadRecord.update(u => {
|
2019-09-16 20:26:32 +00:00
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
reject(error);
|
2020-12-01 20:19:48 +00:00
|
|
|
});
|
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
|
|
|
}
|