2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2023-03-02 10:05:22 +03:00
|
|
|
const ImporterContentFileHandler = require('../index');
|
2023-03-01 10:40:22 +03:00
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
describe('ImporterContentFileHandler', function () {
|
2023-03-01 10:40:22 +03:00
|
|
|
it('creates an instance', function () {
|
2023-03-02 10:05:22 +03:00
|
|
|
const contentFileImporter = new ImporterContentFileHandler({
|
2023-03-02 10:32:36 +03:00
|
|
|
type: 'media',
|
2023-03-01 10:40:22 +03:00
|
|
|
storage: {},
|
|
|
|
config: {},
|
|
|
|
urlUtils: {}
|
|
|
|
});
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
assert.ok(contentFileImporter);
|
|
|
|
assert.equal(contentFileImporter.type, 'media');
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns configured extensions', function () {
|
2023-03-02 10:05:22 +03:00
|
|
|
const contentFileImporter = new ImporterContentFileHandler({
|
2023-03-01 10:40:22 +03:00
|
|
|
storage: {},
|
2023-03-02 10:32:36 +03:00
|
|
|
extensions: ['mp4'],
|
2023-03-01 10:40:22 +03:00
|
|
|
urlUtils: {}
|
|
|
|
});
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
assert.deepEqual(contentFileImporter.extensions, ['mp4']);
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns configured contentTypes', function () {
|
2023-03-02 10:05:22 +03:00
|
|
|
const contentFileImporter = new ImporterContentFileHandler({
|
2023-03-01 10:40:22 +03:00
|
|
|
storage: {},
|
2023-03-02 10:32:36 +03:00
|
|
|
contentTypes: ['video/mp4'],
|
2023-03-01 10:40:22 +03:00
|
|
|
urlUtils: {}
|
|
|
|
});
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
assert.deepEqual(contentFileImporter.contentTypes, ['video/mp4']);
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// @NOTE: below tests need more work, they are just covering the basics
|
|
|
|
// the cases are based on a fact that the implementation was a copy
|
|
|
|
// from the image importer and the tests were adapted to the media importer
|
|
|
|
describe('loadFile', function () {
|
|
|
|
it('loads files and decorates them with newPath with subdirectory', async function () {
|
2023-03-02 10:05:22 +03:00
|
|
|
const contentFileImporter = new ImporterContentFileHandler({
|
2023-03-01 10:40:22 +03:00
|
|
|
storage: {
|
|
|
|
staticFileURLPrefix: 'content/media',
|
|
|
|
getUniqueFileName: (file, targetDir) => Promise.resolve(targetDir + '/' + file.name)
|
|
|
|
},
|
2023-03-02 10:32:36 +03:00
|
|
|
contentPath: '/var/www/ghost/content/media',
|
2023-03-01 10:40:22 +03:00
|
|
|
urlUtils: {
|
|
|
|
getSubdir: () => 'blog',
|
|
|
|
urlJoin: (...args) => args.join('/')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const files = [{
|
2023-03-16 23:52:24 +03:00
|
|
|
name: 'content/media/1.mp4'
|
2023-03-01 10:40:22 +03:00
|
|
|
}];
|
|
|
|
const subDir = 'blog';
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
await contentFileImporter.loadFile(files, subDir);
|
2023-03-01 10:40:22 +03:00
|
|
|
|
2023-03-16 23:52:24 +03:00
|
|
|
assert.equal(files[0].name, '1.mp4');
|
|
|
|
assert.equal(files[0].originalPath, 'content/media/1.mp4');
|
2023-03-01 10:40:22 +03:00
|
|
|
assert.equal(files[0].targetDir, '/var/www/ghost/content/media');
|
2023-03-16 23:52:24 +03:00
|
|
|
assert.equal(files[0].newPath, '//blog/content/media/1.mp4');
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('loads files and decorates them with newPath with NO subdirectory', async function () {
|
2023-03-02 10:05:22 +03:00
|
|
|
const contentFileImporter = new ImporterContentFileHandler({
|
2023-03-01 10:40:22 +03:00
|
|
|
storage: {
|
|
|
|
staticFileURLPrefix: 'content/media',
|
|
|
|
getUniqueFileName: (file, targetDir) => Promise.resolve(targetDir + '/' + file.name)
|
|
|
|
},
|
2023-03-02 10:32:36 +03:00
|
|
|
contentPath: '/var/www/ghost/content/media',
|
2023-03-01 10:40:22 +03:00
|
|
|
urlUtils: {
|
|
|
|
getSubdir: () => 'blog',
|
|
|
|
urlJoin: (...args) => args.join('/')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const files = [{
|
|
|
|
name: 'content/media/1.mp4'
|
|
|
|
}];
|
|
|
|
|
2023-03-02 10:05:22 +03:00
|
|
|
await contentFileImporter.loadFile(files);
|
2023-03-01 10:40:22 +03:00
|
|
|
|
|
|
|
assert.equal(files[0].name, '1.mp4');
|
|
|
|
assert.equal(files[0].originalPath, 'content/media/1.mp4');
|
|
|
|
assert.equal(files[0].targetDir, '/var/www/ghost/content/media');
|
|
|
|
assert.equal(files[0].newPath, '//blog/content/media/1.mp4');
|
|
|
|
});
|
2023-03-17 00:48:57 +03:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2023-03-01 10:40:22 +03:00
|
|
|
});
|
|
|
|
});
|