diff --git a/core/server/api/canary/media.js b/core/server/api/canary/media.js index c89d07f89c..fbbac4197d 100644 --- a/core/server/api/canary/media.js +++ b/core/server/api/canary/media.js @@ -6,8 +6,12 @@ module.exports = { statusCode: 201, permissions: false, async query(frame) { + let thumbnail = null; + if (frame.files.thumbnail && frame.files.thumbnail[0]) { + thumbnail = await storage.getStorage('media').save(frame.files.thumbnail[0]); + } + const file = await storage.getStorage('media').save(frame.files.file[0]); - const thumbnail = await storage.getStorage('media').save(frame.files.thumbnail[0]); return { filePath: file, diff --git a/core/server/web/api/middleware/upload.js b/core/server/web/api/middleware/upload.js index 462b656550..0b3c7786bc 100644 --- a/core/server/web/api/middleware/upload.js +++ b/core/server/web/api/middleware/upload.js @@ -190,28 +190,31 @@ const mediaValidation = function ({type}) { req.file.type = req.file.mimetype; req.file.ext = path.extname(req.file.name).toLowerCase(); - const {thumbnail: [thumbnailFile] = []} = req.files; - if (!thumbnailFile || !checkFileExists(thumbnailFile)) { - return next(new errors.ValidationError({ - message: tpl(messages.thumbnail.missingFile) - })); - } - - req.thumbnail = thumbnailFile; - req.thumbnail.ext = path.extname(thumbnailFile.originalname).toLowerCase(); - req.thumbnail.name = `${path.basename(req.file.name, path.extname(req.file.name))}_thumb${req.thumbnail.ext}`; - req.thumbnail.type = req.thumbnail.mimetype; - if (!checkFileIsValid(req.file, contentTypes, extensions)) { return next(new errors.UnsupportedMediaTypeError({ message: tpl(messages[type].invalidFile, {extensions: extensions}) })); } - if (!checkFileIsValid(req.thumbnail, thumbnailContentTypes, thumbnailExtensions)) { - return next(new errors.UnsupportedMediaTypeError({ - message: tpl(messages.thumbnail.invalidFile, {extensions: thumbnailExtensions}) - })); + const {thumbnail: [thumbnailFile] = []} = req.files; + + if (thumbnailFile) { + if (!checkFileExists(thumbnailFile)) { + return next(new errors.ValidationError({ + message: tpl(messages.thumbnail.missingFile) + })); + } + + req.thumbnail = thumbnailFile; + req.thumbnail.ext = path.extname(thumbnailFile.originalname).toLowerCase(); + req.thumbnail.name = `${path.basename(req.file.name, path.extname(req.file.name))}_thumb${req.thumbnail.ext}`; + req.thumbnail.type = req.thumbnail.mimetype; + + if (!checkFileIsValid(req.thumbnail, thumbnailContentTypes, thumbnailExtensions)) { + return next(new errors.UnsupportedMediaTypeError({ + message: tpl(messages.thumbnail.invalidFile, {extensions: thumbnailExtensions}) + })); + } } next(); diff --git a/test/e2e-api/admin/media.test.js b/test/e2e-api/admin/media.test.js index 5f70ae3dfb..be2edc749e 100644 --- a/test/e2e-api/admin/media.test.js +++ b/test/e2e-api/admin/media.test.js @@ -41,17 +41,17 @@ describe('Media API', function () { media.push(res.body.media[0].thumbnail_url.replace(config.get('url'), '')); }); - it('Can upload a WebM', async function () { + it('Can upload a WebM without a thumbnail', async function () { const res = await request.post(localUtils.API.getApiQuery('media/upload')) .set('Origin', config.get('url')) .expect('Content-Type', /json/) .field('purpose', 'video') .field('ref', 'https://ghost.org/sample_640x360.webm') .attach('file', path.join(__dirname, '/../../utils/fixtures/media/sample_640x360.webm')) - .attach('thumbnail', path.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png')) .expect(201); res.body.media[0].url.should.match(new RegExp(`${config.get('url')}/content/media/\\d+/\\d+/sample_640x360.webm`)); + should(res.body.media[0].thumbnail_url).eql(null); res.body.media[0].ref.should.equal('https://ghost.org/sample_640x360.webm'); media.push(res.body.media[0].url.replace(config.get('url'), '')); @@ -83,14 +83,4 @@ describe('Media API', function () { res.body.errors[0].message.should.match(/select a valid media file/gi); }); - - it('Rejects when thumbnail is not present', async function () { - const res = await request.post(localUtils.API.getApiQuery('media/upload')) - .set('Origin', config.get('url')) - .expect('Content-Type', /json/) - .attach('file', path.join(__dirname, '/../../utils/fixtures/media/sample_640x360.ogv')) - .expect(422); - - res.body.errors[0].message.should.match(/Please select a thumbnail./gi); - }); });