Moved settings#upload method out of settings controller

This commit is contained in:
Nazar Gargol 2019-06-20 13:19:22 +02:00
parent 58a077564f
commit c3b14f82fd
4 changed files with 79 additions and 58 deletions

View File

@ -0,0 +1,70 @@
const moment = require('moment-timezone');
const fs = require('fs-extra');
const path = require('path');
const urlService = require('../url');
const common = require('../../../server/lib/common');
const config = require('../../../server/config');
const activate = (filePath) => {
const settingsPath = config.getContentPath('settings');
const backupRoutesPath = path.join(settingsPath, `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`);
return fs.copy(`${settingsPath}/routes.yaml`, backupRoutesPath)
.then(() => {
return fs.copy(filePath, `${settingsPath}/routes.yaml`);
})
.then(() => {
urlService.resetGenerators({releaseResourcesOnly: true});
})
.then(() => {
const siteApp = require('../../../server/web/site/app');
const bringBackValidRoutes = () => {
urlService.resetGenerators({releaseResourcesOnly: true});
return fs.copy(backupRoutesPath, `${settingsPath}/routes.yaml`)
.then(() => {
return siteApp.reload();
});
};
try {
siteApp.reload();
} catch (err) {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
}
let tries = 0;
function isBlogRunning() {
return Promise.delay(1000)
.then(() => {
if (!urlService.hasFinished()) {
if (tries > 5) {
throw new common.errors.InternalServerError({
message: 'Could not load routes.yaml file.'
});
}
tries = tries + 1;
return isBlogRunning();
}
});
}
return isBlogRunning()
.catch((err) => {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
});
});
};
module.exports.activate = activate;
// module.exports.serve = serve;

View File

@ -17,5 +17,9 @@ module.exports = {
get TaxonomyRouter() {
return require('./TaxonomyRouter');
},
get settings() {
return require('./SettingsHandler');
}
};

View File

@ -96,6 +96,9 @@ module.exports = {
} else {
return engineDefaults['ghost-api'];
}
},
delete: function (){
},
activate: function activate(loadedTheme, checkedTheme, error) {
// no need to check the score, activation should be used in combination with validate.check

View File

@ -1,11 +1,10 @@
const Promise = require('bluebird');
const _ = require('lodash');
const moment = require('moment-timezone');
const fs = require('fs-extra');
const path = require('path');
const config = require('../../config');
const models = require('../../models');
const urlService = require('../../../frontend/services/url');
const frontendRouting = require('../../../frontend/services/routing');
const common = require('../../lib/common');
const settingsCache = require('../../services/settings/cache');
@ -151,62 +150,7 @@ module.exports = {
method: 'edit'
},
query(frame) {
const backupRoutesPath = path.join(config.getContentPath('settings'), `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`);
return fs.copy(`${config.getContentPath('settings')}/routes.yaml`, backupRoutesPath)
.then(() => {
return fs.copy(frame.file.path, `${config.getContentPath('settings')}/routes.yaml`);
})
.then(() => {
urlService.resetGenerators({releaseResourcesOnly: true});
})
.then(() => {
const siteApp = require('../../web/site/app');
const bringBackValidRoutes = () => {
urlService.resetGenerators({releaseResourcesOnly: true});
return fs.copy(backupRoutesPath, `${config.getContentPath('settings')}/routes.yaml`)
.then(() => {
return siteApp.reload();
});
};
try {
siteApp.reload();
} catch (err) {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
}
let tries = 0;
function isBlogRunning() {
return Promise.delay(1000)
.then(() => {
if (!urlService.hasFinished()) {
if (tries > 5) {
throw new common.errors.InternalServerError({
message: 'Could not load routes.yaml file.'
});
}
tries = tries + 1;
return isBlogRunning();
}
});
}
return isBlogRunning()
.catch((err) => {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
});
});
return frontendRouting.settings.activate(frame.file.path);
}
},