Merge pull request #3940 from wickedcode01/bug-fixed

Fix the issue where deleting files on Windows may cause the application to crash.
This commit is contained in:
Cohee
2025-05-01 17:00:11 +03:00
committed by GitHub
15 changed files with 29 additions and 29 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

@@ -28,7 +28,7 @@ router.post('/delete', getFileNameValidationFunction('avatar'), function (reques
const fileName = path.join(request.user.directories.avatars, sanitize(request.body.avatar));
if (fs.existsSync(fileName)) {
fs.rmSync(fileName);
fs.unlinkSync(fileName);
return response.send({ result: 'ok' });
}
@@ -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

@@ -726,7 +726,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);
@@ -760,7 +760,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');
@@ -1001,7 +1001,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 });
@@ -1156,7 +1156,7 @@ router.post('/delete', validateAvatarUrlMiddleware, async function (request, res
return response.sendStatus(400);
}
fs.rmSync(avatarPath);
fs.unlinkSync(avatarPath);
invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url);
let dir_name = (request.body.avatar_url.replace('.png', ''));

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 });
});
@@ -449,7 +449,7 @@ router.post('/delete', validateAvatarUrlMiddleware, function (request, response)
return response.sendStatus(400);
}
fs.rmSync(filePath);
fs.unlinkSync(filePath);
console.info(`Deleted chat file: ${filePath}`);
return response.send('ok');
});
@@ -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

@@ -29,7 +29,7 @@ router.post('/delete', function (request, response) {
console.error('Theme file not found:', filename);
return response.sendStatus(404);
}
fs.rmSync(filename);
fs.unlinkSync(filename);
return response.sendStatus(200);
} catch (error) {
console.error(error);

View File

@@ -75,7 +75,7 @@ export function invalidateThumbnail(directories, type, file) {
const pathToThumbnail = path.join(folder, file);
if (fs.existsSync(pathToThumbnail)) {
fs.rmSync(pathToThumbnail);
fs.unlinkSync(pathToThumbnail);
}
}

View File

@@ -57,7 +57,7 @@ router.post('/delete', (request, response) => {
throw new Error(`World info file ${filename} doesn't exist.`);
}
fs.rmSync(pathToWorldInfo);
fs.unlinkSync(pathToWorldInfo);
return response.sendStatus(200);
});

View File

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