feat(web-media): improve upload/send reliability
Some checks failed
CI / test (push) Failing after 17s
Some checks failed
CI / test (push) Failing after 17s
- add retry logic for presigned media upload - add retry for text send with idempotent client message id - avoid dropping already sent media message when attachment metadata call fails
This commit is contained in:
@@ -104,17 +104,32 @@ export async function uploadToPresignedUrl(
|
||||
uploadUrl: string,
|
||||
requiredHeaders: Record<string, string>,
|
||||
file: File,
|
||||
onProgress?: (percent: number) => void
|
||||
onProgress?: (percent: number) => void,
|
||||
retries = 2
|
||||
): Promise<void> {
|
||||
await axios.put(uploadUrl, file, {
|
||||
headers: requiredHeaders,
|
||||
onUploadProgress: (progressEvent) => {
|
||||
if (!onProgress || !progressEvent.total) {
|
||||
return;
|
||||
let attempt = 0;
|
||||
while (attempt <= retries) {
|
||||
try {
|
||||
await axios.put(uploadUrl, file, {
|
||||
headers: requiredHeaders,
|
||||
onUploadProgress: (progressEvent) => {
|
||||
if (!onProgress || !progressEvent.total) {
|
||||
return;
|
||||
}
|
||||
onProgress(Math.round((progressEvent.loaded * 100) / progressEvent.total));
|
||||
}
|
||||
});
|
||||
return;
|
||||
} catch (error) {
|
||||
const status = axios.isAxiosError(error) ? error.response?.status : undefined;
|
||||
const retryable = status === undefined || status >= 500;
|
||||
if (!retryable || attempt >= retries) {
|
||||
throw error;
|
||||
}
|
||||
onProgress(Math.round((progressEvent.loaded * 100) / progressEvent.total));
|
||||
attempt += 1;
|
||||
await new Promise((resolve) => window.setTimeout(resolve, 400 * attempt));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function attachFile(messageId: number, fileUrl: string, fileType: string, fileSize: number): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user