Refactored image importer to use class

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

- The class syntax would allow swapping out the storage mechanism in the importer making it universal to use with other file types like media or generic files.
This commit is contained in:
Naz 2023-03-01 18:10:23 +08:00
parent 2bf98e288f
commit 4573d8b4b8
No known key found for this signature in database
4 changed files with 20 additions and 15 deletions

View File

@ -59,10 +59,11 @@ class ImportManager {
storage: mediaStorage
});
const imageImporter = new ImageImporter();
/**
* @type {Importer[]} importers
*/
this.importers = [ImageImporter, RevueImporter, DataImporter];
this.importers = [imageImporter, RevueImporter, DataImporter];
/**
* @type {Handler[]}

View File

@ -1,7 +1,6 @@
const _ = require('lodash');
const storage = require('../../../adapters/storage');
let replaceImage;
let ImageImporter;
let preProcessPosts;
let preProcessTags;
let preProcessUsers;
@ -48,9 +47,10 @@ preProcessUsers = function (data, image) {
});
};
ImageImporter = {
type: 'images',
preProcess: function (importData) {
class ImageImporter {
type = 'images';
preProcess(importData) {
if (importData.images && importData.data) {
_.each(importData.images, function (image) {
preProcessPosts(importData.data.data, image);
@ -61,8 +61,9 @@ ImageImporter = {
importData.preProcessedByImage = true;
return importData;
},
doImport: function (imageData) {
}
doImport(imageData) {
const store = storage.getStorage('images');
return Promise.all(imageData.map(function (image) {
@ -71,6 +72,6 @@ ImageImporter = {
});
}));
}
};
}
module.exports = ImageImporter;

View File

@ -6,14 +6,16 @@ const ImageImporter = require('../../../../../../core/server/data/importer/impor
describe('ImageImporter', function () {
it('has the correct interface', function () {
ImageImporter.type.should.eql('images');
ImageImporter.preProcess.should.be.instanceof(Function);
ImageImporter.doImport.should.be.instanceof(Function);
const imageImporter = new ImageImporter();
imageImporter.type.should.eql('images');
imageImporter.preProcess.should.be.instanceof(Function);
imageImporter.doImport.should.be.instanceof(Function);
});
it('does preprocess posts, users and tags correctly', function () {
let inputData = require('../../../../../utils/fixtures/import/import-data-1.json');
let outputData = ImageImporter.preProcess(_.cloneDeep(inputData));
const imageImporter = new ImageImporter();
let outputData = imageImporter.preProcess(_.cloneDeep(inputData));
inputData = inputData.data.data;
outputData = outputData.data.data;
@ -42,6 +44,7 @@ describe('ImageImporter', function () {
it('does import the images correctly', function () {
const inputData = require('../../../../../utils/fixtures/import/import-data-1.json');
const imageImporter = new ImageImporter();
const storageApi = {
save: sinon.stub().returns(Promise.resolve())
@ -51,7 +54,7 @@ describe('ImageImporter', function () {
return storageApi;
});
ImageImporter.doImport(inputData.images).then(function () {
imageImporter.doImport(inputData.images).then(function () {
storageSpy.calledOnce.should.be.true();
storageApi.save.calledTwice.should.be.true();
});

View File

@ -395,7 +395,7 @@ describe('Importer', function () {
const inputCopy = _.cloneDeep(input);
const dataSpy = sinon.spy(DataImporter, 'preProcess');
const imageSpy = sinon.spy(ImageImporter, 'preProcess');
const imageSpy = sinon.spy(ImportManager.importers[0], 'preProcess');
const revueSpy = sinon.spy(RevueImporter, 'preProcess');
ImportManager.preProcess(inputCopy).then(function (output) {
@ -429,7 +429,7 @@ describe('Importer', function () {
return Promise.resolve(i);
});
const imageSpy = sinon.stub(ImageImporter, 'doImport').callsFake(function (i) {
const imageSpy = sinon.stub(ImportManager.importers[0], 'doImport').callsFake(function (i) {
return Promise.resolve(i);
});