diff --git a/app/definitions/IUpload.ts b/app/definitions/IUpload.ts index 187e34881..9931122c2 100644 --- a/app/definitions/IUpload.ts +++ b/app/definitions/IUpload.ts @@ -20,4 +20,7 @@ export interface IUpload { e2e?: E2EType; } -export type TUploadModel = IUpload & Model; +export type TUploadModel = IUpload & + Model & { + asPlain: () => IUpload; + }; diff --git a/app/lib/database/model/Upload.js b/app/lib/database/model/Upload.js index a96e2c995..bfb91655e 100644 --- a/app/lib/database/model/Upload.js +++ b/app/lib/database/model/Upload.js @@ -29,4 +29,18 @@ export default class Upload extends Model { @field('progress') progress; @field('error') error; + + asPlain() { + return { + id: this.id, + rid: this.subscription.id, + path: this.path, + name: this.name, + tmid: this.tmid, + description: this.description, + size: this.size, + type: this.type, + store: this.store + }; + } } diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index f783c188f..1b8fa8731 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -93,10 +93,11 @@ export function sendFileMessage( ): Promise { return new Promise(async (resolve, reject) => { try { + console.log('sendFileMessage', rid, fileInfo); const { id, token } = user; - fileInfo.rid = rid; fileInfo.path = fileInfo.path.startsWith('file://') ? fileInfo.path.substring(7) : fileInfo.path; const [uploadPath, uploadRecord] = await createUploadRecord({ rid, fileInfo, tmid, isForceTryAgain }); + console.log('🚀 ~ returnnewPromise ~ uploadPath, uploadRecord:', uploadPath, uploadRecord); if (!uploadPath || !uploadRecord) { return; } @@ -120,17 +121,23 @@ export function sendFileMessage( } ]; - uploadQueue[uploadPath] = RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data); - - uploadQueue[uploadPath].then(async response => { - // If response is all good... - if (response.respInfo.status >= 200 && response.respInfo.status < 400) { + console.log('🚀 ~ returnnewPromise ~ data:', data, rid, fileInfo); + // @ts-ignore + uploadQueue[uploadPath] = RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data) + .uploadProgress(async (loaded: number, total: number) => { + await db.write(async () => { + await uploadRecord.update(u => { + u.progress = Math.floor((loaded / total) * 100); + }); + }); + }) + .then(async response => { const json = response.json(); let content; if (getContent) { content = await getContent(json.file._id, json.file.url); } - await fetch(`${server}/api/v1/rooms.mediaConfirm/${rid}/${json.file._id}`, { + fetch(`${server}/api/v1/rooms.mediaConfirm/${rid}/${json.file._id}`, { method: 'POST', headers: { ...headers, @@ -138,46 +145,28 @@ export function sendFileMessage( }, body: JSON.stringify({ // msg: '', TODO: backwards compatibility - tmid, + tmid: tmid ?? undefined, description: fileInfo.description, t: 'e2e', content }) - }); - - await db.write(async () => { - await uploadRecord.destroyPermanently(); + }).then(async () => { + console.log('destroy destroy destroy destroy '); + await db.write(async () => { + await uploadRecord.destroyPermanently(); + }); }); resolve(response); - } else { + }) + .catch(async error => { + console.log('catch catch catch catch catch '); await db.write(async () => { await uploadRecord.update(u => { u.error = true; }); }); - } - }); - - uploadQueue[uploadPath].catch(async error => { - try { - await db.write(async () => { - await uploadRecord.update(u => { - u.error = true; - }); - }); - } catch (e) { - log(e); - } - throw error; - }); - - uploadQueue[uploadPath].uploadProgress(async (loaded: number, total: number) => { - await db.write(async () => { - await uploadRecord.update(u => { - u.progress = Math.floor((loaded / total) * 100); - }); + throw error; }); - }); } catch (e) { reject(e); } diff --git a/app/views/RoomView/UploadProgress.tsx b/app/views/RoomView/UploadProgress.tsx index 47c6bdb89..36c117451 100644 --- a/app/views/RoomView/UploadProgress.tsx +++ b/app/views/RoomView/UploadProgress.tsx @@ -160,7 +160,7 @@ class UploadProgress extends Component {I18n.t('Uploading')} {item.name} - this.handleCancelUpload(item)} /> + this.handleCancelUpload(item)} + /> , ]; } @@ -195,7 +203,9 @@ class UploadProgress extends Component this.tryAgain(item)}> - {I18n.t('Try_again')} + + {I18n.t('Try_again')} + this.deleteUpload(item)} />