2014-06-03 17:05:25 +04:00
|
|
|
// # DB API
|
|
|
|
// API for DB operations
|
2014-12-11 00:50:00 +03:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-12-11 00:50:00 +03:00
|
|
|
dataExport = require('../data/export'),
|
|
|
|
importer = require('../data/importer'),
|
|
|
|
models = require('../models'),
|
|
|
|
errors = require('../errors'),
|
2014-12-10 16:28:16 +03:00
|
|
|
utils = require('./utils'),
|
2015-10-20 15:11:49 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-12-11 00:50:00 +03:00
|
|
|
|
2013-12-30 03:52:55 +04:00
|
|
|
api = {},
|
2015-10-20 15:11:49 +03:00
|
|
|
docName = 'db',
|
2014-12-11 00:50:00 +03:00
|
|
|
db;
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-12-30 03:52:55 +04:00
|
|
|
api.settings = require('./settings');
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## DB API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
2013-10-09 12:52:58 +04:00
|
|
|
db = {
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Export Content
|
|
|
|
* Generate the JSON to export
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Ghost Export JSON format
|
|
|
|
*/
|
2014-09-10 08:06:24 +04:00
|
|
|
exportContent: function (options) {
|
2015-10-20 15:11:49 +03:00
|
|
|
var tasks = [];
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
options = options || {};
|
2014-04-08 17:40:33 +04:00
|
|
|
|
2014-02-27 19:48:38 +04:00
|
|
|
// Export data, otherwise send error 500
|
2015-10-20 15:11:49 +03:00
|
|
|
function exportContent() {
|
2014-09-10 08:06:24 +04:00
|
|
|
return dataExport().then(function (exportedData) {
|
|
|
|
return {db: [exportedData]};
|
|
|
|
}).catch(function (error) {
|
|
|
|
return Promise.reject(new errors.InternalServerError(error.message || error));
|
2014-04-08 17:40:33 +04:00
|
|
|
});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.handlePermissions(docName, 'exportContent'),
|
|
|
|
exportContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-10-09 12:52:58 +04:00
|
|
|
},
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Import Content
|
|
|
|
* Import posts, tags etc from a JSON blob
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Success
|
|
|
|
*/
|
2014-09-10 08:06:24 +04:00
|
|
|
importContent: function (options) {
|
2015-10-20 15:11:49 +03:00
|
|
|
var tasks = [];
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
options = options || {};
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2015-10-20 15:11:49 +03:00
|
|
|
function validate(options) {
|
|
|
|
// Check if a file was provided
|
|
|
|
if (!utils.checkFileExists(options, 'importfile')) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.ValidationError(i18n.t('errors.api.db.selectFileToImport')));
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file is valid
|
|
|
|
if (!utils.checkFileIsValid(options.importfile, importer.getTypes(), importer.getExtensions())) {
|
|
|
|
return Promise.reject(new errors.UnsupportedMediaTypeError(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('errors.api.db.unsupportedFile') +
|
2015-10-20 15:11:49 +03:00
|
|
|
_.reduce(importer.getExtensions(), function (memo, ext) {
|
|
|
|
return memo ? memo + ', ' + ext : ext;
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2015-10-20 15:11:49 +03:00
|
|
|
return options;
|
2014-12-10 16:28:16 +03:00
|
|
|
}
|
2014-05-07 09:28:51 +04:00
|
|
|
|
2015-10-20 15:11:49 +03:00
|
|
|
function importContent(options) {
|
2014-12-11 00:50:00 +03:00
|
|
|
return importer.importFromFile(options.importfile)
|
2016-02-04 11:23:43 +03:00
|
|
|
.then(function () {
|
|
|
|
api.settings.updateSettingsCache();
|
|
|
|
})
|
2014-12-11 00:50:00 +03:00
|
|
|
.return({db: []});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
validate,
|
|
|
|
utils.handlePermissions(docName, 'importContent'),
|
|
|
|
importContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-12-25 04:05:20 +04:00
|
|
|
},
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Delete All Content
|
|
|
|
* Remove all posts and tags
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Success
|
|
|
|
*/
|
2014-09-10 08:06:24 +04:00
|
|
|
deleteAllContent: function (options) {
|
2015-10-20 15:11:49 +03:00
|
|
|
var tasks;
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
options = options || {};
|
2014-04-08 17:40:33 +04:00
|
|
|
|
2015-10-20 15:11:49 +03:00
|
|
|
function deleteContent() {
|
2014-12-11 00:50:00 +03:00
|
|
|
return Promise.resolve(models.deleteAllContent())
|
2014-09-10 08:06:24 +04:00
|
|
|
.return({db: []})
|
2014-08-17 10:17:23 +04:00
|
|
|
.catch(function (error) {
|
|
|
|
return Promise.reject(new errors.InternalServerError(error.message || error));
|
2014-04-08 17:40:33 +04:00
|
|
|
});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.handlePermissions(docName, 'deleteAllContent'),
|
|
|
|
deleteContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-11-01 16:12:01 +04:00
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
};
|
|
|
|
|
2014-01-29 08:18:00 +04:00
|
|
|
module.exports = db;
|