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 -29Ignore whitespace
src/endpoints/assets.js+2 -2
@@ -235,14 +235,14 @@ router.post('/download', async (request, response) => {
235235 const contentType = mime.lookup(temp_path) || 'application/octet-stream';
236236 response.setHeader('Content-Type', contentType);
237237 response.send(fileContent);
238238 fs.rmSyncunlinkSync(temp_path);
239239 return;
240240 }
241241
242242 // Move into asset place
243243 console.info('Download finished, moving file from', temp_path, 'to', file_path);
244244 fs.copyFileSync(temp_path, file_path);
245245 fs.rmSyncunlinkSync(temp_path);
246246 response.sendStatus(200);
247247 }
248248 catch (error) {
src/endpoints/avatars.js+2 -2
@@ -28,7 +28,7 @@ router.post('/delete', getFileNameValidationFunction('avatar'), function (reques
2828 const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar));
2929
3030 if (fs.existsSync(fileName)) {
3131 fs.rmSyncunlinkSync(fileName);
3232 return response.send({ result: 'ok' });
3333 }
3434
@@ -53,7 +53,7 @@ router.post('/upload', async (request, response) => {
5353 const filename = request.body.overwrite_name || `${Date.now()}.png`;
5454 const pathToNewFile = path.join(request.user.directories.avatars, filename);
5555 writeFileAtomicSync(pathToNewFile, image);
5656 fs.rmSyncunlinkSync(pathToUpload);
5757 return response.send({ path: filename });
5858 } catch (err) {
5959 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) {
204204 console.debug('Transcribing audio with KoboldCpp', server);
205205
206206 const fileBase64 = fs.readFileSync(request.file.path).toString('base64');
207207 fs.rmSyncunlinkSync(request.file.path);
208208
209209 const headers = {};
210210 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
3030 return response.sendStatus(400);
3131 }
3232
3333 fs.rmSyncunlinkSync(fileName);
3434 invalidateThumbnail(request.user.directories, 'bg', request.body.bg);
3535 return response.send('ok');
3636});
@@ -52,7 +52,7 @@ router.post('/rename', function (request, response) {
5252 }
5353
5454 fs.copyFileSync(oldFileName, newFileName);
5555 fs.rmSyncunlinkSync(oldFileName);
5656 invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg);
5757 return response.send('ok');
5858});
@@ -65,7 +65,7 @@ router.post('/upload', function (request, response) {
6565
6666 try {
6767 fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename));
6868 fs.rmSyncunlinkSync(img_path);
6969 invalidateThumbnail(request.user.directories, 'bg', filename);
7070 response.send(filename);
7171 } catch (err) {
src/endpoints/characters.js+4 -4
@@ -726,7 +726,7 @@ function convertWorldInfoToCharacterBook(name, entries) {
726726 */
727727async function importFromYaml(uploadPath, context, preservedFileName) {
728728 const fileText = fs.readFileSync(uploadPath, 'utf8');
729729 fs.rmSyncunlinkSync(uploadPath);
730730 const yamlData = yaml.parse(fileText);
731731 console.info('Importing from YAML');
732732 yamlData.name = sanitize(yamlData.name);
@@ -760,7 +760,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) {
760760 */
761761async function importFromCharX(uploadPath, { request }, preservedFileName) {
762762 const data = fs.readFileSync(uploadPath).buffer;
763763 fs.rmSyncunlinkSync(uploadPath);
764764 console.info('Importing from CharX');
765765 const cardBuffer = await extractFileFromZipBuffer(data, 'card.json');
766766
@@ -1001,7 +1001,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res
10011001 }
10021002
10031003 // Remove the old character file
10041004 fs.rmSyncunlinkSync(oldAvatarPath);
10051005
10061006 // Return new avatar name to ST
10071007 return response.send({ avatar: newAvatarName });
@@ -1156,7 +1156,7 @@ router.post('/delete', validateAvatarUrlMiddleware, async function (request, res
11561156 return response.sendStatus(400);
11571157 }
11581158
11591159 fs.rmSyncunlinkSync(avatarPath);
11601160 invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url);
11611161 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
433433 }
434434
435435 fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);
436436 fs.rmSyncunlinkSync(pathToOriginalFile);
437437 console.info('Successfully renamed.');
438438 return response.send({ ok: true, sanitizedFileName });
439439});
@@ -449,7 +449,7 @@ router.post('/delete', validateAvatarUrlMiddleware, function (request, response)
449449 return response.sendStatus(400);
450450 }
451451
452452 fs.rmSyncunlinkSync(filePath);
453453 console.info(`Deleted chat file: ${filePath}`);
454454 return response.send('ok');
455455});
@@ -665,7 +665,7 @@ router.post('/group/delete', (request, response) => {
665665 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
666666
667667 if (fs.existsSync(pathToFile)) {
668668 fs.rmSyncunlinkSync(pathToFile);
669669 return response.send({ ok: true });
670670 }
671671
src/endpoints/files.js+1 -1
@@ -66,7 +66,7 @@ router.post('/delete', async (request, response) => {
6666 return response.status(404).send('File not found');
6767 }
6868
6969 fs.rmSyncunlinkSync(pathToDelete);
7070 console.info(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`);
7171 return response.sendStatus(200);
7272 } catch (error) {
src/endpoints/groups.js+2 -2
@@ -117,7 +117,7 @@ router.post('/delete', async (request, response) => {
117117 const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`);
118118
119119 if (fs.existsSync(pathToFile)) {
120120 fs.rmSyncunlinkSync(pathToFile);
121121 }
122122 }
123123 }
@@ -126,7 +126,7 @@ router.post('/delete', async (request, response) => {
126126 }
127127
128128 if (fs.existsSync(pathToGroup)) {
129129 fs.rmSyncunlinkSync(pathToGroup);
130130 }
131131
132132 return response.send({ ok: true });
src/endpoints/openai.js+1 -1
@@ -234,7 +234,7 @@ router.post('/transcribe-audio', async (request, response) => {
234234 return response.status(500).send(text);
235235 }
236236
237237 fs.rmSyncunlinkSync(request.file.path);
238238 const data = await result.json();
239239 console.debug('OpenAI transcription response', data);
240240 return response.json(data);
src/endpoints/presets.js+1 -1
@@ -124,7 +124,7 @@ router.post('/delete-openai', function (request, response) {
124124 const pathToFile = path.join(request.user.directories.openAI_Settings, `${name}.json`);
125125
126126 if (fs.existsSync(pathToFile)) {
127127 fs.rmSyncunlinkSync(pathToFile);
128128 return response.send({ ok: true });
129129 }
130130
src/endpoints/sprites.js+5 -5
@@ -165,7 +165,7 @@ router.post('/delete', async (request, response) => {
165165 // Remove existing sprite with the same label
166166 for (const file of files) {
167167 if (path.parse(file).name === spriteName) {
168168 fs.rmSyncunlinkSync(path.join(spritesPath, file));
169169 }
170170 }
171171
@@ -206,7 +206,7 @@ router.post('/upload-zip', async (request, response) => {
206206 const existingFile = files.find(file => path.parse(file).name === path.parse(filename).name);
207207
208208 if (existingFile) {
209209 fs.rmSyncunlinkSync(path.join(spritesPath, existingFile));
210210 }
211211
212212 // Write sprite buffer to disk
@@ -215,7 +215,7 @@ router.post('/upload-zip', async (request, response) => {
215215 }
216216
217217 // Remove uploaded ZIP file
218218 fs.rmSyncunlinkSync(spritePackPath);
219219 return response.send({ count: sprites.length });
220220 } catch (error) {
221221 console.error(error);
@@ -251,7 +251,7 @@ router.post('/upload', async (request, response) => {
251251 // Remove existing sprite with the same label
252252 for (const file of files) {
253253 if (path.parse(file).name === spriteName) {
254254 fs.rmSyncunlinkSync(path.join(spritesPath, file));
255255 }
256256 }
257257
@@ -261,7 +261,7 @@ router.post('/upload', async (request, response) => {
261261 // Copy uploaded file to sprites folder
262262 fs.cpSync(spritePath, pathToFile);
263263 // Remove uploaded file
264264 fs.rmSyncunlinkSync(spritePath);
265265 return response.sendStatus(200);
266266 } catch (error) {
267267 console.error(error);
src/endpoints/themes.js+1 -1
@@ -29,7 +29,7 @@ router.post('/delete', function (request, response) {
2929 console.error('Theme file not found:', filename);
3030 return response.sendStatus(404);
3131 }
3232 fs.rmSyncunlinkSync(filename);
3333 return response.sendStatus(200);
3434 } catch (error) {
3535 console.error(error);
src/endpoints/thumbnails.js+1 -1
@@ -75,7 +75,7 @@ export function invalidateThumbnail(directories, type, file) {
7575 const pathToThumbnail = path.join(folder, file);
7676
7777 if (fs.existsSync(pathToThumbnail)) {
7878 fs.rmSyncunlinkSync(pathToThumbnail);
7979 }
8080}
8181
src/endpoints/worldinfo.js+1 -1
@@ -57,7 +57,7 @@ router.post('/delete', (request, response) => {
5757 throw new Error(`World info file ${filename} doesn't exist.`);
5858 }
5959
6060 fs.rmSyncunlinkSync(pathToWorldInfo);
6161
6262 return response.sendStatus(200);
6363});
src/util.js+1 -1
@@ -435,7 +435,7 @@ export function removeOldBackups(directory, prefix, limit = null) {
435435 break;
436436 }
437437
438438 fs.rmSyncunlinkSync(oldest);
439439 }
440440 }
441441}