From 519faf874977291a4d78ee8e684b2b8f8f98857b Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Tue, 23 Feb 2021 08:49:48 +0000 Subject: [PATCH] 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 --- core/frontend/services/themes/storage.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/frontend/services/themes/storage.js b/core/frontend/services/themes/storage.js index 31b9522035..8429cc727d 100644 --- a/core/frontend/services/themes/storage.js +++ b/core/frontend/services/themes/storage.js @@ -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