Skip to content

Commit 73cdb97

Browse files
committed
refactor: dedup V1 upload record creation via createUploadRecord
1 parent e1a66ff commit 73cdb97

1 file changed

Lines changed: 6 additions & 39 deletions

File tree

app/lib/methods/sendFileMessage/sendFileMessage.ts

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
21
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
3-
import { Alert } from 'react-native';
42

53
import { type IUser, type TSendFileMessageFileInfo, type TUploadModel } from '../../../definitions';
6-
import i18n from '../../../i18n';
74
import database from '../../database';
85
import FileUpload from '../helpers/fileUpload';
9-
import log from '../helpers/log';
10-
import { copyFileToCacheDirectoryIfNeeded, getUploadPath, isUploadActive, persistUploadError, uploadQueue } from './utils';
6+
import { copyFileToCacheDirectoryIfNeeded, createUploadRecord, persistUploadError, uploadQueue } from './utils';
117
import { type IFormData } from '../helpers/fileUpload/definitions';
128

139
export async function sendFileMessage(
@@ -19,46 +15,17 @@ export async function sendFileMessage(
1915
isForceTryAgain?: boolean
2016
): Promise<void> {
2117
let uploadPath: string | null = '';
18+
let uploadRecord: TUploadModel | null;
2219
try {
2320
const { id, token } = user;
2421
const uploadUrl = `${server}/api/v1/rooms.upload/${rid}`;
2522
fileInfo.rid = rid;
2623

2724
const db = database.active;
28-
const uploadsCollection = db.get('uploads');
29-
uploadPath = getUploadPath(fileInfo.path, rid);
30-
let uploadRecord: TUploadModel;
31-
try {
32-
uploadRecord = await uploadsCollection.find(uploadPath);
33-
if (uploadRecord.id) {
34-
if (isUploadActive(fileInfo.path, rid) && !isForceTryAgain) {
35-
return Alert.alert(i18n.t('FileUpload_Error'), i18n.t('Upload_in_progress'));
36-
}
37-
// Record left behind by a crashed or failed upload: reset and reuse it.
38-
await db.write(async () => {
39-
await uploadRecord?.update(u => {
40-
u.error = false;
41-
u.progress = 0;
42-
});
43-
});
44-
}
45-
} catch (error) {
46-
try {
47-
await db.write(async () => {
48-
uploadRecord = await uploadsCollection.create(u => {
49-
u._raw = sanitizedRaw({ id: uploadPath }, uploadsCollection.schema);
50-
Object.assign(u, fileInfo);
51-
if (tmid) {
52-
u.tmid = tmid;
53-
}
54-
if (u.subscription) {
55-
u.subscription.id = rid;
56-
}
57-
});
58-
});
59-
} catch (e) {
60-
return log(e);
61-
}
25+
26+
[uploadPath, uploadRecord] = await createUploadRecord({ rid, fileInfo, tmid, isForceTryAgain });
27+
if (!uploadPath || !uploadRecord) {
28+
return;
6229
}
6330

6431
fileInfo.path = await copyFileToCacheDirectoryIfNeeded(fileInfo.path, fileInfo.name);

0 commit comments

Comments
 (0)