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