Update upload to use fetch

e9178e52eb5b138235aaf9ee7ada9e8017196402

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +28 -19Showing whitespace changes
public/scripts/backgrounds.js+28 -19
@@ -217,7 +217,7 @@ async function onCopyToSystemBackgroundClick(e) {
217 const formData = new FormData();217 const formData = new FormData();
218 formData.set('avatar', file);218 formData.set('avatar', file);
219219
220 uploadBackground(formData);220 await uploadBackground(formData);
221221
222 const list = chat_metadata[LIST_METADATA_KEY] || [];222 const list = chat_metadata[LIST_METADATA_KEY] || [];
223 const index = list.indexOf(bgNames.oldBg);223 const index = list.indexOf(bgNames.oldBg);
@@ -448,7 +448,7 @@ async function onBackgroundUploadSelected() {
448448
449 const formData = new FormData(form);449 const formData = new FormData(form);
450 await convertFileIfVideo(formData);450 await convertFileIfVideo(formData);
451 uploadBackground(formData);451 await uploadBackground(formData);
452 form.reset();452 form.reset();
453}453}
454454
@@ -484,6 +484,7 @@ async function convertFileIfVideo(formData) {
484 formData.set('avatar', convertedFile);484 formData.set('avatar', convertedFile);
485 toastMessage.remove();485 toastMessage.remove();
486 } catch (error) {486 } catch (error) {
487 formData.delete('avatar');
487 toastMessage.remove();488 toastMessage.remove();
488 console.error('Error converting video to animated webp:', error);489 console.error('Error converting video to animated webp:', error);
489 toastr.error('Error converting video to animated webp');490 toastr.error('Error converting video to animated webp');
@@ -494,26 +495,34 @@ async function convertFileIfVideo(formData) {
494 * Uploads a background to the server495 * Uploads a background to the server
495 * @param {FormData} formData496 * @param {FormData} formData
496 */497 */
497function uploadBackground(formData) {498async function uploadBackground(formData) {
498 jQuery.ajax({499 try {
499 type: 'POST',500 if (!formData.has('avatar')) {
500 url: '/api/backgrounds/upload',501 console.log('No file provided. Background upload cancelled.');
501 data: formData,502 return;
502 beforeSend: function () {503 }
503 },504
504 cache: false,505 const headers = getRequestHeaders();
505 contentType: false,506 delete headers['Content-Type'];
506 processData: false,507
507 success: async function (bg) {508 const response = await fetch('/api/backgrounds/upload', {
509 method: 'POST',
510 headers: getRequestHeaders(),
511 body: formData,
512 cache: 'no-cache',
513 });
514
515 if (!response.ok) {
516 throw new Error('Failed to upload background');
517 }
518
519 const bg = await response.text();
508 setBackground(bg, generateUrlParameter(bg, false));520 setBackground(bg, generateUrlParameter(bg, false));
509 await getBackgrounds();521 await getBackgrounds();
510 highlightNewBackground(bg);522 highlightNewBackground(bg);
511 },523 } catch (error) {
512 error: function (jqXHR, exception) {524 console.error('Error uploading background:', error);
513 console.log(exception);525 }
514 console.log(jqXHR);
515 },
516 });
517}526}
518527
519/**528/**