Fixed root zip media/files copying during import

refs https://github.com/TryGhost/Toolbox/issues/523

- During import process of content files the files from the root directory were also copied over. This is causing chaos in the root of content folder with files that only needed for data import. For example, the csv files needed for Revue import were also copied over by "file importer" even though those do not belong to any content.
- The approach of filtering on a "handler" level was taken over filtering in the import manager due to how our globing patterns work. See a previous commit with reverted previous fix for more context.
This commit is contained in:
Naz 2023-03-16 22:48:57 +01:00
parent c487b12518
commit 74a1ab4525
No known key found for this signature in database
3 changed files with 46 additions and 0 deletions

View File

@ -61,6 +61,7 @@ class ImportManager {
// in the importer, so we need to keep it as general "content" unless // in the importer, so we need to keep it as general "content" unless
// it becomes a strict requirement // it becomes a strict requirement
directories: ['media', 'content'], directories: ['media', 'content'],
ignoreRootFolderFiles: true,
extensions: config.get('uploads').media.extensions, extensions: config.get('uploads').media.extensions,
contentTypes: config.get('uploads').media.contentTypes, contentTypes: config.get('uploads').media.contentTypes,
contentPath: config.getContentPath('media'), contentPath: config.getContentPath('media'),
@ -74,6 +75,7 @@ class ImportManager {
// in the importer, so we need to keep it as general "content" unless // in the importer, so we need to keep it as general "content" unless
// it becomes a strict requirement // it becomes a strict requirement
directories: ['files', 'content'], directories: ['files', 'content'],
ignoreRootFolderFiles: true,
extensions: config.get('uploads').files.extensions, extensions: config.get('uploads').files.extensions,
contentTypes: config.get('uploads').files.contentTypes, contentTypes: config.get('uploads').files.contentTypes,
contentPath: config.getContentPath('files'), contentPath: config.getContentPath('files'),

View File

@ -24,6 +24,7 @@ class ImporterContentFileHandler {
* @param {Object} deps dependencies * @param {Object} deps dependencies
* @param {'media' | 'files' | 'images'} deps.type type of content file * @param {'media' | 'files' | 'images'} deps.type type of content file
* @param {string[]} deps.extensions file extensions to search for * @param {string[]} deps.extensions file extensions to search for
* @param {boolean} [deps.ignoreRootFolderFiles] whether to ignore files in the root folder
* @param {string[]} deps.contentTypes content types to search for * @param {string[]} deps.contentTypes content types to search for
* @param {string[]} deps.directories directories to search for content files * @param {string[]} deps.directories directories to search for content files
* @param {string} deps.contentPath path to the destination content directory * @param {string} deps.contentPath path to the destination content directory
@ -35,6 +36,7 @@ class ImporterContentFileHandler {
this.directories = deps.directories; this.directories = deps.directories;
this.extensions = deps.extensions; this.extensions = deps.extensions;
this.contentTypes = deps.contentTypes; this.contentTypes = deps.contentTypes;
this.ignoreRootFolderFiles = deps.ignoreRootFolderFiles;
this.storage = deps.storage; this.storage = deps.storage;
this.#contentPath = deps.contentPath; this.#contentPath = deps.contentPath;
this.urlUtils = deps.urlUtils; this.urlUtils = deps.urlUtils;
@ -47,6 +49,12 @@ class ImporterContentFileHandler {
return new RegExp('^' + dir + '/'); return new RegExp('^' + dir + '/');
}); });
if (this.ignoreRootFolderFiles) {
files = _.filter(files, function (file) {
return file.name.indexOf('/') !== -1;
});
}
// normalize the directory structure // normalize the directory structure
const filesContentPath = this.#contentPath; const filesContentPath = this.#contentPath;
files = _.map(files, function (file) { files = _.map(files, function (file) {

View File

@ -88,5 +88,41 @@ describe('ImporterContentFileHandler', function () {
assert.equal(files[0].targetDir, '/var/www/ghost/content/media'); assert.equal(files[0].targetDir, '/var/www/ghost/content/media');
assert.equal(files[0].newPath, '//blog/content/media/1.mp4'); assert.equal(files[0].newPath, '//blog/content/media/1.mp4');
}); });
it('ignores files in root folder', async function () {
const contentFileImporter = new ImporterContentFileHandler({
storage: {
staticFileURLPrefix: 'content/media',
getUniqueFileName: (file, targetDir) => Promise.resolve(targetDir + '/' + file.name)
},
contentPath: '/var/www/ghost/content/media',
ignoreRootFolderFiles: true,
urlUtils: {
getSubdir: () => 'blog',
urlJoin: (...args) => args.join('/')
}
});
const files = [{
name: 'root.mp4'
}, {
name: 'content/media/1.mp4'
}];
await contentFileImporter.loadFile(files);
// @NOTE: the root file is ignored. It's a weird test because ideally the file
// should be removed completely from the list, but the importer works
// by modifying the list in place and it's not easy to remove the file
assert.equal(files[0].name, 'root.mp4');
assert.equal(files[0].originalPath, undefined);
assert.equal(files[0].targetDir, undefined);
assert.equal(files[0].newPath, undefined);
assert.equal(files[1].name, '1.mp4');
assert.equal(files[1].originalPath, 'content/media/1.mp4');
assert.equal(files[1].targetDir, '/var/www/ghost/content/media');
assert.equal(files[1].newPath, '//blog/content/media/1.mp4');
});
}); });
}); });