mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-01 02:41:39 -05:00
Extracted image store to constructor parameter
refs https://github.com/TryGhost/Toolbox/issues/523 - We need to be able to use different storage mechanisms when importing different types of content - Having the storage passed in using constructor DI allows to have more flexible storage mechanism in the Images importer (soon to become a generic file importer)
This commit is contained in:
parent
4573d8b4b8
commit
edebe1571c
3 changed files with 25 additions and 11 deletions
|
@ -22,6 +22,7 @@ const urlUtils = require('../../../shared/url-utils');
|
|||
const {GhostMailer} = require('../../services/mail');
|
||||
const jobManager = require('../../services/jobs');
|
||||
const mediaStorage = require('../../adapters/storage').getStorage('media');
|
||||
const imageStorage = require('../../adapters/storage').getStorage('images');
|
||||
|
||||
const emailTemplate = require('./email-template');
|
||||
const ghostMailer = new GhostMailer();
|
||||
|
@ -59,7 +60,9 @@ class ImportManager {
|
|||
storage: mediaStorage
|
||||
});
|
||||
|
||||
const imageImporter = new ImageImporter();
|
||||
const imageImporter = new ImageImporter({
|
||||
store: imageStorage
|
||||
});
|
||||
/**
|
||||
* @type {Importer[]} importers
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const _ = require('lodash');
|
||||
const storage = require('../../../adapters/storage');
|
||||
let replaceImage;
|
||||
let preProcessPosts;
|
||||
let preProcessTags;
|
||||
|
@ -50,6 +49,18 @@ preProcessUsers = function (data, image) {
|
|||
class ImageImporter {
|
||||
type = 'images';
|
||||
|
||||
/** @property {import('ghost-storage-base')} */
|
||||
#store;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} deps
|
||||
* @param {import('ghost-storage-base')} deps.store
|
||||
*/
|
||||
constructor(deps) {
|
||||
this.#store = deps.store;
|
||||
}
|
||||
|
||||
preProcess(importData) {
|
||||
if (importData.images && importData.data) {
|
||||
_.each(importData.images, function (image) {
|
||||
|
@ -64,7 +75,7 @@ class ImageImporter {
|
|||
}
|
||||
|
||||
doImport(imageData) {
|
||||
const store = storage.getStorage('images');
|
||||
const store = this.#store;
|
||||
|
||||
return Promise.all(imageData.map(function (image) {
|
||||
return store.save(image, image.targetDir).then(function (result) {
|
||||
|
|
|
@ -6,7 +6,9 @@ const ImageImporter = require('../../../../../../core/server/data/importer/impor
|
|||
|
||||
describe('ImageImporter', function () {
|
||||
it('has the correct interface', function () {
|
||||
const imageImporter = new ImageImporter();
|
||||
const imageImporter = new ImageImporter({
|
||||
store: {}
|
||||
});
|
||||
imageImporter.type.should.eql('images');
|
||||
imageImporter.preProcess.should.be.instanceof(Function);
|
||||
imageImporter.doImport.should.be.instanceof(Function);
|
||||
|
@ -14,7 +16,9 @@ describe('ImageImporter', function () {
|
|||
|
||||
it('does preprocess posts, users and tags correctly', function () {
|
||||
let inputData = require('../../../../../utils/fixtures/import/import-data-1.json');
|
||||
const imageImporter = new ImageImporter();
|
||||
const imageImporter = new ImageImporter({
|
||||
store: {}
|
||||
});
|
||||
let outputData = imageImporter.preProcess(_.cloneDeep(inputData));
|
||||
|
||||
inputData = inputData.data.data;
|
||||
|
@ -44,18 +48,14 @@ 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())
|
||||
};
|
||||
|
||||
const storageSpy = sinon.stub(storage, 'getStorage').callsFake(function () {
|
||||
return storageApi;
|
||||
const imageImporter = new ImageImporter({
|
||||
store: storageApi
|
||||
});
|
||||
|
||||
imageImporter.doImport(inputData.images).then(function () {
|
||||
storageSpy.calledOnce.should.be.true();
|
||||
storageApi.save.calledTwice.should.be.true();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue