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
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
30
server.js
30
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) {
|
||||
|
|
Loading…
Reference in New Issue