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
app
definitions
lib
database/model
methods
views/RoomView

View File

@ -20,4 +20,7 @@ export interface IUpload {
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('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> {
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);
}

View File

@ -160,7 +160,7 @@ class UploadProgress extends Component<IUploadProgressProps, IUploadProgressStat
item.error = false;
});
});
await sendFileMessage(rid, item, item.tmid, server, user, true);
await sendFileMessage(rid, item.asPlain(), item.tmid, server, user, true);
} catch (e) {
log(e);
}
@ -179,11 +179,19 @@ class UploadProgress extends Component<IUploadProgressProps, IUploadProgressStat
>
{I18n.t('Uploading')} {item.name}
</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
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}
</Text>
<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>
</View>
<CustomIcon name='close' size={20} color={themes[theme!].fontSecondaryInfo} onPress={() => this.deleteUpload(item)} />