Merge pull request #3940 from wickedcode01/bug-fixed Fix the issue where deleting files on Windows may cause the application to crash.

62b02bec3fb2df4eb046c616aca438a90d00f73a

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

Signed
15 files changed, +29 -29Showing 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+2 -2
@@ -28,7 +28,7 @@ router.post('/delete', getFileNameValidationFunction('avatar'), function (reques
28 const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar));28 const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar));
2929
30 if (fs.existsSync(fileName)) {30 if (fs.existsSync(fileName)) {
31 fs.rmSync(fileName);31 fs.unlinkSync(fileName);
32 return response.send({ result: 'ok' });32 return response.send({ result: 'ok' });
33 }33 }
3434
@@ -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+4 -4
@@ -726,7 +726,7 @@ function convertWorldInfoToCharacterBook(name, entries) {
726 */726 */
727async function importFromYaml(uploadPath, context, preservedFileName) {727async function importFromYaml(uploadPath, context, preservedFileName) {
728 const fileText = fs.readFileSync(uploadPath, 'utf8');728 const fileText = fs.readFileSync(uploadPath, 'utf8');
729 fs.rmSync(uploadPath);729 fs.unlinkSync(uploadPath);
730 const yamlData = yaml.parse(fileText);730 const yamlData = yaml.parse(fileText);
731 console.info('Importing from YAML');731 console.info('Importing from YAML');
732 yamlData.name = sanitize(yamlData.name);732 yamlData.name = sanitize(yamlData.name);
@@ -760,7 +760,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) {
760 */760 */
761async function importFromCharX(uploadPath, { request }, preservedFileName) {761async function importFromCharX(uploadPath, { request }, preservedFileName) {
762 const data = fs.readFileSync(uploadPath).buffer;762 const data = fs.readFileSync(uploadPath).buffer;
763 fs.rmSync(uploadPath);763 fs.unlinkSync(uploadPath);
764 console.info('Importing from CharX');764 console.info('Importing from CharX');
765 const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');765 const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');
766766
@@ -1001,7 +1001,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
1001 }1001 }
10021002
1003 // Remove the old character file1003 // Remove the old character file
1004 fs.rmSync(oldAvatarPath);1004 fs.unlinkSync(oldAvatarPath);
10051005
1006 // Return new avatar name to ST1006 // Return new avatar name to ST
1007 return response.send({ avatar: newAvatarName });1007 return response.send({ avatar: newAvatarName });
@@ -1156,7 +1156,7 @@ router.post('/delete', validateAvatarUrlMiddleware, async function (request, res
1156 return response.sendStatus(400);1156 return response.sendStatus(400);
1157 }1157 }
11581158
1159 fs.rmSync(avatarPath);1159 fs.unlinkSync(avatarPath);
1160 invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url);1160 invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url);
1161 let dir_name = (request.body.avatar_url.replace('.png', ''));1161 let dir_name = (request.body.avatar_url.replace('.png', ''));
11621162
src/endpoints/chats.js+3 -3
@@ -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});
@@ -449,7 +449,7 @@ router.post('/delete', validateAvatarUrlMiddleware, function (request, response)
449 return response.sendStatus(400);449 return response.sendStatus(400);
450 }450 }
451451
452 fs.rmSync(filePath);452 fs.unlinkSync(filePath);
453 console.info(`Deleted chat file: ${filePath}`);453 console.info(`Deleted chat file: ${filePath}`);
454 return response.send('ok');454 return response.send('ok');
455});455});
@@ -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/endpoints/themes.js+1 -1
@@ -29,7 +29,7 @@ router.post('/delete', function (request, response) {
29 console.error('Theme file not found:', filename);29 console.error('Theme file not found:', filename);
30 return response.sendStatus(404);30 return response.sendStatus(404);
31 }31 }
32 fs.rmSync(filename);32 fs.unlinkSync(filename);
33 return response.sendStatus(200);33 return response.sendStatus(200);
34 } catch (error) {34 } catch (error) {
35 console.error(error);35 console.error(error);
src/endpoints/thumbnails.js+1 -1
@@ -75,7 +75,7 @@ export function invalidateThumbnail(directories, type, file) {
75 const pathToThumbnail = path.join(folder, file);75 const pathToThumbnail = path.join(folder, file);
7676
77 if (fs.existsSync(pathToThumbnail)) {77 if (fs.existsSync(pathToThumbnail)) {
78 fs.rmSync(pathToThumbnail);78 fs.unlinkSync(pathToThumbnail);
79 }79 }
80}80}
8181
src/endpoints/worldinfo.js+1 -1
@@ -57,7 +57,7 @@ router.post('/delete', (request, response) => {
57 throw new Error(`World info file ${filename} doesn't exist.`);57 throw new Error(`World info file ${filename} doesn't exist.`);
58 }58 }
5959
60 fs.rmSync(pathToWorldInfo);60 fs.unlinkSync(pathToWorldInfo);
6161
62 return response.sendStatus(200);62 return response.sendStatus(200);
63});63});
src/util.js+1 -1
@@ -435,7 +435,7 @@ export function removeOldBackups(directory, prefix, limit = null) {
435 break;435 break;
436 }436 }
437437
438 fs.rmSync(oldest);438 fs.unlinkSync(oldest);
439 }439 }
440 }440 }
441}441}