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

d3bb625efebec5805348df6bc05a855897bdd27d

wickedcode <wickedcode@outlook.com>

12 files changed, +23 -23Showing whitespace changes
src/endpoints/assets.js+2 -2
@@ -235,14 +235,14 @@ router.post('/download', async (request, response) => {
235 const contentType = mime.lookup(temp_path) || 'application/octet-stream';235 const contentType = mime.lookup(temp_path) || 'application/octet-stream';
236 response.setHeader('Content-Type', contentType);236 response.setHeader('Content-Type', contentType);
237 response.send(fileContent);237 response.send(fileContent);
238 fs.rmSync(temp_path);238 fs.unlinkSync(temp_path);
239 return;239 return;
240 }240 }
241241
242 // Move into asset place242 // Move into asset place
243 console.info('Download finished, moving file from', temp_path, 'to', file_path);243 console.info('Download finished, moving file from', temp_path, 'to', file_path);
244 fs.copyFileSync(temp_path, file_path);244 fs.copyFileSync(temp_path, file_path);
245 fs.rmSync(temp_path);245 fs.unlinkSync(temp_path);
246 response.sendStatus(200);246 response.sendStatus(200);
247 }247 }
248 catch (error) {248 catch (error) {
src/endpoints/avatars.js+1 -1
@@ -53,7 +53,7 @@ router.post('/upload', async (request, response) => {
53 const filename = request.body.overwrite_name || `${Date.now()}.png`;53 const filename = request.body.overwrite_name || `${Date.now()}.png`;
54 const pathToNewFile = path.join(request.user.directories.avatars, filename);54 const pathToNewFile = path.join(request.user.directories.avatars, filename);
55 writeFileAtomicSync(pathToNewFile, image);55 writeFileAtomicSync(pathToNewFile, image);
56 fs.rmSync(pathToUpload);56 fs.unlinkSync(pathToUpload);
57 return response.send({ path: filename });57 return response.send({ path: filename });
58 } catch (err) {58 } catch (err) {
59 return response.status(400).send('Is not a valid image');59 return response.status(400).send('Is not a valid image');
src/endpoints/backends/kobold.js+1 -1
@@ -204,7 +204,7 @@ router.post('/transcribe-audio', async function (request, response) {
204 console.debug('Transcribing audio with KoboldCpp', server);204 console.debug('Transcribing audio with KoboldCpp', server);
205205
206 const fileBase64 = fs.readFileSync(request.file.path).toString('base64');206 const fileBase64 = fs.readFileSync(request.file.path).toString('base64');
207 fs.rmSync(request.file.path);207 fs.unlinkSync(request.file.path);
208208
209 const headers = {};209 const headers = {};
210 setAdditionalHeadersByType(headers, TEXTGEN_TYPES.KOBOLDCPP, server, request.user.directories);210 setAdditionalHeadersByType(headers, TEXTGEN_TYPES.KOBOLDCPP, server, request.user.directories);
src/endpoints/backgrounds.js+3 -3
@@ -30,7 +30,7 @@ router.post('/delete', getFileNameValidationFunction('bg'), function (request, r
30 return response.sendStatus(400);30 return response.sendStatus(400);
31 }31 }
3232
33 fs.rmSync(fileName);33 fs.unlinkSync(fileName);
34 invalidateThumbnail(request.user.directories, 'bg', request.body.bg);34 invalidateThumbnail(request.user.directories, 'bg', request.body.bg);
35 return response.send('ok');35 return response.send('ok');
36});36});
@@ -52,7 +52,7 @@ router.post('/rename', function (request, response) {
52 }52 }
5353
54 fs.copyFileSync(oldFileName, newFileName);54 fs.copyFileSync(oldFileName, newFileName);
55 fs.rmSync(oldFileName);55 fs.unlinkSync(oldFileName);
56 invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg);56 invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg);
57 return response.send('ok');57 return response.send('ok');
58});58});
@@ -65,7 +65,7 @@ router.post('/upload', function (request, response) {
6565
66 try {66 try {
67 fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename));67 fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename));
68 fs.rmSync(img_path);68 fs.unlinkSync(img_path);
69 invalidateThumbnail(request.user.directories, 'bg', filename);69 invalidateThumbnail(request.user.directories, 'bg', filename);
70 response.send(filename);70 response.send(filename);
71 } catch (err) {71 } catch (err) {
src/endpoints/characters.js+3 -3
@@ -720,7 +720,7 @@ function convertWorldInfoToCharacterBook(name, entries) {
720 */720 */
721async function importFromYaml(uploadPath, context, preservedFileName) {721async function importFromYaml(uploadPath, context, preservedFileName) {
722 const fileText = fs.readFileSync(uploadPath, 'utf8');722 const fileText = fs.readFileSync(uploadPath, 'utf8');
723 fs.rmSync(uploadPath);723 fs.unlinkSync(uploadPath);
724 const yamlData = yaml.parse(fileText);724 const yamlData = yaml.parse(fileText);
725 console.info('Importing from YAML');725 console.info('Importing from YAML');
726 yamlData.name = sanitize(yamlData.name);726 yamlData.name = sanitize(yamlData.name);
@@ -754,7 +754,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) {
754 */754 */
755async function importFromCharX(uploadPath, { request }, preservedFileName) {755async function importFromCharX(uploadPath, { request }, preservedFileName) {
756 const data = fs.readFileSync(uploadPath).buffer;756 const data = fs.readFileSync(uploadPath).buffer;
757 fs.rmSync(uploadPath);757 fs.unlinkSync(uploadPath);
758 console.info('Importing from CharX');758 console.info('Importing from CharX');
759 const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');759 const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');
760760
@@ -995,7 +995,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
995 }995 }
996996
997 // Remove the old character file997 // Remove the old character file
998 fs.rmSync(oldAvatarPath);998 fs.unlinkSync(oldAvatarPath);
999999
1000 // Return new avatar name to ST1000 // Return new avatar name to ST
1001 return response.send({ avatar: newAvatarName });1001 return response.send({ avatar: newAvatarName });
src/endpoints/chats.js+2 -2
@@ -433,7 +433,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
433 }433 }
434434
435 fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);435 fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);
436 fs.rmSync(pathToOriginalFile);436 fs.unlinkSync(pathToOriginalFile);
437 console.info('Successfully renamed.');437 console.info('Successfully renamed.');
438 return response.send({ ok: true, sanitizedFileName });438 return response.send({ ok: true, sanitizedFileName });
439});439});
@@ -665,7 +665,7 @@ router.post('/group/delete', (request, response) => {
665 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);665 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
666666
667 if (fs.existsSync(pathToFile)) {667 if (fs.existsSync(pathToFile)) {
668 fs.rmSync(pathToFile);668 fs.unlinkSync(pathToFile);
669 return response.send({ ok: true });669 return response.send({ ok: true });
670 }670 }
671671
src/endpoints/files.js+1 -1
@@ -66,7 +66,7 @@ router.post('/delete', async (request, response) => {
66 return response.status(404).send('File not found');66 return response.status(404).send('File not found');
67 }67 }
6868
69 fs.rmSync(pathToDelete);69 fs.unlinkSync(pathToDelete);
70 console.info(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`);70 console.info(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`);
71 return response.sendStatus(200);71 return response.sendStatus(200);
72 } catch (error) {72 } catch (error) {
src/endpoints/groups.js+2 -2
@@ -117,7 +117,7 @@ router.post('/delete', async (request, response) => {
117 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);117 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
118118
119 if (fs.existsSync(pathToFile)) {119 if (fs.existsSync(pathToFile)) {
120 fs.rmSync(pathToFile);120 fs.unlinkSync(pathToFile);
121 }121 }
122 }122 }
123 }123 }
@@ -126,7 +126,7 @@ router.post('/delete', async (request, response) => {
126 }126 }
127127
128 if (fs.existsSync(pathToGroup)) {128 if (fs.existsSync(pathToGroup)) {
129 fs.rmSync(pathToGroup);129 fs.unlinkSync(pathToGroup);
130 }130 }
131131
132 return response.send({ ok: true });132 return response.send({ ok: true });
src/endpoints/openai.js+1 -1
@@ -234,7 +234,7 @@ router.post('/transcribe-audio', async (request, response) => {
234 return response.status(500).send(text);234 return response.status(500).send(text);
235 }235 }
236236
237 fs.rmSync(request.file.path);237 fs.unlinkSync(request.file.path);
238 const data = await result.json();238 const data = await result.json();
239 console.debug('OpenAI transcription response', data);239 console.debug('OpenAI transcription response', data);
240 return response.json(data);240 return response.json(data);
src/endpoints/presets.js+1 -1
@@ -124,7 +124,7 @@ router.post('/delete-openai', function (request, response) {
124 const pathToFile = path.join(request.user.directories.openAI_Settings, `${name}.json`);124 const pathToFile = path.join(request.user.directories.openAI_Settings, `${name}.json`);
125125
126 if (fs.existsSync(pathToFile)) {126 if (fs.existsSync(pathToFile)) {
127 fs.rmSync(pathToFile);127 fs.unlinkSync(pathToFile);
128 return response.send({ ok: true });128 return response.send({ ok: true });
129 }129 }
130130
src/endpoints/sprites.js+5 -5
@@ -165,7 +165,7 @@ router.post('/delete', async (request, response) => {
165 // Remove existing sprite with the same label165 // Remove existing sprite with the same label
166 for (const file of files) {166 for (const file of files) {
167 if (path.parse(file).name === spriteName) {167 if (path.parse(file).name === spriteName) {
168 fs.rmSync(path.join(spritesPath, file));168 fs.unlinkSync(path.join(spritesPath, file));
169 }169 }
170 }170 }
171171
@@ -206,7 +206,7 @@ router.post('/upload-zip', async (request, response) => {
206 const existingFile = files.find(file => path.parse(file).name === path.parse(filename).name);206 const existingFile = files.find(file => path.parse(file).name === path.parse(filename).name);
207207
208 if (existingFile) {208 if (existingFile) {
209 fs.rmSync(path.join(spritesPath, existingFile));209 fs.unlinkSync(path.join(spritesPath, existingFile));
210 }210 }
211211
212 // Write sprite buffer to disk212 // Write sprite buffer to disk
@@ -215,7 +215,7 @@ router.post('/upload-zip', async (request, response) => {
215 }215 }
216216
217 // Remove uploaded ZIP file217 // Remove uploaded ZIP file
218 fs.rmSync(spritePackPath);218 fs.unlinkSync(spritePackPath);
219 return response.send({ count: sprites.length });219 return response.send({ count: sprites.length });
220 } catch (error) {220 } catch (error) {
221 console.error(error);221 console.error(error);
@@ -251,7 +251,7 @@ router.post('/upload', async (request, response) => {
251 // Remove existing sprite with the same label251 // Remove existing sprite with the same label
252 for (const file of files) {252 for (const file of files) {
253 if (path.parse(file).name === spriteName) {253 if (path.parse(file).name === spriteName) {
254 fs.rmSync(path.join(spritesPath, file));254 fs.unlinkSync(path.join(spritesPath, file));
255 }255 }
256 }256 }
257257
@@ -261,7 +261,7 @@ router.post('/upload', async (request, response) => {
261 // Copy uploaded file to sprites folder261 // Copy uploaded file to sprites folder
262 fs.cpSync(spritePath, pathToFile);262 fs.cpSync(spritePath, pathToFile);
263 // Remove uploaded file263 // Remove uploaded file
264 fs.rmSync(spritePath);264 fs.unlinkSync(spritePath);
265 return response.sendStatus(200);265 return response.sendStatus(200);
266 } catch (error) {266 } catch (error) {
267 console.error(error);267 console.error(error);
src/util.js+1 -1
@@ -419,7 +419,7 @@ export function removeOldBackups(directory, prefix, limit = null) {
419 break;419 break;
420 }420 }
421421
422 fs.rmSync(oldest);422 fs.unlinkSync(oldest);
423 }423 }
424 }424 }
425}425}