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 GitHub
parent 08e1268aed
commit 519faf8749
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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