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 isEmpty from 'lodash/isEmpty';
|
2022-12-05 16:31:44 +00:00
|
|
|
import { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob';
|
|
|
|
import { Alert } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import { IUpload, IUser, TUploadModel } from '../../definitions';
|
2022-12-05 16:31:44 +00:00
|
|
|
import i18n from '../../i18n';
|
|
|
|
import database from '../database';
|
|
|
|
import FileUpload from './helpers/fileUpload';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { IFileUpload } from './helpers/fileUpload/interfaces';
|
2022-12-05 16:31:44 +00:00
|
|
|
import log from './helpers/log';
|
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-12-05 16:31:44 +00:00
|
|
|
const getUploadPath = (path: string, rid: string) => `${path}-${rid}`;
|
|
|
|
|
|
|
|
export function isUploadActive(path: string, rid: string): boolean {
|
|
|
|
return !!uploadQueue[getUploadPath(path, rid)];
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 16:31:44 +00:00
|
|
|
export async function cancelUpload(item: TUploadModel, rid: string): Promise<void> {
|
|
|
|
const uploadPath = getUploadPath(item.path, rid);
|
|
|
|
if (!isEmpty(uploadQueue[uploadPath])) {
|
2020-12-01 20:19:48 +00:00
|
|
|
try {
|
2022-12-05 16:31:44 +00:00
|
|
|
await uploadQueue[uploadPath].cancel();
|
2020-12-01 20:19:48 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2022-12-05 16:31:44 +00:00
|
|
|
delete uploadQueue[uploadPath];
|
|
|
|
}
|
|
|
|
if (item.id) {
|
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);
|
|
|
|
}
|
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-12-21 17:34:48 +00:00
|
|
|
user: Partial<Pick<IUser, 'id' | 'token'>>,
|
|
|
|
isForceTryAgain?: boolean
|
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-12-05 16:31:44 +00:00
|
|
|
const uploadPath = getUploadPath(fileInfo.path, rid);
|
2022-02-21 16:06:57 +00:00
|
|
|
let uploadRecord: TUploadModel;
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2022-12-05 16:31:44 +00:00
|
|
|
uploadRecord = await uploadsCollection.find(uploadPath);
|
2022-12-21 17:34:48 +00:00
|
|
|
if (uploadRecord.id && !isForceTryAgain) {
|
2022-12-05 16:31:44 +00:00
|
|
|
return Alert.alert(i18n.t('FileUpload_Error'), i18n.t('Upload_in_progress'));
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
} 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 => {
|
2022-12-05 16:31:44 +00:00
|
|
|
u._raw = sanitizedRaw({ id: uploadPath }, uploadsCollection.schema);
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(u, fileInfo);
|
2022-12-21 17:34:48 +00:00
|
|
|
if (tmid) {
|
|
|
|
u.tmid = tmid;
|
|
|
|
}
|
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
|
|
|
|
};
|
|
|
|
|
2022-12-05 16:31:44 +00:00
|
|
|
uploadQueue[uploadPath] = FileUpload.fetch('POST', uploadUrl, headers, formData);
|
2019-06-28 19:07:20 +00:00
|
|
|
|
2022-12-05 16:31:44 +00:00
|
|
|
uploadQueue[uploadPath].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
|
|
|
|
2022-12-05 16:31:44 +00:00
|
|
|
uploadQueue[uploadPath].then(async response => {
|
2021-09-13 20:41:05 +00:00
|
|
|
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
|
|
|
|
2022-12-05 16:31:44 +00:00
|
|
|
uploadQueue[uploadPath].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
|
|
|
}
|