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
Signed| @@ -6448,11 +6448,7 @@ async function read_avatar_load(input) { | ||
| 6448 | 6448 | const formData = new FormData(/** @type {HTMLFormElement} */($('#form_create').get(0))); |
| 6449 | 6449 | await fetch(getThumbnailUrl('avatar', formData.get('avatar_url').toString()), { |
| 6450 | 6450 | method: 'GET', |
| 6451 | 6451 | cache: 'no-cachereload', |
| 6452 | - headers: { | |
| 6453 | - 'pragma': 'no-cache', | |
| 6454 | - 'cache-control': 'no-cache', | |
| 6455 | - }, | |
| 6456 | 6452 | }); |
| 6457 | 6453 | |
| 6458 | 6454 | const messages = $('.mes').toArray(); |
| @@ -10854,7 +10850,7 @@ jQuery(async function () { | ||
| 10854 | 10850 | data.set(file, characters[this_chid].avatar); |
| 10855 | 10851 | await processDroppedFiles([file], data); |
| 10856 | 10852 | await openCharacterChat(chatFile); |
| 10857 | 10853 | await fetch(getThumbnailUrl('avatar', characters[this_chid].avatar), { cache: 'no-cachereload' }); |
| 10858 | 10854 | } catch { |
| 10859 | 10855 | toastr.error('Failed to replace the character card.', 'Something went wrong'); |
| 10860 | 10856 | } |
| @@ -317,14 +317,20 @@ async function uploadUserAvatar(url, name) { | ||
| 317 | 317 | formData.append('overwrite_name', name); |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | const response = await fetch('/api/avatars/upload', { |
| 321 | 321 | method: 'POST', |
| 322 | 322 | headers: getRequestHeaders({ omitContentType: true }), |
| 323 | 323 | cache: 'no-cache', |
| 324 | 324 | body: formData, |
| 325 | 325 | }); |
| 326 | 326 | |
| 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); | |
| 328 | 334 | } |
| 329 | 335 | |
| 330 | 336 | async function changeUserAvatar(e) { |
| @@ -375,21 +381,23 @@ async function changeUserAvatar(e) { | ||
| 375 | 381 | if (response.ok) { |
| 376 | 382 | const data = await response.json(); |
| 377 | 383 | |
| 384 | + const overwriteName = formData.get('overwrite_name'); | |
| 385 | + const dataPath = data?.path; | |
| 386 | + | |
| 378 | 387 | // 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' }); | |
| 381 | 390 | await fetch(getUserAvatargetThumbnailUrl('persona', String(namedataPath)), { cache: 'no-cachereload' }); |
| 382 | - await fetch(getThumbnailUrl('persona', String(name)), { cache: 'no-cache' }); | |
| 383 | 391 | reloadUserAvatar(true); |
| 384 | 392 | } |
| 385 | 393 | |
| 386 | 394 | if (!nameoverwriteName && data.pathdataPath) { |
| 387 | 395 | await getUserAvatars(); |
| 388 | 396 | await delay(5001); |
| 389 | 397 | await createPersona(data.pathdataPath); |
| 390 | 398 | } |
| 391 | 399 | |
| 392 | 400 | await getUserAvatars(true, namedataPath || data.pathoverwriteName); |
| 393 | 401 | } |
| 394 | 402 | |
| 395 | 403 | // Will allow to select the same file twice in a row |
| @@ -1424,6 +1432,12 @@ async function loadPersonaForCurrentChat({ doRender = false } = {}) { | ||
| 1424 | 1432 | // Cache persona list to check if they exist |
| 1425 | 1433 | const userAvatars = await getUserAvatars(doRender); |
| 1426 | 1434 | |
| 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 | + | |
| 1427 | 1441 | // Define a persona for this chat |
| 1428 | 1442 | let chatPersona = ''; |
| 1429 | 1443 | |
| @@ -53,7 +53,7 @@ router.post('/upload', getFileNameValidationFunction('overwrite_name'), async (r | ||
| 53 | 53 | cacheBuster.bust(request, response); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | const filename = sanitize(request.body.overwrite_name || `${Date.now()}.png`); |
| 57 | 57 | const pathToNewFile = path.join(request.user.directories.avatars, filename); |
| 58 | 58 | writeFileAtomicSync(pathToNewFile, image); |
| 59 | 59 | fs.unlinkSync(pathToUpload); |