Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Image Backups API endpoint #14659

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ projectFilesBackup
/content/media/**/*
/content/files/**/*
/content/public/*
/content/backups/*
/content/adapters/storage/**/*
/content/adapters/scheduling/**/*
!/content/themes/casper
Expand Down
3 changes: 0 additions & 3 deletions config.development.json

This file was deleted.

3 changes: 0 additions & 3 deletions content/logs/README.md

This file was deleted.

11 changes: 11 additions & 0 deletions core/server/api/canary/backups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const backupsServices = require('../../services/backups');

module.exports = {
docName: 'backups',
download: {
permissions: false,
query() {
return backupsServices.api.exporter();
}
}
};
4 changes: 4 additions & 0 deletions core/server/api/canary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = {
return shared.pipeline(require('./identities'), localUtils);
},

get backups() {
return shared.pipeline(require('./backups'), localUtils);
},

get integrations() {
return shared.pipeline(require('./integrations'), localUtils);
},
Expand Down
9 changes: 9 additions & 0 deletions core/server/api/canary/utils/serializers/output/backups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const debug = require('@tryghost/debug');

module.exports = {
all(data, apiConfig, frame) {
debug('all');

frame.response = data;
}
};
4 changes: 4 additions & 0 deletions core/server/api/canary/utils/serializers/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
return require('./default');
},

get backups() {
return require('./backups');
},

get authentication() {
return require('./authentication');
},
Expand Down
53 changes: 53 additions & 0 deletions core/server/services/backups/BackupsExporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const {compress} = require('@tryghost/zip');
const path = require('path');
const config = require('../../../shared/config');
const fs = require('fs-extra');

/**
* Prototype for exporting images in bulk
*/

class BackupsExporter {
getDirectories() {
return {
images: config.getContentPath('images'),
backups: config.getContentPath('backups')
};
}
/**
*
* @TODO: Perhaps let it multithread to avoid http timeouts and memory issues on big requests?
* Maybe add cases later for other types for backups?
*/
serve() {
const self = this;
return function downloadBackup(req, res, next) {
const backupsPath = self.getDirectories().backups;
const imagesPath = self.getDirectories().images;
const zipPath = path.join(backupsPath, 'images.zip');
let stream;
fs.ensureDir(backupsPath)
.then(async function () {
return await compress(imagesPath, zipPath);
})
.then(function (result) {
res.set({
'Content-disposition': 'attachment; filename={backups}.zip'.replace('{backups}', 'images'),
'Content-Type': 'application/zip',
'Content-Length': result.size
});
stream = fs.createReadStream(zipPath);
stream.pipe(res);
})
.catch(function (err) {
next(err);
})
.finally(function () {
return;
});
};
}
}

module.exports = BackupsExporter;

14 changes: 14 additions & 0 deletions core/server/services/backups/backups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const BackupsExporter = require('./BackupsExporter');

let backupsExporter;

const getStorage = () => {
backupsExporter = backupsExporter || new BackupsExporter();
return backupsExporter;
};

module.exports = {
getZip: async () => {
return await getStorage().serve();
}
};
10 changes: 10 additions & 0 deletions core/server/services/backups/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {getZip} = require('./backups');

module.exports = {
/**
* Methods used in the API
*/
api: {
exporter: getZip
}
};
4 changes: 4 additions & 0 deletions core/server/web/api/canary/admin/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ module.exports = function apiRoutes() {
http(api.files.upload)
);

// ## backups
// @TODO make images dynamic to allow multiple use cases in future
router.get('/backups/images/download', mw.authAdminApi, http(api.backups.download));

// ## Invites
router.get('/invites', mw.authAdminApi, http(api.invites.browse));
router.get('/invites/:id', mw.authAdminApi, http(api.invites.read));
Expand Down
2 changes: 2 additions & 0 deletions core/shared/config/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const getContentPath = function getContentPath(type) {
return path.join(this.get('paths:contentPath'), 'settings/');
case 'public':
return path.join(this.get('paths:contentPath'), 'public/');
case 'backups':
return path.join(this.get('paths:contentPath'), 'backups/');
default:
// new Error is allowed here, as we do not want config to depend on @tryghost/error
// @TODO: revisit this decision when @tryghost/error is no longer dependent on all of ghost-ignition
Expand Down