Fix convert to persona with unusual names (#4319) * Fix convert to persona with unusual names * Fix picking new persona on current persona deletion * Prefer actual path on page reload

7b67769c926cf2e92cb8cbe19c22b9a678834b30

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

Signed
3 files changed, +28 -18Ignore whitespace
public/script.js+2 -6
@@ -6448,11 +6448,7 @@ async function read_avatar_load(input) {
64486448 const formData = new FormData(/** @type {HTMLFormElement} */($('#form_create').get(0)));
64496449 await fetch(getThumbnailUrl('avatar', formData.get('avatar_url').toString()), {
64506450 method: 'GET',
64516451 cache: 'no-cachereload',
6452- headers: {
6453- 'pragma': 'no-cache',
6454- 'cache-control': 'no-cache',
6455- },
64566452 });
64576453
64586454 const messages = $('.mes').toArray();
@@ -10854,7 +10850,7 @@ jQuery(async function () {
1085410850 data.set(file, characters[this_chid].avatar);
1085510851 await processDroppedFiles([file], data);
1085610852 await openCharacterChat(chatFile);
1085710853 await fetch(getThumbnailUrl('avatar', characters[this_chid].avatar), { cache: 'no-cachereload' });
1085810854 } catch {
1085910855 toastr.error('Failed to replace the character card.', 'Something went wrong');
1086010856 }
public/scripts/personas.js+25 -11
@@ -317,14 +317,20 @@ async function uploadUserAvatar(url, name) {
317317 formData.append('overwrite_name', name);
318318 }
319319
320320 const response = await fetch('/api/avatars/upload', {
321321 method: 'POST',
322322 headers: getRequestHeaders({ omitContentType: true }),
323323 cache: 'no-cache',
324324 body: formData,
325325 });
326326
327- await getUserAvatars(true, name);
327+ if (!response.ok) {
328+ throw new Error(`Failed to upload avatar: ${response.statusText}`);
329+ }
330+
331+ // Get the actual path from the response
332+ const data = await response.json();
333+ await getUserAvatars(true, data?.path || name);
328334}
329335
330336async function changeUserAvatar(e) {
@@ -367,7 +373,7 @@ async function changeUserAvatar(e) {
367373
368374 const response = await fetch(url, {
369375 method: 'POST',
370376 headers: getRequestHeaders({ omitContentType: true }),
371377 cache: 'no-cache',
372378 body: formData,
373379 });
@@ -375,21 +381,23 @@ async function changeUserAvatar(e) {
375381 if (response.ok) {
376382 const data = await response.json();
377383
384+ const overwriteName = formData.get('overwrite_name');
385+ const dataPath = data?.path;
386+
378387 // If the user uploaded a new avatar, we want to make sure it's not cached
379- const name = formData.get('overwrite_name');
388+ if (overwriteName && dataPath) {
380- if (name) {
389+ await fetch(getUserAvatar(String(dataPath)), { cache: 'reload' });
381390 await fetch(getUserAvatargetThumbnailUrl('persona', String(namedataPath)), { cache: 'no-cachereload' });
382- await fetch(getThumbnailUrl('persona', String(name)), { cache: 'no-cache' });
383391 reloadUserAvatar(true);
384392 }
385393
386394 if (!nameoverwriteName && data.pathdataPath) {
387395 await getUserAvatars();
388396 await delay(5001);
389397 await createPersona(data.pathdataPath);
390398 }
391399
392400 await getUserAvatars(true, namedataPath || data.pathoverwriteName);
393401 }
394402
395403 // Will allow to select the same file twice in a row
@@ -1424,6 +1432,12 @@ async function loadPersonaForCurrentChat({ doRender = false } = {}) {
14241432 // Cache persona list to check if they exist
14251433 const userAvatars = await getUserAvatars(doRender);
14261434
1435+ // Check if the user avatar is set and exists in the list of user avatars
1436+ if (userAvatars.length && !userAvatars.includes(user_avatar)) {
1437+ console.log(`User avatar ${user_avatar} not found in user avatars list, pick the first available one`);
1438+ setUserAvatar(userAvatars[0], { toastPersonaNameChange: false, navigateToCurrent: true });
1439+ }
1440+
14271441 // Define a persona for this chat
14281442 let chatPersona = '';
14291443
src/endpoints/avatars.js+1 -1
@@ -53,7 +53,7 @@ router.post('/upload', getFileNameValidationFunction('overwrite_name'), async (r
5353 cacheBuster.bust(request, response);
5454 }
5555
5656 const filename = sanitize(request.body.overwrite_name || `${Date.now()}.png`);
5757 const pathToNewFile = path.join(request.user.directories.avatars, filename);
5858 writeFileAtomicSync(pathToNewFile, image);
5959 fs.unlinkSync(pathToUpload);