Merge pull request #3940 from wickedcode01/bug-fixed Fix the issue where deleting files on Windows may cause the application to crash.
Signed| @@ -235,14 +235,14 @@ router.post('/download', async (request, response) => { | ||
| 235 | 235 | const contentType = mime.lookup(temp_path) || 'application/octet-stream'; |
| 236 | 236 | response.setHeader('Content-Type', contentType); |
| 237 | 237 | response.send(fileContent); |
| 238 | 238 | fs.rmSyncunlinkSync(temp_path); |
| 239 | 239 | return; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Move into asset place |
| 243 | 243 | console.info('Download finished, moving file from', temp_path, 'to', file_path); |
| 244 | 244 | fs.copyFileSync(temp_path, file_path); |
| 245 | 245 | fs.rmSyncunlinkSync(temp_path); |
| 246 | 246 | response.sendStatus(200); |
| 247 | 247 | } |
| 248 | 248 | catch (error) { |
| @@ -28,7 +28,7 @@ router.post('/delete', getFileNameValidationFunction('avatar'), function (reques | ||
| 28 | 28 | const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar)); |
| 29 | 29 | |
| 30 | 30 | if (fs.existsSync(fileName)) { |
| 31 | 31 | fs.rmSyncunlinkSync(fileName); |
| 32 | 32 | return response.send({ result: 'ok' }); |
| 33 | 33 | } |
| 34 | 34 | |
| @@ -53,7 +53,7 @@ router.post('/upload', async (request, response) => { | ||
| 53 | 53 | const filename = request.body.overwrite_name || `${Date.now()}.png`; |
| 54 | 54 | const pathToNewFile = path.join(request.user.directories.avatars, filename); |
| 55 | 55 | writeFileAtomicSync(pathToNewFile, image); |
| 56 | 56 | fs.rmSyncunlinkSync(pathToUpload); |
| 57 | 57 | return response.send({ path: filename }); |
| 58 | 58 | } catch (err) { |
| 59 | 59 | return response.status(400).send('Is not a valid image'); |
| @@ -204,7 +204,7 @@ router.post('/transcribe-audio', async function (request, response) { | ||
| 204 | 204 | console.debug('Transcribing audio with KoboldCpp', server); |
| 205 | 205 | |
| 206 | 206 | const fileBase64 = fs.readFileSync(request.file.path).toString('base64'); |
| 207 | 207 | fs.rmSyncunlinkSync(request.file.path); |
| 208 | 208 | |
| 209 | 209 | const headers = {}; |
| 210 | 210 | setAdditionalHeadersByType(headers, TEXTGEN_TYPES.KOBOLDCPP, server, request.user.directories); |
| @@ -30,7 +30,7 @@ router.post('/delete', getFileNameValidationFunction('bg'), function (request, r | ||
| 30 | 30 | return response.sendStatus(400); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | fs.rmSyncunlinkSync(fileName); |
| 34 | 34 | invalidateThumbnail(request.user.directories, 'bg', request.body.bg); |
| 35 | 35 | return response.send('ok'); |
| 36 | 36 | }); |
| @@ -52,7 +52,7 @@ router.post('/rename', function (request, response) { | ||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | fs.copyFileSync(oldFileName, newFileName); |
| 55 | 55 | fs.rmSyncunlinkSync(oldFileName); |
| 56 | 56 | invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg); |
| 57 | 57 | return response.send('ok'); |
| 58 | 58 | }); |
| @@ -65,7 +65,7 @@ router.post('/upload', function (request, response) { | ||
| 65 | 65 | |
| 66 | 66 | try { |
| 67 | 67 | fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename)); |
| 68 | 68 | fs.rmSyncunlinkSync(img_path); |
| 69 | 69 | invalidateThumbnail(request.user.directories, 'bg', filename); |
| 70 | 70 | response.send(filename); |
| 71 | 71 | } catch (err) { |
| @@ -726,7 +726,7 @@ function convertWorldInfoToCharacterBook(name, entries) { | ||
| 726 | 726 | */ |
| 727 | 727 | async function importFromYaml(uploadPath, context, preservedFileName) { |
| 728 | 728 | const fileText = fs.readFileSync(uploadPath, 'utf8'); |
| 729 | 729 | fs.rmSyncunlinkSync(uploadPath); |
| 730 | 730 | const yamlData = yaml.parse(fileText); |
| 731 | 731 | console.info('Importing from YAML'); |
| 732 | 732 | yamlData.name = sanitize(yamlData.name); |
| @@ -760,7 +760,7 @@ async function importFromYaml(uploadPath, context, preservedFileName) { | ||
| 760 | 760 | */ |
| 761 | 761 | async function importFromCharX(uploadPath, { request }, preservedFileName) { |
| 762 | 762 | const data = fs.readFileSync(uploadPath).buffer; |
| 763 | 763 | fs.rmSyncunlinkSync(uploadPath); |
| 764 | 764 | console.info('Importing from CharX'); |
| 765 | 765 | const cardBuffer = await extractFileFromZipBuffer(data, 'card.json'); |
| 766 | 766 | |
| @@ -1001,7 +1001,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res | ||
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | 1003 | // Remove the old character file |
| 1004 | 1004 | fs.rmSyncunlinkSync(oldAvatarPath); |
| 1005 | 1005 | |
| 1006 | 1006 | // Return new avatar name to ST |
| 1007 | 1007 | return response.send({ avatar: newAvatarName }); |
| @@ -1156,7 +1156,7 @@ router.post('/delete', validateAvatarUrlMiddleware, async function (request, res | ||
| 1156 | 1156 | return response.sendStatus(400); |
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | fs.rmSyncunlinkSync(avatarPath); |
| 1160 | 1160 | invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url); |
| 1161 | 1161 | let dir_name = (request.body.avatar_url.replace('.png', '')); |
| 1162 | 1162 | |
| @@ -433,7 +433,7 @@ router.post('/rename', validateAvatarUrlMiddleware, async function (request, res | ||
| 433 | 433 | } |
| 434 | 434 | |
| 435 | 435 | fs.copyFileSync(pathToOriginalFile, pathToRenamedFile); |
| 436 | 436 | fs.rmSyncunlinkSync(pathToOriginalFile); |
| 437 | 437 | console.info('Successfully renamed.'); |
| 438 | 438 | return response.send({ ok: true, sanitizedFileName }); |
| 439 | 439 | }); |
| @@ -449,7 +449,7 @@ router.post('/delete', validateAvatarUrlMiddleware, function (request, response) | ||
| 449 | 449 | return response.sendStatus(400); |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | fs.rmSyncunlinkSync(filePath); |
| 453 | 453 | console.info(`Deleted chat file: ${filePath}`); |
| 454 | 454 | return response.send('ok'); |
| 455 | 455 | }); |
| @@ -665,7 +665,7 @@ router.post('/group/delete', (request, response) => { | ||
| 665 | 665 | const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`); |
| 666 | 666 | |
| 667 | 667 | if (fs.existsSync(pathToFile)) { |
| 668 | 668 | fs.rmSyncunlinkSync(pathToFile); |
| 669 | 669 | return response.send({ ok: true }); |
| 670 | 670 | } |
| 671 | 671 | |
| @@ -66,7 +66,7 @@ router.post('/delete', async (request, response) => { | ||
| 66 | 66 | return response.status(404).send('File not found'); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | fs.rmSyncunlinkSync(pathToDelete); |
| 70 | 70 | console.info(`Deleted file: ${request.body.path} from ${request.user.profile.handle}`); |
| 71 | 71 | return response.sendStatus(200); |
| 72 | 72 | } catch (error) { |
| @@ -117,7 +117,7 @@ router.post('/delete', async (request, response) => { | ||
| 117 | 117 | const pathToFile = path.join(request.user.directories.groupChats, `${id}.jsonl`); |
| 118 | 118 | |
| 119 | 119 | if (fs.existsSync(pathToFile)) { |
| 120 | 120 | fs.rmSyncunlinkSync(pathToFile); |
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | } |
| @@ -126,7 +126,7 @@ router.post('/delete', async (request, response) => { | ||
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | if (fs.existsSync(pathToGroup)) { |
| 129 | 129 | fs.rmSyncunlinkSync(pathToGroup); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | return response.send({ ok: true }); |
| @@ -234,7 +234,7 @@ router.post('/transcribe-audio', async (request, response) => { | ||
| 234 | 234 | return response.status(500).send(text); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | fs.rmSyncunlinkSync(request.file.path); |
| 238 | 238 | const data = await result.json(); |
| 239 | 239 | console.debug('OpenAI transcription response', data); |
| 240 | 240 | return response.json(data); |
| @@ -124,7 +124,7 @@ router.post('/delete-openai', function (request, response) { | ||
| 124 | 124 | const pathToFile = path.join(request.user.directories.openAI_Settings, `${name}.json`); |
| 125 | 125 | |
| 126 | 126 | if (fs.existsSync(pathToFile)) { |
| 127 | 127 | fs.rmSyncunlinkSync(pathToFile); |
| 128 | 128 | return response.send({ ok: true }); |
| 129 | 129 | } |
| 130 | 130 | |
| @@ -165,7 +165,7 @@ router.post('/delete', async (request, response) => { | ||
| 165 | 165 | // Remove existing sprite with the same label |
| 166 | 166 | for (const file of files) { |
| 167 | 167 | if (path.parse(file).name === spriteName) { |
| 168 | 168 | fs.rmSyncunlinkSync(path.join(spritesPath, file)); |
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
| @@ -206,7 +206,7 @@ router.post('/upload-zip', async (request, response) => { | ||
| 206 | 206 | const existingFile = files.find(file => path.parse(file).name === path.parse(filename).name); |
| 207 | 207 | |
| 208 | 208 | if (existingFile) { |
| 209 | 209 | fs.rmSyncunlinkSync(path.join(spritesPath, existingFile)); |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | // Write sprite buffer to disk |
| @@ -215,7 +215,7 @@ router.post('/upload-zip', async (request, response) => { | ||
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | // Remove uploaded ZIP file |
| 218 | 218 | fs.rmSyncunlinkSync(spritePackPath); |
| 219 | 219 | return response.send({ count: sprites.length }); |
| 220 | 220 | } catch (error) { |
| 221 | 221 | console.error(error); |
| @@ -251,7 +251,7 @@ router.post('/upload', async (request, response) => { | ||
| 251 | 251 | // Remove existing sprite with the same label |
| 252 | 252 | for (const file of files) { |
| 253 | 253 | if (path.parse(file).name === spriteName) { |
| 254 | 254 | fs.rmSyncunlinkSync(path.join(spritesPath, file)); |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| @@ -261,7 +261,7 @@ router.post('/upload', async (request, response) => { | ||
| 261 | 261 | // Copy uploaded file to sprites folder |
| 262 | 262 | fs.cpSync(spritePath, pathToFile); |
| 263 | 263 | // Remove uploaded file |
| 264 | 264 | fs.rmSyncunlinkSync(spritePath); |
| 265 | 265 | return response.sendStatus(200); |
| 266 | 266 | } catch (error) { |
| 267 | 267 | console.error(error); |
| @@ -29,7 +29,7 @@ router.post('/delete', function (request, response) { | ||
| 29 | 29 | console.error('Theme file not found:', filename); |
| 30 | 30 | return response.sendStatus(404); |
| 31 | 31 | } |
| 32 | 32 | fs.rmSyncunlinkSync(filename); |
| 33 | 33 | return response.sendStatus(200); |
| 34 | 34 | } catch (error) { |
| 35 | 35 | console.error(error); |
| @@ -75,7 +75,7 @@ export function invalidateThumbnail(directories, type, file) { | ||
| 75 | 75 | const pathToThumbnail = path.join(folder, file); |
| 76 | 76 | |
| 77 | 77 | if (fs.existsSync(pathToThumbnail)) { |
| 78 | 78 | fs.rmSyncunlinkSync(pathToThumbnail); |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | |
| @@ -57,7 +57,7 @@ router.post('/delete', (request, response) => { | ||
| 57 | 57 | throw new Error(`World info file ${filename} doesn't exist.`); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | fs.rmSyncunlinkSync(pathToWorldInfo); |
| 61 | 61 | |
| 62 | 62 | return response.sendStatus(200); |
| 63 | 63 | }); |
| @@ -435,7 +435,7 @@ export function removeOldBackups(directory, prefix, limit = null) { | ||
| 435 | 435 | break; |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | 438 | fs.rmSyncunlinkSync(oldest); |
| 439 | 439 | } |
| 440 | 440 | } |
| 441 | 441 | } |