2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const os = require('os');
|
|
|
|
const glob = require('glob');
|
|
|
|
const uuid = require('uuid');
|
|
|
|
const {extract} = require('@tryghost/zip');
|
2020-08-11 20:44:21 +03:00
|
|
|
const {pipeline, sequence} = require('@tryghost/promise');
|
2021-04-27 16:18:04 +03:00
|
|
|
const i18n = require('../../lib/common/i18n');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../../shared/logging');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
const ImageHandler = require('./handlers/image');
|
|
|
|
const JSONHandler = require('./handlers/json');
|
|
|
|
const MarkdownHandler = require('./handlers/markdown');
|
|
|
|
const ImageImporter = require('./importers/image');
|
|
|
|
const DataImporter = require('./importers/data');
|
|
|
|
|
|
|
|
// Glob levels
|
|
|
|
const ROOT_ONLY = 0;
|
|
|
|
|
|
|
|
const ROOT_OR_SINGLE_DIR = 1;
|
|
|
|
const ALL_DIRS = 2;
|
|
|
|
let defaults;
|
2014-12-11 00:50:00 +03:00
|
|
|
|
|
|
|
defaults = {
|
|
|
|
extensions: ['.zip'],
|
2016-08-18 22:25:51 +03:00
|
|
|
contentTypes: ['application/zip', 'application/x-zip-compressed'],
|
2014-12-29 21:33:47 +03:00
|
|
|
directories: []
|
2014-12-11 00:50:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function ImportManager() {
|
2014-12-14 14:31:00 +03:00
|
|
|
this.importers = [ImageImporter, DataImporter];
|
2014-12-21 02:48:13 +03:00
|
|
|
this.handlers = [ImageHandler, JSONHandler, MarkdownHandler];
|
2019-01-30 04:28:16 +03:00
|
|
|
// Keep track of file to cleanup at the end
|
|
|
|
this.fileToDelete = null;
|
2014-12-11 00:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A number, or a string containing a number.
|
|
|
|
* @typedef {Object} ImportData
|
|
|
|
* @property [Object] data
|
|
|
|
* @property [Array] images
|
|
|
|
*/
|
|
|
|
|
|
|
|
_.extend(ImportManager.prototype, {
|
|
|
|
/**
|
|
|
|
* Get an array of all the file extensions for which we have handlers
|
2014-12-29 21:33:47 +03:00
|
|
|
* @returns {string[]}
|
2014-12-11 00:50:00 +03:00
|
|
|
*/
|
|
|
|
getExtensions: function () {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.flatten(_.union(_.map(this.handlers, 'extensions'), defaults.extensions));
|
2014-12-11 00:50:00 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get an array of all the mime types for which we have handlers
|
2014-12-29 21:33:47 +03:00
|
|
|
* @returns {string[]}
|
2014-12-11 00:50:00 +03:00
|
|
|
*/
|
2016-08-18 22:25:51 +03:00
|
|
|
getContentTypes: function () {
|
|
|
|
return _.flatten(_.union(_.map(this.handlers, 'contentTypes'), defaults.contentTypes));
|
2014-12-11 00:50:00 +03:00
|
|
|
},
|
|
|
|
/**
|
2014-12-29 21:33:47 +03:00
|
|
|
* Get an array of directories for which we have handlers
|
|
|
|
* @returns {string[]}
|
2014-12-11 00:50:00 +03:00
|
|
|
*/
|
2014-12-29 21:33:47 +03:00
|
|
|
getDirectories: function () {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.flatten(_.union(_.map(this.handlers, 'directories'), defaults.directories));
|
2014-12-29 21:33:47 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Convert items into a glob string
|
|
|
|
* @param {String[]} items
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
getGlobPattern: function (items) {
|
|
|
|
return '+(' + _.reduce(items, function (memo, ext) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return memo !== '' ? memo + '|' + ext : ext;
|
2014-12-11 00:50:00 +03:00
|
|
|
}, '') + ')';
|
|
|
|
},
|
|
|
|
/**
|
2014-12-29 21:33:47 +03:00
|
|
|
* @param {String[]} extensions
|
|
|
|
* @param {Number} level
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
getExtensionGlob: function (extensions, level) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const prefix = level === ALL_DIRS ? '**/*' :
|
2014-12-29 21:33:47 +03:00
|
|
|
(level === ROOT_OR_SINGLE_DIR ? '{*/*,*}' : '*');
|
|
|
|
|
|
|
|
return prefix + this.getGlobPattern(extensions);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String[]} directories
|
|
|
|
* @param {Number} level
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
getDirectoryGlob: function (directories, level) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const prefix = level === ALL_DIRS ? '**/' :
|
2014-12-29 21:33:47 +03:00
|
|
|
(level === ROOT_OR_SINGLE_DIR ? '{*/,}' : '');
|
|
|
|
|
|
|
|
return prefix + this.getGlobPattern(directories);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Remove files after we're done (abstracted into a function for easier testing)
|
2014-12-11 00:50:00 +03:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2014-12-29 21:33:47 +03:00
|
|
|
cleanUp: function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const self = this;
|
2019-01-30 04:28:16 +03:00
|
|
|
|
|
|
|
if (self.fileToDelete === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.remove(self.fileToDelete, function (err) {
|
|
|
|
if (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
logging.error(new errors.GhostError({
|
2019-01-30 04:28:16 +03:00
|
|
|
err: err,
|
2020-05-22 21:22:20 +03:00
|
|
|
context: i18n.t('errors.data.importer.index.couldNotCleanUpFile.error'),
|
|
|
|
help: i18n.t('errors.data.importer.index.couldNotCleanUpFile.context')
|
2019-01-30 04:28:16 +03:00
|
|
|
}));
|
|
|
|
}
|
2014-12-29 21:33:47 +03:00
|
|
|
|
2019-01-30 04:28:16 +03:00
|
|
|
self.fileToDelete = null;
|
|
|
|
});
|
2014-12-11 00:50:00 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Return true if the given file is a Zip
|
|
|
|
* @returns Boolean
|
|
|
|
*/
|
|
|
|
isZip: function (ext) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.includes(defaults.extensions, ext);
|
2014-12-11 00:50:00 +03:00
|
|
|
},
|
2014-12-29 21:33:47 +03:00
|
|
|
/**
|
|
|
|
* Checks the content of a zip folder to see if it is valid.
|
|
|
|
* Importable content includes any files or directories which the handlers can process
|
|
|
|
* Importable content must be found either in the root, or inside one base directory
|
|
|
|
*
|
|
|
|
* @param {String} directory
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
isValidZip: function (directory) {
|
|
|
|
// Globs match content in the root or inside a single directory
|
2020-04-29 18:44:27 +03:00
|
|
|
const extMatchesBase = glob.sync(this.getExtensionGlob(this.getExtensions(), ROOT_OR_SINGLE_DIR), {cwd: directory});
|
|
|
|
|
|
|
|
const extMatchesAll = glob.sync(
|
|
|
|
this.getExtensionGlob(this.getExtensions(), ALL_DIRS), {cwd: directory}
|
|
|
|
);
|
|
|
|
|
|
|
|
const dirMatches = glob.sync(
|
|
|
|
this.getDirectoryGlob(this.getDirectories(), ROOT_OR_SINGLE_DIR), {cwd: directory}
|
|
|
|
);
|
|
|
|
|
|
|
|
const oldRoonMatches = glob.sync(this.getDirectoryGlob(['drafts', 'published', 'deleted'], ROOT_OR_SINGLE_DIR),
|
|
|
|
{cwd: directory});
|
2014-12-21 02:48:13 +03:00
|
|
|
|
|
|
|
// This is a temporary extra message for the old format roon export which doesn't work with Ghost
|
|
|
|
if (oldRoonMatches.length > 0) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.UnsupportedMediaTypeError({message: i18n.t('errors.data.importer.index.unsupportedRoonExport')});
|
2014-12-21 02:48:13 +03:00
|
|
|
}
|
2014-12-29 21:33:47 +03:00
|
|
|
|
|
|
|
// If this folder contains importable files or a content or images directory
|
|
|
|
if (extMatchesBase.length > 0 || (dirMatches.length > 0 && extMatchesAll.length > 0)) {
|
2015-01-04 00:11:40 +03:00
|
|
|
return true;
|
2014-12-29 21:33:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (extMatchesAll.length < 1) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.UnsupportedMediaTypeError({message: i18n.t('errors.data.importer.index.noContentToImport')});
|
2014-12-29 21:33:47 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.UnsupportedMediaTypeError({message: i18n.t('errors.data.importer.index.invalidZipStructure')});
|
2014-12-29 21:33:47 +03:00
|
|
|
},
|
2014-12-11 00:50:00 +03:00
|
|
|
/**
|
|
|
|
* Use the extract module to extract the given zip file to a temp directory & return the temp directory path
|
|
|
|
* @param {String} filePath
|
|
|
|
* @returns {Promise[]} Files
|
|
|
|
*/
|
|
|
|
extractZip: function (filePath) {
|
2019-01-30 04:28:16 +03:00
|
|
|
const tmpDir = path.join(os.tmpdir(), uuid.v4());
|
|
|
|
this.fileToDelete = tmpDir;
|
|
|
|
|
2020-04-15 15:23:45 +03:00
|
|
|
return extract(filePath, tmpDir).then(function () {
|
2014-12-11 00:50:00 +03:00
|
|
|
return tmpDir;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Use the handler extensions to get a globbing pattern, then use that to fetch all the files from the zip which
|
|
|
|
* are relevant to the given handler, and return them as a name and path combo
|
|
|
|
* @param {Object} handler
|
|
|
|
* @param {String} directory
|
|
|
|
* @returns [] Files
|
|
|
|
*/
|
|
|
|
getFilesFromZip: function (handler, directory) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const globPattern = this.getExtensionGlob(handler.extensions, ALL_DIRS);
|
2014-12-11 00:50:00 +03:00
|
|
|
return _.map(glob.sync(globPattern, {cwd: directory}), function (file) {
|
|
|
|
return {name: file, path: path.join(directory, file)};
|
|
|
|
});
|
|
|
|
},
|
2014-12-29 21:33:47 +03:00
|
|
|
/**
|
|
|
|
* Get the name of the single base directory if there is one, else return an empty string
|
|
|
|
* @param {String} directory
|
|
|
|
* @returns {Promise (String)}
|
|
|
|
*/
|
|
|
|
getBaseDirectory: function (directory) {
|
|
|
|
// Globs match root level only
|
2020-04-29 18:44:27 +03:00
|
|
|
const extMatches = glob.sync(this.getExtensionGlob(this.getExtensions(), ROOT_ONLY), {cwd: directory});
|
|
|
|
|
|
|
|
const dirMatches = glob.sync(this.getDirectoryGlob(this.getDirectories(), ROOT_ONLY), {cwd: directory});
|
|
|
|
let extMatchesAll;
|
2014-12-29 21:33:47 +03:00
|
|
|
|
|
|
|
// There is no base directory
|
|
|
|
if (extMatches.length > 0 || dirMatches.length > 0) {
|
2015-01-04 00:11:40 +03:00
|
|
|
return;
|
2014-12-29 21:33:47 +03:00
|
|
|
}
|
|
|
|
// There is a base directory, grab it from any ext match
|
|
|
|
extMatchesAll = glob.sync(
|
|
|
|
this.getExtensionGlob(this.getExtensions(), ALL_DIRS), {cwd: directory}
|
|
|
|
);
|
|
|
|
if (extMatchesAll.length < 1 || extMatchesAll[0].split('/') < 1) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.ValidationError({message: i18n.t('errors.data.importer.index.invalidZipFileBaseDirectory')});
|
2014-12-29 21:33:47 +03:00
|
|
|
}
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2015-01-04 00:11:40 +03:00
|
|
|
return extMatchesAll[0].split('/')[0];
|
2014-12-29 21:33:47 +03:00
|
|
|
},
|
2014-12-11 00:50:00 +03:00
|
|
|
/**
|
|
|
|
* Process Zip
|
|
|
|
* Takes a reference to a zip file, extracts it, sends any relevant files from inside to the right handler, and
|
|
|
|
* returns an object in the importData format: {data: {}, images: []}
|
|
|
|
* The data key contains JSON representing any data that should be imported
|
|
|
|
* The image key contains references to images that will be stored (and where they will be stored)
|
|
|
|
* @param {File} file
|
|
|
|
* @returns {Promise(ImportData)}
|
|
|
|
*/
|
|
|
|
processZip: function (file) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const self = this;
|
2015-01-04 00:11:40 +03:00
|
|
|
|
2014-12-29 21:33:47 +03:00
|
|
|
return this.extractZip(file.path).then(function (zipDirectory) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const ops = [];
|
|
|
|
const importData = {};
|
|
|
|
let baseDir;
|
2015-01-04 00:11:40 +03:00
|
|
|
|
|
|
|
self.isValidZip(zipDirectory);
|
|
|
|
baseDir = self.getBaseDirectory(zipDirectory);
|
2014-12-11 00:50:00 +03:00
|
|
|
|
|
|
|
_.each(self.handlers, function (handler) {
|
2019-07-05 14:40:43 +03:00
|
|
|
if (Object.prototype.hasOwnProperty.call(importData, handler.type)) {
|
2014-12-11 00:50:00 +03:00
|
|
|
// This limitation is here to reduce the complexity of the importer for now
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.UnsupportedMediaTypeError({
|
|
|
|
message: i18n.t('errors.data.importer.index.zipContainsMultipleDataFormats')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2014-12-11 00:50:00 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const files = self.getFilesFromZip(handler, zipDirectory);
|
2014-12-11 00:50:00 +03:00
|
|
|
|
|
|
|
if (files.length > 0) {
|
|
|
|
ops.push(function () {
|
2014-12-29 21:33:47 +03:00
|
|
|
return handler.loadFile(files, baseDir).then(function (data) {
|
2014-12-11 00:50:00 +03:00
|
|
|
importData[handler.type] = data;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ops.length === 0) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.UnsupportedMediaTypeError({
|
|
|
|
message: i18n.t('errors.data.importer.index.noContentToImport')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2014-12-11 00:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sequence(ops).then(function () {
|
|
|
|
return importData;
|
2014-12-29 21:33:47 +03:00
|
|
|
});
|
2014-12-11 00:50:00 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Process File
|
|
|
|
* Takes a reference to a single file, sends it to the relevant handler to be loaded and returns an object in the
|
|
|
|
* importData format: {data: {}, images: []}
|
|
|
|
* The data key contains JSON representing any data that should be imported
|
|
|
|
* The image key contains references to images that will be stored (and where they will be stored)
|
|
|
|
* @param {File} file
|
|
|
|
* @returns {Promise(ImportData)}
|
|
|
|
*/
|
|
|
|
processFile: function (file, ext) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const fileHandler = _.find(this.handlers, function (handler) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.includes(handler.extensions, ext);
|
2014-12-11 00:50:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return fileHandler.loadFile([_.pick(file, 'name', 'path')]).then(function (loadedData) {
|
|
|
|
// normalize the returned data
|
2020-04-29 18:44:27 +03:00
|
|
|
const importData = {};
|
2014-12-11 00:50:00 +03:00
|
|
|
importData[fileHandler.type] = loadedData;
|
|
|
|
return importData;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Import Step 1:
|
|
|
|
* Load the given file into usable importData in the format: {data: {}, images: []}, regardless of
|
|
|
|
* whether the file is a single importable file like a JSON file, or a zip file containing loads of files.
|
|
|
|
* @param {File} file
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
loadFile: function (file) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const self = this;
|
|
|
|
const ext = path.extname(file.name).toLowerCase();
|
2015-01-04 00:11:40 +03:00
|
|
|
return this.isZip(ext) ? self.processZip(file) : self.processFile(file, ext);
|
2014-12-11 00:50:00 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Import Step 2:
|
|
|
|
* Pass the prepared importData through the preProcess function of the various importers, so that the importers can
|
|
|
|
* make any adjustments to the data based on relationships between it
|
|
|
|
* @param {ImportData} importData
|
|
|
|
* @returns {Promise(ImportData)}
|
|
|
|
*/
|
|
|
|
preProcess: function (importData) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const ops = [];
|
2014-12-11 00:50:00 +03:00
|
|
|
_.each(this.importers, function (importer) {
|
|
|
|
ops.push(function () {
|
|
|
|
return importer.preProcess(importData);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return pipeline(ops);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Import Step 3:
|
|
|
|
* Each importer gets passed the data from importData which has the key matching its type - i.e. it only gets the
|
|
|
|
* data that it should import. Each importer then handles actually importing that data into Ghost
|
|
|
|
* @param {ImportData} importData
|
2017-08-22 13:15:40 +03:00
|
|
|
* @param {importOptions} importOptions to allow override of certain import features such as locking a user
|
2014-12-11 00:50:00 +03:00
|
|
|
* @returns {Promise(ImportData)}
|
|
|
|
*/
|
2017-08-22 13:15:40 +03:00
|
|
|
doImport: function (importData, importOptions) {
|
|
|
|
importOptions = importOptions || {};
|
2020-04-29 18:44:27 +03:00
|
|
|
const ops = [];
|
2014-12-11 00:50:00 +03:00
|
|
|
_.each(this.importers, function (importer) {
|
2019-07-05 14:40:43 +03:00
|
|
|
if (Object.prototype.hasOwnProperty.call(importData, importer.type)) {
|
2014-12-11 00:50:00 +03:00
|
|
|
ops.push(function () {
|
2017-08-22 13:15:40 +03:00
|
|
|
return importer.doImport(importData[importer.type], importOptions);
|
2014-12-11 00:50:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return sequence(ops).then(function (importResult) {
|
|
|
|
return importResult;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Import Step 4:
|
|
|
|
* Report on what was imported, currently a no-op
|
|
|
|
* @param {ImportData} importData
|
|
|
|
* @returns {Promise(ImportData)}
|
|
|
|
*/
|
|
|
|
generateReport: function (importData) {
|
|
|
|
return Promise.resolve(importData);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Import From File
|
|
|
|
* The main method of the ImportManager, call this to kick everything off!
|
|
|
|
* @param {File} file
|
2017-08-22 13:15:40 +03:00
|
|
|
* @param {importOptions} importOptions to allow override of certain import features such as locking a user
|
2014-12-29 21:33:47 +03:00
|
|
|
* @returns {Promise}
|
2014-12-11 00:50:00 +03:00
|
|
|
*/
|
2018-07-31 13:50:11 +03:00
|
|
|
importFromFile: function (file, importOptions = {}) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const self = this;
|
2014-12-11 00:50:00 +03:00
|
|
|
|
|
|
|
// Step 1: Handle converting the file to usable data
|
|
|
|
return this.loadFile(file).then(function (importData) {
|
|
|
|
// Step 2: Let the importers pre-process the data
|
|
|
|
return self.preProcess(importData);
|
|
|
|
}).then(function (importData) {
|
|
|
|
// Step 3: Actually do the import
|
|
|
|
// @TODO: It would be cool to have some sort of dry run flag here
|
2017-08-22 13:15:40 +03:00
|
|
|
return self.doImport(importData, importOptions);
|
2014-12-11 00:50:00 +03:00
|
|
|
}).then(function (importData) {
|
2014-12-29 21:33:47 +03:00
|
|
|
// Step 4: Report on the import
|
2019-01-30 04:28:16 +03:00
|
|
|
return self.generateReport(importData);
|
|
|
|
}).finally(() => self.cleanUp()); // Step 5: Cleanup any files
|
2014-12-11 00:50:00 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = new ImportManager();
|