Compare commits

...
Author SHA1 Message Date
Wolfsblvt 360abf80d3 feat: sanitize existing character filenames on disk during character list loading
- Add sanitizeOnDiskFileName() to detect and rename characters with unsanitized filenames on disk
- Rename associated chat directories when sanitizing character files to maintain sync
- Apply sanitization in /all endpoint to transparently fix legacy characters on first load
- Use getUniqueName() to handle name collisions for both avatar files and chat directories
- Gracefully handle rename failures by keeping the original filename
2026-04-26 19:54:26 +02:00
+52 -1
View File
@@ -1454,7 +1454,8 @@ router.post('/delete', validateAvatarUrlMiddleware, async function (request, res
router.post('/all', async function (request, response) {
try {
const files = fs.readdirSync(request.user.directories.characters);
const pngFiles = files.filter(file => file.endsWith('.png'));
const pngFiles = files.filter(file => file.endsWith('.png'))
.map(file => sanitizeOnDiskFileName(file, request.user.directories));
const processingPromises = pngFiles.map(file => processCharacter(file, request.user.directories, { shallow: useShallowCharacters }));
const data = (await Promise.all(processingPromises)).filter(c => c.name);
return response.send(data);
@@ -1522,6 +1523,56 @@ router.post('/chats', validateAvatarUrlMiddleware, async function (request, resp
}
});
/**
* Checks if a character file on disk has an unsanitized filename and renames it if needed.
* Also renames the associated chats directory to keep them in sync.
* @param {string} fileName The original filename (e.g. "Maya \ Lisa.png")
* @param {import('../users.js').UserDirectoryList} directories User directories
* @returns {string} The sanitized filename, or the original if no change was needed
*/
function sanitizeOnDiskFileName(fileName, directories) {
const nameWithoutExt = path.parse(fileName).name;
const sanitizedName = sanitize(nameWithoutExt);
if (sanitizedName === nameWithoutExt) {
return fileName;
}
const oldChatsDir = path.join(directories.chats, nameWithoutExt);
const hasChats = fs.existsSync(oldChatsDir);
// Find a unique name where both the avatar file and chat directory (if applicable) don't conflict
const newInternalName = getUniqueName(sanitizedName, (name) =>
fs.existsSync(path.join(directories.characters, `${name}.png`)) ||
(hasChats && fs.existsSync(path.join(directories.chats, name))),
{ nameBuilder: (base, i) => i === 0 ? base : `${base}${i}`, startIndex: 0 });
if (!newInternalName) {
console.error(`Failed to find a unique sanitized name for: ${fileName}`);
return fileName;
}
const newFileName = `${newInternalName}.png`;
const oldFilePath = path.join(directories.characters, fileName);
const newFilePath = path.join(directories.characters, newFileName);
try {
fs.renameSync(oldFilePath, newFilePath);
console.info(`Sanitized character filename: "${fileName}" -> "${newFileName}"`);
if (hasChats) {
const newChatsDir = path.join(directories.chats, newInternalName);
fs.renameSync(oldChatsDir, newChatsDir);
console.info(`Renamed chat directory: "${nameWithoutExt}" -> "${newInternalName}"`);
}
} catch (err) {
console.error(`Failed to sanitize character filename: ${fileName}`, err);
return fileName;
}
return newFileName;
}
/**
* Gets the name for the uploaded PNG file.
* @param {string} file File name