From 831a76505cd95c55d973e6c5c53acf0d8ddb02a8 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 8 Mar 2023 11:03:34 +0800 Subject: [PATCH] Fixed root zip image/media/files copying during import 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. - Any content import files - images, media, files, should be in according folders in the imported zip file. The root files in the base zip directory are for data-related imports --- .../core/server/data/importer/import-manager.js | 9 ++++++++- .../test/unit/server/data/importer/index.test.js | 2 +- .../{ => images}/image.JPG | 0 .../test/ImporterContentFileHandler.test.js | 16 ++++++++++++---- 4 files changed, 21 insertions(+), 6 deletions(-) rename ghost/core/test/utils/fixtures/import/zips/zip-uppercase-extensions/{ => images}/image.JPG (100%) diff --git a/ghost/core/core/server/data/importer/import-manager.js b/ghost/core/core/server/data/importer/import-manager.js index a7b0053564..6de8825c73 100644 --- a/ghost/core/core/server/data/importer/import-manager.js +++ b/ghost/core/core/server/data/importer/import-manager.js @@ -301,7 +301,14 @@ class ImportManager { const baseDir = this.getBaseDirectory(zipDirectory); for (const handler of this.handlers) { - const files = this.getFilesFromZip(handler, zipDirectory); + let files = []; + if (handler.directories?.length > 0) { + for (const dir of handler.directories) { + files.push(...this.getFilesFromZip(handler, path.join(zipDirectory, (baseDir || ''), dir))); + } + } else { + files.push(...this.getFilesFromZip(handler, zipDirectory)); + } debug('handler', handler.type, files); diff --git a/ghost/core/test/unit/server/data/importer/index.test.js b/ghost/core/test/unit/server/data/importer/index.test.js index b23cf2c753..820bf5b804 100644 --- a/ghost/core/test/unit/server/data/importer/index.test.js +++ b/ghost/core/test/unit/server/data/importer/index.test.js @@ -228,7 +228,7 @@ describe('Importer', function () { extractSpy.calledOnce.should.be.true(); validSpy.calledOnce.should.be.true(); baseDirSpy.calledOnce.should.be.true(); - getFileSpy.callCount.should.eql(6); + getFileSpy.callCount.should.eql(9); jsonSpy.calledOnce.should.be.true(); imageSpy.called.should.be.false(); mdSpy.called.should.be.false(); diff --git a/ghost/core/test/utils/fixtures/import/zips/zip-uppercase-extensions/image.JPG b/ghost/core/test/utils/fixtures/import/zips/zip-uppercase-extensions/images/image.JPG similarity index 100% rename from ghost/core/test/utils/fixtures/import/zips/zip-uppercase-extensions/image.JPG rename to ghost/core/test/utils/fixtures/import/zips/zip-uppercase-extensions/images/image.JPG diff --git a/ghost/importer-handler-content-files/test/ImporterContentFileHandler.test.js b/ghost/importer-handler-content-files/test/ImporterContentFileHandler.test.js index aefbe872de..d0c842dfd7 100644 --- a/ghost/importer-handler-content-files/test/ImporterContentFileHandler.test.js +++ b/ghost/importer-handler-content-files/test/ImporterContentFileHandler.test.js @@ -52,16 +52,24 @@ describe('ImporterContentFileHandler', function () { }); const files = [{ - name: 'content/media/1.mp4' + name: 'content/media/video-in-content-media.mp4' + }, { + name: 'media/video-in-media.mp4' }]; const subDir = 'blog'; await contentFileImporter.loadFile(files, subDir); - assert.equal(files[0].name, '1.mp4'); - assert.equal(files[0].originalPath, 'content/media/1.mp4'); + assert.equal(files.length, 2); + assert.equal(files[0].name, 'video-in-content-media.mp4'); + assert.equal(files[0].originalPath, 'content/media/video-in-content-media.mp4'); 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/video-in-content-media.mp4'); + + assert.equal(files[1].name, 'video-in-media.mp4'); + assert.equal(files[1].originalPath, 'media/video-in-media.mp4'); + assert.equal(files[1].targetDir, '/var/www/ghost/content/media'); + assert.equal(files[1].newPath, '//blog/content/media/video-in-media.mp4'); }); it('loads files and decorates them with newPath with NO subdirectory', async function () {