0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Basic implementation of backup retreival from file

This commit is contained in:
Nazar Gargol 2019-12-16 18:34:26 +07:00 committed by Daniel Lockyer
parent 49983e799c
commit 52635f1aa8
3 changed files with 33 additions and 4 deletions

View file

@ -31,7 +31,8 @@ module.exports = {
exportContent: {
options: [
'include'
'include',
'filename'
],
validation: {
options: {
@ -47,7 +48,17 @@ module.exports = {
}
},
permissions: true,
query(frame) {
async query(frame) {
if (frame.options.filename) {
let backup = await dbBackup.readBackup(frame.options.filename);
if (!backup) {
return new common.errors.NotFoundError();
}
return backup;
}
return Promise.resolve()
.then(() => exporter.doExport({include: frame.options.withRelated}))
.catch((err) => {

View file

@ -1,3 +1,4 @@
const path = require('path');
const Promise = require('bluebird');
const common = require('../../lib/common');
const dbBackup = require('../../data/db/backup');
@ -122,7 +123,9 @@ module.exports = {
},
permissions: true,
async query(frame) {
const filename = await dbBackup.backup();
const backupPath = await dbBackup.backup();
const parsedFileName = path.parse(backupPath);
const filename = `${parsedFileName.name}${parsedFileName.ext}`;
return models.Base.transaction((t) => {
frame.options.transacting = t;

View file

@ -17,6 +17,20 @@ writeExportFile = function writeExportFile(exportResult) {
return fs.writeFile(filename, JSON.stringify(exportResult.data)).return(filename);
};
const readBackup = async (filename) => {
// TODO: prevent from directory traversal - need to sanitize the filename probably on validation layer
var backupPath = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', filename));
const exists = await fs.pathExists(backupPath);
if (exists) {
const backup = await fs.readFile(backupPath);
return JSON.parse(backup);
} else {
return null;
}
};
/**
* ## Backup
* does an export, and stores this in a local file
@ -40,5 +54,6 @@ backup = function backup(options) {
};
module.exports = {
backup
backup,
readBackup
};