mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-21 14:40:48 +01:00
Add temp folder into assets, file are download into this temp file and moved only when complete. Allow to handle fail download.
This commit is contained in:
parent
017df860e5
commit
46964b1b99
0
public/assets/temp/.placeholder
Normal file
0
public/assets/temp/.placeholder
Normal file
@ -291,8 +291,14 @@ async function moduleWorker() {
|
|||||||
// 1.2) Switched chat
|
// 1.2) Switched chat
|
||||||
if (currentCharacterBGM !== newCharacter) {
|
if (currentCharacterBGM !== newCharacter) {
|
||||||
currentCharacterBGM = newCharacter;
|
currentCharacterBGM = newCharacter;
|
||||||
updateBGM();
|
try {
|
||||||
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
await updateBGM();
|
||||||
|
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
||||||
|
}
|
||||||
|
catch(error){
|
||||||
|
console.debug(DEBUG_PREFIX,"Error while trying to update BGM character, will try again");
|
||||||
|
currentCharacterBGM = null
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,10 +313,16 @@ async function moduleWorker() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
try {
|
||||||
currentExpressionBGM = newExpression;
|
await updateBGM();
|
||||||
console.debug(DEBUG_PREFIX,"(SOLO) Updated current character expression to",currentExpressionBGM,"cooldown",cooldownBGM);
|
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
||||||
updateBGM();
|
currentExpressionBGM = newExpression;
|
||||||
|
console.debug(DEBUG_PREFIX,"(SOLO) Updated current character expression to",currentExpressionBGM,"cooldown",cooldownBGM);
|
||||||
|
}
|
||||||
|
catch(error){
|
||||||
|
console.debug(DEBUG_PREFIX,"Error while trying to update BGM expression, will try again");
|
||||||
|
currentCharacterBGM = null
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,11 +352,18 @@ async function moduleWorker() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
try {
|
||||||
currentCharacterBGM = newCharacter;
|
currentCharacterBGM = newCharacter;
|
||||||
currentExpressionBGM = FALLBACK_EXPRESSION;
|
await updateBGM();
|
||||||
updateBGM();
|
cooldownBGM = extension_settings.audio.bgm_cooldown * 1000;
|
||||||
console.debug(DEBUG_PREFIX,"(GROUP) Updated current character BGM to",currentExpressionBGM,"cooldown",cooldownBGM);
|
currentCharacterBGM = newCharacter;
|
||||||
|
currentExpressionBGM = FALLBACK_EXPRESSION;
|
||||||
|
console.debug(DEBUG_PREFIX,"(GROUP) Updated current character BGM to",currentExpressionBGM,"cooldown",cooldownBGM);
|
||||||
|
}
|
||||||
|
catch(error){
|
||||||
|
console.debug(DEBUG_PREFIX,"Error while trying to update BGM group, will try again");
|
||||||
|
currentCharacterBGM = null
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
30
server.js
30
server.js
@ -5023,6 +5023,8 @@ app.post('/get_assets', jsonParser, async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const folder of folders) {
|
for (const folder of folders) {
|
||||||
|
if (folder == "temp")
|
||||||
|
continue;
|
||||||
const files = fs.readdirSync(path.join(folderPath,folder))
|
const files = fs.readdirSync(path.join(folderPath,folder))
|
||||||
.filter(filename => {
|
.filter(filename => {
|
||||||
return filename != ".placeholder";
|
return filename != ".placeholder";
|
||||||
@ -5083,19 +5085,33 @@ app.post('/asset_download', jsonParser, async (request, response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const safe_input = path.normalize(inputFilename).replace(/^(\.\.(\/|\\|$))+/, '');
|
const safe_input = path.normalize(inputFilename).replace(/^(\.\.(\/|\\|$))+/, '');
|
||||||
|
const temp_path = path.join(directories.assets, "temp", safe_input)
|
||||||
const file_path = path.join(directories.assets, category, safe_input)
|
const file_path = path.join(directories.assets, category, safe_input)
|
||||||
console.debug("Request received to download", url,"to",file_path);
|
console.debug("Request received to download", url,"to",file_path);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const downloadFile = (async (url, file_path) => {
|
// Download to temp
|
||||||
const res = await fetch(url);
|
const downloadFile = (async (url, temp_path) => {
|
||||||
const destination = path.resolve(file_path);
|
const res = await fetch(url);
|
||||||
const fileStream = fs.createWriteStream(destination, { flags: 'wx' });
|
const destination = path.resolve(temp_path);
|
||||||
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
// Delete if previous download failed
|
||||||
console.debug("Download finished, file saved to",file_path);
|
if (fs.existsSync(temp_path)) {
|
||||||
|
fs.unlink(temp_path, (err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const fileStream = fs.createWriteStream(destination, { flags: 'wx' });
|
||||||
|
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
||||||
});
|
});
|
||||||
|
|
||||||
await downloadFile(url, file_path);
|
await downloadFile(url, temp_path);
|
||||||
|
|
||||||
|
// Move into asset place
|
||||||
|
console.debug("Download finished, moving file from",temp_path,"to",file_path);
|
||||||
|
fs.rename(temp_path,file_path, (err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log('Rename complete!');
|
||||||
|
});
|
||||||
response.sendStatus(200);
|
response.sendStatus(200);
|
||||||
}
|
}
|
||||||
catch(error) {
|
catch(error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user