Update upload to use fetch

This commit is contained in:
Cohee
2025-04-09 19:45:33 +03:00
parent 2fa6a11650
commit e9178e52eb

View File

@@ -217,7 +217,7 @@ async function onCopyToSystemBackgroundClick(e) {
const formData = new FormData(); const formData = new FormData();
formData.set('avatar', file); formData.set('avatar', file);
uploadBackground(formData); await uploadBackground(formData);
const list = chat_metadata[LIST_METADATA_KEY] || []; const list = chat_metadata[LIST_METADATA_KEY] || [];
const index = list.indexOf(bgNames.oldBg); const index = list.indexOf(bgNames.oldBg);
@@ -448,7 +448,7 @@ async function onBackgroundUploadSelected() {
const formData = new FormData(form); const formData = new FormData(form);
await convertFileIfVideo(formData); await convertFileIfVideo(formData);
uploadBackground(formData); await uploadBackground(formData);
form.reset(); form.reset();
} }
@@ -484,6 +484,7 @@ async function convertFileIfVideo(formData) {
formData.set('avatar', convertedFile); formData.set('avatar', convertedFile);
toastMessage.remove(); toastMessage.remove();
} catch (error) { } catch (error) {
formData.delete('avatar');
toastMessage.remove(); toastMessage.remove();
console.error('Error converting video to animated webp:', error); console.error('Error converting video to animated webp:', error);
toastr.error('Error converting video to animated webp'); toastr.error('Error converting video to animated webp');
@@ -494,26 +495,34 @@ async function convertFileIfVideo(formData) {
* Uploads a background to the server * Uploads a background to the server
* @param {FormData} formData * @param {FormData} formData
*/ */
function uploadBackground(formData) { async function uploadBackground(formData) {
jQuery.ajax({ try {
type: 'POST', if (!formData.has('avatar')) {
url: '/api/backgrounds/upload', console.log('No file provided. Background upload cancelled.');
data: formData, return;
beforeSend: function () { }
},
cache: false, const headers = getRequestHeaders();
contentType: false, delete headers['Content-Type'];
processData: false,
success: async function (bg) { const response = await fetch('/api/backgrounds/upload', {
setBackground(bg, generateUrlParameter(bg, false)); method: 'POST',
await getBackgrounds(); headers: getRequestHeaders(),
highlightNewBackground(bg); body: formData,
}, cache: 'no-cache',
error: function (jqXHR, exception) { });
console.log(exception);
console.log(jqXHR); if (!response.ok) {
}, throw new Error('Failed to upload background');
}); }
const bg = await response.text();
setBackground(bg, generateUrlParameter(bg, false));
await getBackgrounds();
highlightNewBackground(bg);
} catch (error) {
console.error('Error uploading background:', error);
}
} }
/** /**