2023-03-01 10:40:22 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const path = require('path');
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
class ImporterContentFileHandler {
|
2023-03-01 10:40:22 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} deps dependencies
|
|
|
|
* @param {Object} deps.storage storage adapter instance
|
|
|
|
* @param {Object} deps.config config instance
|
|
|
|
* @param {object} deps.urlUtils urlUtils instance
|
|
|
|
*/
|
|
|
|
constructor(deps) {
|
|
|
|
this.storage = deps.storage;
|
|
|
|
this.config = deps.config;
|
|
|
|
this.urlUtils = deps.urlUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = 'media';
|
|
|
|
|
|
|
|
get extensions() {
|
|
|
|
return this.config.get('uploads').media.extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
get contentTypes() {
|
|
|
|
return this.config.get('uploads').media.contentTypes;
|
|
|
|
}
|
|
|
|
|
2023-03-02 09:34:32 +03:00
|
|
|
// @NOTE: making the second parameter strict folder "content/media" broke the glob pattern
|
|
|
|
// in the importer, so we need to keep it as general "content" unless
|
|
|
|
// it becomes a strict requirement
|
2023-03-01 10:40:22 +03:00
|
|
|
directories = ['media', 'content'];
|
|
|
|
|
|
|
|
async loadFile(files, baseDir) {
|
|
|
|
const baseDirRegex = baseDir ? new RegExp('^' + baseDir + '/') : new RegExp('');
|
|
|
|
|
|
|
|
const mediaFolderRegexes = _.map(this.storage.staticFileURLPrefix.split('/'), function (dir) {
|
|
|
|
return new RegExp('^' + dir + '/');
|
|
|
|
});
|
|
|
|
|
|
|
|
// normalize the directory structure
|
|
|
|
const mediaContentPath = this.config.getContentPath('media');
|
|
|
|
files = _.map(files, function (file) {
|
|
|
|
const noBaseDir = file.name.replace(baseDirRegex, '');
|
|
|
|
let noGhostDirs = noBaseDir;
|
|
|
|
|
|
|
|
_.each(mediaFolderRegexes, function (regex) {
|
|
|
|
noGhostDirs = noGhostDirs.replace(regex, '');
|
|
|
|
});
|
|
|
|
|
|
|
|
file.originalPath = noBaseDir;
|
|
|
|
file.name = noGhostDirs;
|
|
|
|
file.targetDir = path.join(mediaContentPath, path.dirname(noGhostDirs));
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
|
|
|
|
const self = this;
|
2023-03-02 08:59:09 +03:00
|
|
|
return Promise.all(files.map(function (contentFile) {
|
|
|
|
return self.storage.getUniqueFileName(contentFile, contentFile.targetDir).then(function (targetFilename) {
|
|
|
|
contentFile.newPath = self.urlUtils.urlJoin(
|
|
|
|
'/',
|
|
|
|
self.urlUtils.getSubdir(),
|
|
|
|
self.storage.staticFileURLPrefix,
|
|
|
|
path.relative(mediaContentPath, targetFilename)
|
|
|
|
);
|
2023-03-01 10:40:22 +03:00
|
|
|
|
2023-03-02 08:59:09 +03:00
|
|
|
return contentFile;
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
module.exports = ImporterContentFileHandler;
|