fix: recommend to use unlinkSync instead of rmSync, which has a better compatibility handling non-English characters

This commit is contained in:
wickedcode
2025-05-01 03:09:25 -04:00
parent 7431b0e8aa
commit d3bb625efe
12 changed files with 23 additions and 23 deletions

View File

@@ -235,14 +235,14 @@ router.post('/download', async (request, response) => {
const contentType = mime.lookup(temp_path) || 'application/octet-stream';
response.setHeader('Content-Type', contentType);
response.send(fileContent);
fs.rmSync(temp_path);
fs.unlinkSync(temp_path);
return;
}
// Move into asset place
console.info('Download finished, moving file from', temp_path, 'to', file_path);
fs.copyFileSync(temp_path, file_path);
fs.rmSync(temp_path);
fs.unlinkSync(temp_path);
response.sendStatus(200);
}
catch (error) {

View File

@@ -53,7 +53,7 @@ router.post('/upload', async (request, response) => {
const filename = request.body.overwrite_name || `${Date.now()}.png`;
const pathToNewFile = path.join(request.user.directories.avatars, filename);
writeFileAtomicSync(pathToNewFile, image);
fs.rmSync(pathToUpload);
fs.unlinkSync(pathToUpload);
return response.send({ path: filename });
} catch (err) {
return response.status(400).send('Is not a valid image');

View File

@@ -204,7 +204,7 @@ router.post('/transcribe-audio', async function (request, response) {
console.debug('Transcribing audio with KoboldCpp', server);
const fileBase64 = fs.readFileSync(request.file.path).toString('base64');
fs.rmSync(request.file.path);
fs.unlinkSync(request.file.path);
const headers = {};
setAdditionalHeadersByType(headers, TEXTGEN_TYPES.KOBOLDCPP, server, request.user.directories);

View File

@@ -30,7 +30,7 @@ router.post('/delete', getFileNameValidationFunction('bg'), function (request, r
return response.sendStatus(400);
}
fs.rmSync(fileName);
fs.unlinkSync(fileName);
invalidateThumbnail(request.user.directories, 'bg', request.body.bg);
return response.send('ok');
});
@@ -52,7 +52,7 @@ router.post('/rename', function (request, response) {
}
fs.copyFileSync(oldFileName, newFileName);
fs.rmSync(oldFileName);
fs.unlinkSync(oldFileName);
invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg);
return response.send('ok');
});
@@ -65,7 +65,7 @@ router.post('/upload', function (request, response) {
try {
fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename));
fs.rmSync(img_path);
fs.unlinkSync(img_path);
invalidateThumbnail(request.user.directories, 'bg', filename);
response.send(filename);
} catch (err) {

View File

@@ -720,7 +720,7 @@ function convertWorldInfoToCharacterBook(name, entries) {
*/
async function importFromYaml(uploadPath, context, preservedFileName) {
const fileText = fs.readFileSync(uploadPath, 'utf8');
fs.rmSync(uploadPath);
fs.unlinkSync(uploadPath);
const yamlData = yaml.parse(fileText);
console.info('Importing from YAML');
yamlData.name = sanitize(yamlData.name);
@@ -754,7 +754,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) {
*/
async function importFromCharX(uploadPath, { request }, preservedFileName) {
const data = fs.readFileSync(uploadPath).buffer;
fs.rmSync(uploadPath);
fs.unlinkSync(uploadPath);
console.info('Importing from CharX');
const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');
@@ -995,7 +995,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
}
// Remove the old character file
fs.rmSync(oldAvatarPath);
fs.unlinkSync(oldAvatarPath);
// Return new avatar name to ST
return response.send({ avatar: newAvatarName });

View File

@@ -433,7 +433,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
}
fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);
fs.rmSync(pathToOriginalFile);
fs.unlinkSync(pathToOriginalFile);
console.info('Successfully renamed.');
return response.send({ ok: true, sanitizedFileName });
});
@@ -665,7 +665,7 @@ router.post('/group/delete', (request, response) => {
const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
if (fs.existsSync(pathToFile)) {
fs.rmSync(pathToFile);
fs.unlinkSync(pathToFile);
return response.send({ ok: true });
}

View File

@@ -66,7 +66,7 @@ router.post('/delete', async (request, response) => {
return response.status(404).send('File not found');
}
fs.rmSync(pathToDelete);
fs.unlinkSync(pathToDelete);
console.info(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`);
return response.sendStatus(200);
} catch (error) {

View File

@@ -117,7 +117,7 @@ router.post('/delete', async (request, response) => {
const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
if (fs.existsSync(pathToFile)) {
fs.rmSync(pathToFile);
fs.unlinkSync(pathToFile);
}
}
}
@@ -126,7 +126,7 @@ router.post('/delete', async (request, response) => {
}
if (fs.existsSync(pathToGroup)) {
fs.rmSync(pathToGroup);
fs.unlinkSync(pathToGroup);
}
return response.send({ ok: true });

View File

@@ -234,7 +234,7 @@ router.post('/transcribe-audio', async (request, response) => {
return response.status(500).send(text);
}
fs.rmSync(request.file.path);
fs.unlinkSync(request.file.path);
const data = await result.json();
console.debug('OpenAI transcription response', data);
return response.json(data);

View File

@@ -124,7 +124,7 @@ router.post('/delete-openai', function (request, response) {
const pathToFile = path.join(request.user.directories.openAI_Settings, `${name}.json`);
if (fs.existsSync(pathToFile)) {
fs.rmSync(pathToFile);
fs.unlinkSync(pathToFile);
return response.send({ ok: true });
}

View File

@@ -165,7 +165,7 @@ router.post('/delete', async (request, response) => {
// Remove existing sprite with the same label
for (const file of files) {
if (path.parse(file).name === spriteName) {
fs.rmSync(path.join(spritesPath, file));
fs.unlinkSync(path.join(spritesPath, file));
}
}
@@ -206,7 +206,7 @@ router.post('/upload-zip', async (request, response) => {
const existingFile = files.find(file => path.parse(file).name === path.parse(filename).name);
if (existingFile) {
fs.rmSync(path.join(spritesPath, existingFile));
fs.unlinkSync(path.join(spritesPath, existingFile));
}
// Write sprite buffer to disk
@@ -215,7 +215,7 @@ router.post('/upload-zip', async (request, response) => {
}
// Remove uploaded ZIP file
fs.rmSync(spritePackPath);
fs.unlinkSync(spritePackPath);
return response.send({ count: sprites.length });
} catch (error) {
console.error(error);
@@ -251,7 +251,7 @@ router.post('/upload', async (request, response) => {
// Remove existing sprite with the same label
for (const file of files) {
if (path.parse(file).name === spriteName) {
fs.rmSync(path.join(spritesPath, file));
fs.unlinkSync(path.join(spritesPath, file));
}
}
@@ -261,7 +261,7 @@ router.post('/upload', async (request, response) => {
// Copy uploaded file to sprites folder
fs.cpSync(spritePath, pathToFile);
// Remove uploaded file
fs.rmSync(spritePath);
fs.unlinkSync(spritePath);
return response.sendStatus(200);
} catch (error) {
console.error(error);

View File

@@ -419,7 +419,7 @@ export function removeOldBackups(directory, prefix, limit = null) {
break;
}
fs.rmSync(oldest);
fs.unlinkSync(oldest);
}
}
}