From 46964b1b995eed1ee2bc6a310a1fb26be1833ad3 Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Thu, 24 Aug 2023 02:34:06 +0200 Subject: [PATCH] Add temp folder into assets, file are download into this temp file and moved only when complete. Allow to handle fail download. --- public/assets/temp/.placeholder | 0 public/scripts/extensions/audio/index.js | 41 +++++++++++++++++------- server.js | 30 +++++++++++++---- 3 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 public/assets/temp/.placeholder diff --git a/public/assets/temp/.placeholder b/public/assets/temp/.placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/public/scripts/extensions/audio/index.js b/public/scripts/extensions/audio/index.js index 4c73615fd..94d6cca06 100644 --- a/public/scripts/extensions/audio/index.js +++ b/public/scripts/extensions/audio/index.js @@ -291,8 +291,14 @@ async function moduleWorker() { // 1.2) Switched chat if (currentCharacterBGM !== newCharacter) { currentCharacterBGM = newCharacter; - updateBGM(); - cooldownBGM = extension_settings.audio.bgm_cooldown * 1000; + try { + 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; } @@ -307,10 +313,16 @@ async function moduleWorker() { return; } - cooldownBGM = extension_settings.audio.bgm_cooldown * 1000; - currentExpressionBGM = newExpression; - console.debug(DEBUG_PREFIX,"(SOLO) Updated current character expression to",currentExpressionBGM,"cooldown",cooldownBGM); - updateBGM(); + try { + await updateBGM(); + cooldownBGM = extension_settings.audio.bgm_cooldown * 1000; + 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; } @@ -340,11 +352,18 @@ async function moduleWorker() { return; } - cooldownBGM = extension_settings.audio.bgm_cooldown * 1000; - currentCharacterBGM = newCharacter; - currentExpressionBGM = FALLBACK_EXPRESSION; - updateBGM(); - console.debug(DEBUG_PREFIX,"(GROUP) Updated current character BGM to",currentExpressionBGM,"cooldown",cooldownBGM); + try { + currentCharacterBGM = newCharacter; + await updateBGM(); + cooldownBGM = extension_settings.audio.bgm_cooldown * 1000; + 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; } diff --git a/server.js b/server.js index 80e35b39a..0ca8af64c 100644 --- a/server.js +++ b/server.js @@ -5023,6 +5023,8 @@ app.post('/get_assets', jsonParser, async (request, response) => { }); for (const folder of folders) { + if (folder == "temp") + continue; const files = fs.readdirSync(path.join(folderPath,folder)) .filter(filename => { return filename != ".placeholder"; @@ -5083,19 +5085,33 @@ app.post('/asset_download', jsonParser, async (request, response) => { } 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) console.debug("Request received to download", url,"to",file_path); try { - const downloadFile = (async (url, file_path) => { - const res = await fetch(url); - const destination = path.resolve(file_path); - const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); - await finished(Readable.fromWeb(res.body).pipe(fileStream)); - console.debug("Download finished, file saved to",file_path); + // Download to temp + const downloadFile = (async (url, temp_path) => { + const res = await fetch(url); + const destination = path.resolve(temp_path); + // Delete if previous download failed + 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); } catch(error) {