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-02 10:32:36 +03:00
|
|
|
/** @property {'media' | 'files' | 'images'} */
|
|
|
|
type;
|
|
|
|
|
|
|
|
/** @property {string[]} */
|
|
|
|
directories;
|
|
|
|
|
|
|
|
/** @property {string[]} */
|
|
|
|
extensions;
|
|
|
|
|
|
|
|
/** @property {string[]} */
|
|
|
|
contentTypes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds path to the destination content directory
|
|
|
|
* @property {string} */
|
|
|
|
#contentPath;
|
|
|
|
|
2023-03-01 10:40:22 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} deps dependencies
|
2023-03-02 10:32:36 +03:00
|
|
|
* @param {'media' | 'files' | 'images'} deps.type type of content file
|
|
|
|
* @param {string[]} deps.extensions file extensions to search for
|
2023-03-17 00:48:57 +03:00
|
|
|
* @param {boolean} [deps.ignoreRootFolderFiles] whether to ignore files in the root folder
|
2023-03-02 10:32:36 +03:00
|
|
|
* @param {string[]} deps.contentTypes content types to search for
|
|
|
|
* @param {string[]} deps.directories directories to search for content files
|
|
|
|
* @param {string} deps.contentPath path to the destination content directory
|
2023-03-01 10:40:22 +03:00
|
|
|
* @param {Object} deps.storage storage adapter instance
|
|
|
|
* @param {object} deps.urlUtils urlUtils instance
|
|
|
|
*/
|
|
|
|
constructor(deps) {
|
2023-03-02 10:32:36 +03:00
|
|
|
this.type = deps.type;
|
|
|
|
this.directories = deps.directories;
|
|
|
|
this.extensions = deps.extensions;
|
|
|
|
this.contentTypes = deps.contentTypes;
|
2023-03-17 00:48:57 +03:00
|
|
|
this.ignoreRootFolderFiles = deps.ignoreRootFolderFiles;
|
2023-03-01 10:40:22 +03:00
|
|
|
this.storage = deps.storage;
|
2023-03-02 10:32:36 +03:00
|
|
|
this.#contentPath = deps.contentPath;
|
2023-03-01 10:40:22 +03:00
|
|
|
this.urlUtils = deps.urlUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadFile(files, baseDir) {
|
|
|
|
const baseDirRegex = baseDir ? new RegExp('^' + baseDir + '/') : new RegExp('');
|
|
|
|
|
2023-03-02 11:34:21 +03:00
|
|
|
const contentFilesFolderRegexes = _.map(this.storage.staticFileURLPrefix.split('/'), function (dir) {
|
2023-03-01 10:40:22 +03:00
|
|
|
return new RegExp('^' + dir + '/');
|
|
|
|
});
|
|
|
|
|
2023-03-17 00:48:57 +03:00
|
|
|
if (this.ignoreRootFolderFiles) {
|
|
|
|
files = _.filter(files, function (file) {
|
|
|
|
return file.name.indexOf('/') !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-01 10:40:22 +03:00
|
|
|
// normalize the directory structure
|
2023-03-02 11:34:21 +03:00
|
|
|
const filesContentPath = this.#contentPath;
|
2023-03-01 10:40:22 +03:00
|
|
|
files = _.map(files, function (file) {
|
|
|
|
const noBaseDir = file.name.replace(baseDirRegex, '');
|
|
|
|
let noGhostDirs = noBaseDir;
|
|
|
|
|
2023-03-02 11:34:21 +03:00
|
|
|
_.each(contentFilesFolderRegexes, function (regex) {
|
2023-03-01 10:40:22 +03:00
|
|
|
noGhostDirs = noGhostDirs.replace(regex, '');
|
|
|
|
});
|
|
|
|
|
|
|
|
file.originalPath = noBaseDir;
|
|
|
|
file.name = noGhostDirs;
|
2023-03-02 11:34:21 +03:00
|
|
|
file.targetDir = path.join(filesContentPath, path.dirname(noGhostDirs));
|
2023-03-01 10:40:22 +03:00
|
|
|
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,
|
2023-03-02 11:34:21 +03:00
|
|
|
path.relative(filesContentPath, targetFilename)
|
2023-03-02 08:59:09 +03:00
|
|
|
);
|
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;
|