Fixed fs error during theme install deleting active theme (#12688)

closes https://github.com/TryGhost/Ghost/issues/12506

- adds an error handler that will rename the backup folder to the original name if the newly uploaded theme wasn't saved successfully
This commit is contained in:
Kevin Ansfield 2021-02-23 08:49:48 +00:00 committed by Daniel Lockyer
parent 50f40f3e1c
commit ae506097f4
No known key found for this signature in database
GPG Key ID: FFBC6FA2A6F6ABC1
1 changed files with 16 additions and 0 deletions

View File

@ -48,6 +48,7 @@ module.exports = {
}
let checkedTheme;
let renamedExisting = false;
return validate.checkSafe(zip, true)
.then((_checkedTheme) => {
@ -58,6 +59,7 @@ module.exports = {
.then((themeExists) => {
// CASE: move the existing theme to a backup folder
if (themeExists) {
renamedExisting = true;
return getStorage().rename(shortName, backupName);
}
})
@ -86,6 +88,20 @@ module.exports = {
theme: toJSON(shortName, checkedTheme)
};
})
.catch((error) => {
// restore backup if we renamed an existing theme but saving failed
if (renamedExisting) {
return getStorage().exists(shortName).then((themeExists) => {
if (!themeExists) {
return getStorage().rename(backupName, shortName).then(() => {
throw error;
});
}
});
}
throw error;
})
.finally(() => {
// @TODO: we should probably do this as part of saving the theme
// CASE: remove extracted dir from gscan happens in background