feat(core): when building thumbnails, fallback to JPG if the source is not a browser-friendly format

If the source is browser-friendly (jpg, jpeg, png) then keep the exact original extension.
pull/107/head
Romain 6 years ago
parent 7edf3bbf15
commit e1517d2fbd

@ -2,6 +2,8 @@ const warn = require('debug')('thumbsup:warn')
const path = require('path')
const urljoin = require('url-join')
const BROWSER_SUPPORTED_EXT = /\.(jpg|jpeg|png)$/i
exports.paths = function (filepath, mediaType, opts) {
if (mediaType === 'image') {
const items = imageOutput(filepath)
@ -18,6 +20,10 @@ exports.paths = function (filepath, mediaType, opts) {
}
function imageOutput (filepath) {
const extension = path.extname(filepath)
if (!extension.match(BROWSER_SUPPORTED_EXT)) {
filepath = ext(filepath, 'jpg')
}
return {
thumbnail: {
path: 'media/thumbs/' + filepath,

@ -59,6 +59,21 @@ describe('Output paths', function () {
rel: 'fs:link'
})
})
it('keeps the original image format if the browser supports it', function () {
['jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG'].forEach(ext => {
var o = output.paths(`holidays/beach.${ext}`, 'image', {})
should(o.thumbnail.path).eql(`media/thumbs/holidays/beach.${ext}`)
})
})
it('converts images to JPEG if not supported', function () {
// some of these formats are supported on certain browser, but we aim for maximum compatibility
['bmp', 'tiff', 'webp'].forEach(ext => {
var o = output.paths(`holidays/beach.${ext}`, 'image', {})
should(o.thumbnail.path).eql(`media/thumbs/holidays/beach.jpg`)
})
})
})
describe('Videos', function () {

Loading…
Cancel
Save