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) {
217217 const formData = new FormData();
218218 formData.set('avatar', file);
219219
220220 await uploadBackground(formData);
221221
222222 const list = chat_metadata[LIST_METADATA_KEY] || [];
223223 const index = list.indexOf(bgNames.oldBg);
@@ -448,7 +448,7 @@ async function onBackgroundUploadSelected() {
448448
449449 const formData = new FormData(form);
450450 await convertFileIfVideo(formData);
451451 await uploadBackground(formData);
452452 form.reset();
453453}
454454
@@ -484,6 +484,7 @@ async function convertFileIfVideo(formData) {
484484 formData.set('avatar', convertedFile);
485485 toastMessage.remove();
486486 } catch (error) {
487+ formData.delete('avatar');
487488 toastMessage.remove();
488489 console.error('Error converting video to animated webp:', error);
489490 toastr.error('Error converting video to animated webp');
@@ -494,26 +495,34 @@ async function convertFileIfVideo(formData) {
494495 * Uploads a background to the server
495496 * @param {FormData} formData
496497 */
497498async 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();
508520 setBackground(bg, generateUrlParameter(bg, false));
509521 await getBackgrounds();
510522 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- });
517526}
518527
519528/**