Update persona upload methods to fetch

This commit is contained in:
Cohee
2025-01-30 02:30:51 +02:00
parent 3d817352ec
commit cbeb7ddcec

View File

@ -268,7 +268,7 @@ export async function getUserAvatars(doRender = true, openPageAt = '') {
* Uploads an avatar file to the server * Uploads an avatar file to the server
* @param {string} url URL for the avatar file * @param {string} url URL for the avatar file
* @param {string} [name] Optional name for the avatar file * @param {string} [name] Optional name for the avatar file
* @returns {Promise} Promise object representing the AJAX request * @returns {Promise} Promise that resolves when the avatar is uploaded
*/ */
async function uploadUserAvatar(url, name) { async function uploadUserAvatar(url, name) {
const fetchResult = await fetch(url); const fetchResult = await fetch(url);
@ -281,18 +281,17 @@ async function uploadUserAvatar(url, name) {
formData.append('overwrite_name', name); formData.append('overwrite_name', name);
} }
return jQuery.ajax({ const headers = getRequestHeaders();
type: 'POST', delete headers['Content-Type'];
url: '/api/avatars/upload',
data: formData, await fetch('/api/avatars/upload', {
beforeSend: () => { }, method: 'POST',
cache: false, headers: headers,
contentType: false, cache: 'no-cache',
processData: false, body: formData,
success: async function () {
await getUserAvatars(true, name);
},
}); });
await getUserAvatars(true, name);
} }
async function changeUserAvatar(e) { async function changeUserAvatar(e) {
@ -333,15 +332,19 @@ async function changeUserAvatar(e) {
formData.set('avatar', convertedFile); formData.set('avatar', convertedFile);
} }
jQuery.ajax({ const headers = getRequestHeaders();
type: 'POST', delete headers['Content-Type'];
url: url,
data: formData, const response = await fetch(url, {
beforeSend: () => { }, method: 'POST',
cache: false, headers: headers,
contentType: false, cache: 'no-cache',
processData: false, body: formData,
success: async function (data) { });
if (response.ok) {
const data = await response.json();
// If the user uploaded a new avatar, we want to make sure it's not cached // If the user uploaded a new avatar, we want to make sure it's not cached
const name = formData.get('overwrite_name'); const name = formData.get('overwrite_name');
if (name) { if (name) {
@ -356,9 +359,7 @@ async function changeUserAvatar(e) {
} }
await getUserAvatars(true, name || data.path); await getUserAvatars(true, name || data.path);
}, }
error: (jqXHR, exception) => { },
});
// Will allow to select the same file twice in a row // Will allow to select the same file twice in a row
form.reset(); form.reset();