Still fixing progress

This commit is contained in:
Diego Mello 2024-05-17 16:28:16 -03:00
parent 1e5b1c78e7
commit 2543aa2f9c
4 changed files with 56 additions and 40 deletions

View File

@ -20,4 +20,7 @@ export interface IUpload {
e2e?: E2EType; e2e?: E2EType;
} }
export type TUploadModel = IUpload & Model; export type TUploadModel = IUpload &
Model & {
asPlain: () => IUpload;
};

View File

@ -29,4 +29,18 @@ export default class Upload extends Model {
@field('progress') progress; @field('progress') progress;
@field('error') error; @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
};
}
} }

View File

@ -93,10 +93,11 @@ export function sendFileMessage(
): Promise<FetchBlobResponse | void> { ): Promise<FetchBlobResponse | void> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
console.log('sendFileMessage', rid, fileInfo);
const { id, token } = user; const { id, token } = user;
fileInfo.rid = rid;
fileInfo.path = fileInfo.path.startsWith('file://') ? fileInfo.path.substring(7) : fileInfo.path; fileInfo.path = fileInfo.path.startsWith('file://') ? fileInfo.path.substring(7) : fileInfo.path;
const [uploadPath, uploadRecord] = await createUploadRecord({ rid, fileInfo, tmid, isForceTryAgain }); const [uploadPath, uploadRecord] = await createUploadRecord({ rid, fileInfo, tmid, isForceTryAgain });
console.log('🚀 ~ returnnewPromise ~ uploadPath, uploadRecord:', uploadPath, uploadRecord);
if (!uploadPath || !uploadRecord) { if (!uploadPath || !uploadRecord) {
return; return;
} }
@ -120,17 +121,23 @@ export function sendFileMessage(
} }
]; ];
uploadQueue[uploadPath] = RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data); console.log('🚀 ~ returnnewPromise ~ data:', data, rid, fileInfo);
// @ts-ignore
uploadQueue[uploadPath].then(async response => { uploadQueue[uploadPath] = RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data)
// If response is all good... .uploadProgress(async (loaded: number, total: number) => {
if (response.respInfo.status >= 200 && response.respInfo.status < 400) { await db.write(async () => {
await uploadRecord.update(u => {
u.progress = Math.floor((loaded / total) * 100);
});
});
})
.then(async response => {
const json = response.json(); const json = response.json();
let content; let content;
if (getContent) { if (getContent) {
content = await getContent(json.file._id, json.file.url); 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', method: 'POST',
headers: { headers: {
...headers, ...headers,
@ -138,46 +145,28 @@ export function sendFileMessage(
}, },
body: JSON.stringify({ body: JSON.stringify({
// msg: '', TODO: backwards compatibility // msg: '', TODO: backwards compatibility
tmid, tmid: tmid ?? undefined,
description: fileInfo.description, description: fileInfo.description,
t: 'e2e', t: 'e2e',
content content
}) })
}); }).then(async () => {
console.log('destroy destroy destroy destroy ');
await db.write(async () => { await db.write(async () => {
await uploadRecord.destroyPermanently(); await uploadRecord.destroyPermanently();
}); });
});
resolve(response); resolve(response);
} else { })
.catch(async error => {
console.log('catch catch catch catch catch ');
await db.write(async () => { await db.write(async () => {
await uploadRecord.update(u => { await uploadRecord.update(u => {
u.error = true; 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; 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);
});
});
});
} catch (e) { } catch (e) {
reject(e); reject(e);
} }

View File

@ -160,7 +160,7 @@ class UploadProgress extends Component<IUploadProgressProps, IUploadProgressStat
item.error = false; item.error = false;
}); });
}); });
await sendFileMessage(rid, item, item.tmid, server, user, true); await sendFileMessage(rid, item.asPlain(), item.tmid, server, user, true);
} catch (e) { } catch (e) {
log(e); log(e);
} }
@ -179,11 +179,19 @@ class UploadProgress extends Component<IUploadProgressProps, IUploadProgressStat
> >
{I18n.t('Uploading')} {item.name} {I18n.t('Uploading')} {item.name}
</Text> </Text>
<CustomIcon name='close' size={20} color={themes[theme!].fontSecondaryInfo} onPress={() => this.handleCancelUpload(item)} /> <CustomIcon
name='close'
size={20}
color={themes[theme!].fontSecondaryInfo}
onPress={() => this.handleCancelUpload(item)}
/>
</View>, </View>,
<View <View
key='progress' key='progress'
style={[styles.progress, { width: (width * (item.progress ?? 0)) / 100, backgroundColor: themes[theme!].badgeBackgroundLevel2 }]} style={[
styles.progress,
{ width: (width * (item.progress ?? 0)) / 100, backgroundColor: themes[theme!].badgeBackgroundLevel2 }
]}
/> />
]; ];
} }
@ -195,7 +203,9 @@ class UploadProgress extends Component<IUploadProgressProps, IUploadProgressStat
{I18n.t('Error_uploading')} {item.name} {I18n.t('Error_uploading')} {item.name}
</Text> </Text>
<TouchableOpacity onPress={() => this.tryAgain(item)}> <TouchableOpacity onPress={() => this.tryAgain(item)}>
<Text style={[styles.tryAgainButtonText, { color: themes[theme!].badgeBackgroundLevel2 }]}>{I18n.t('Try_again')}</Text> <Text style={[styles.tryAgainButtonText, { color: themes[theme!].badgeBackgroundLevel2 }]}>
{I18n.t('Try_again')}
</Text>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
<CustomIcon name='close' size={20} color={themes[theme!].fontSecondaryInfo} onPress={() => this.deleteUpload(item)} /> <CustomIcon name='close' size={20} color={themes[theme!].fontSecondaryInfo} onPress={() => this.deleteUpload(item)} />