Skip to content

Commit

Permalink
fix: gif as image format (#171)
Browse files Browse the repository at this point in the history
* fix: gif as image format

* build: add .isMediaUrl & .isMediaExtension helpers

* fix: linter

* test: update

* test: update snapshot
  • Loading branch information
Kikobeats authored May 7, 2019
1 parent a9d55b4 commit 1fbdae2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 66 deletions.
28 changes: 16 additions & 12 deletions packages/metascraper-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const AUDIO = 'audio'
const IMAGE = 'image'

const imageExtensions = chain(require('image-extensions'))
.difference(['gif'])
.reduce((acc, ext) => ({ ...acc, [ext]: IMAGE }), {})
.value()

Expand All @@ -47,7 +46,6 @@ const audioExtensions = chain(require('audio-extensions'))
.value()

const videoExtensions = chain(require('video-extensions'))
.union(['gif'])
.reduce((acc, ext) => ({ ...acc, [ext]: VIDEO }), {})
.value()

Expand Down Expand Up @@ -115,23 +113,27 @@ const protocol = url => {
return protocol.replace(':', '')
}

const isMediaUrl = (url, type, opts) =>
isUrl(url, opts) && isMediaExtension(url, type)
const isMediaTypeUrl = (url, type, opts) => isUrl(url, opts) && isMediaTypeExtension(url, type)

const isMediaExtension = (url, type) =>
eq(type, get(EXTENSIONS, extension(url)))
const isMediaTypeExtension = (url, type) => eq(type, get(EXTENSIONS, extension(url)))

const isVideoUrl = (url, opts) => isMediaUrl(url, VIDEO, opts)
const isMediaUrl = (url, opts) =>
isImageUrl(url, opts) || isVideoUrl(url, opts) || isAudioUrl(url, opts)

const isAudioUrl = (url, opts) => isMediaUrl(url, AUDIO, opts)
const isVideoUrl = (url, opts) => isMediaTypeUrl(url, VIDEO, opts)

const isImageUrl = (url, opts) => isMediaUrl(url, IMAGE, opts)
const isAudioUrl = (url, opts) => isMediaTypeUrl(url, AUDIO, opts)

const isVideoExtension = url => isMediaExtension(url, VIDEO)
const isImageUrl = (url, opts) => isMediaTypeUrl(url, IMAGE, opts)

const isAudioExtension = url => isMediaExtension(url, AUDIO)
const isMediaExtension = url =>
isImageExtension(url) || isVideoExtension(url) || isAudioExtension(url)

const isImageExtension = url => isMediaExtension(url, IMAGE)
const isVideoExtension = url => isMediaTypeExtension(url, VIDEO)

const isAudioExtension = url => isMediaTypeExtension(url, AUDIO)

const isImageExtension = url => isMediaTypeExtension(url, IMAGE)

const extension = (str = '') => {
const urlObj = urlLib.parse(str)
Expand Down Expand Up @@ -211,9 +213,11 @@ module.exports = {
normalizeUrl,
isMime,
isUrl,
isMediaUrl,
isVideoUrl,
isAudioUrl,
isImageUrl,
isMediaExtension,
isVideoExtension,
isAudioExtension,
isImageExtension
Expand Down
74 changes: 22 additions & 52 deletions packages/metascraper-helpers/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,26 @@ describe('metascraper-helpers', () => {
should(url(NaN, { url: 'https://kikobeats.com' })).be.null()
should(url('http://<foo>', { url: 'https://kikobeats.com' })).be.null()

should(url('blog', { url: 'https://kikobeats.com/' })).be.equal(
'https://kikobeats.com/blog'
)
should(url('blog', { url: 'https://kikobeats.com/' })).be.equal('https://kikobeats.com/blog')

should(
url(
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
)
).be.equal(
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
)
url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
).be.equal('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
should(
url(
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
{ url: 'https://kikobeats.com/' }
)
).be.equal(
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
)
url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', {
url: 'https://kikobeats.com/'
})
).be.equal('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
should(
url('magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a', {
url: 'https://kikobeats.com/'
})
).be.equal('magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a')
should(
url(
'http://cdn2.cloudpro.co.uk/sites/cloudprod7/files/4/29//handshake_0.jpg',
{
url: 'http://www.cloudpro.co.uk/go/6024'
}
)
).be.equal(
'http://cdn2.cloudpro.co.uk/sites/cloudprod7/files/4/29/handshake_0.jpg'
)
url('http://cdn2.cloudpro.co.uk/sites/cloudprod7/files/4/29//handshake_0.jpg', {
url: 'http://www.cloudpro.co.uk/go/6024'
})
).be.equal('http://cdn2.cloudpro.co.uk/sites/cloudprod7/files/4/29/handshake_0.jpg')
})

it('.absoluteUrl', () => {
Expand All @@ -68,30 +54,14 @@ describe('metascraper-helpers', () => {
'https://kikobeats.com/',
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
)
).be.equal(
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
)
should(absoluteUrl('https://kikobeats.com/', '')).be.equal(
'https://kikobeats.com/'
)
should(absoluteUrl('https://kikobeats.com/', null)).be.equal(
'https://kikobeats.com/'
)
should(absoluteUrl('https://kikobeats.com/', undefined)).be.equal(
'https://kikobeats.com/'
)
should(absoluteUrl('https://kikobeats.com/', 'blog')).be.equal(
'https://kikobeats.com/blog'
)
should(absoluteUrl('https://kikobeats.com', '/blog')).be.equal(
'https://kikobeats.com/blog'
)
should(absoluteUrl('https://kikobeats.com/', '/blog')).be.equal(
'https://kikobeats.com/blog'
)
should(absoluteUrl('http://kikobeats.com/', '/blog')).be.equal(
'http://kikobeats.com/blog'
)
).be.equal('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
should(absoluteUrl('https://kikobeats.com/', '')).be.equal('https://kikobeats.com/')
should(absoluteUrl('https://kikobeats.com/', null)).be.equal('https://kikobeats.com/')
should(absoluteUrl('https://kikobeats.com/', undefined)).be.equal('https://kikobeats.com/')
should(absoluteUrl('https://kikobeats.com/', 'blog')).be.equal('https://kikobeats.com/blog')
should(absoluteUrl('https://kikobeats.com', '/blog')).be.equal('https://kikobeats.com/blog')
should(absoluteUrl('https://kikobeats.com/', '/blog')).be.equal('https://kikobeats.com/blog')
should(absoluteUrl('http://kikobeats.com/', '/blog')).be.equal('http://kikobeats.com/blog')
})

it('.extension', () => {
Expand All @@ -104,7 +74,7 @@ describe('metascraper-helpers', () => {
it('.isMime', () => {
should(isMime('image/jpeg', 'image')).be.true()
should(isMime('image/png', 'image')).be.true()
should(isMime('image/gif', 'video')).be.true()
should(isMime('image/gif', 'image')).be.true()
should(isMime('video/mp4', 'video')).be.true()
should(isMime('audio/x-aac', 'audio')).be.true()
should(isMime('audio/x-wav', 'audio')).be.true()
Expand All @@ -115,7 +85,6 @@ describe('metascraper-helpers', () => {
should(isVideoUrl('demo.mp4')).be.false()
should(isVideoUrl('/demo.mp4')).be.false()
should(isVideoUrl('https://microlink.io/demo.mp4')).be.true()
should(isVideoUrl('https://microlink.io/demo.gif')).be.true()
})

it('.isImageUrl', () => {
Expand All @@ -124,6 +93,7 @@ describe('metascraper-helpers', () => {
should(isImageUrl('https://microlink.io/demo.png')).be.true()
should(isImageUrl('https://microlink.io/demo.jpg')).be.true()
should(isImageUrl('https://microlink.io/demo.jpeg')).be.true()
should(isImageUrl('https://microlink.io/demo.gif')).be.true()
})

it('.isAudioUrl', () => {
Expand All @@ -139,7 +109,6 @@ describe('metascraper-helpers', () => {
it('.isVideoExtension', () => {
should(isVideoExtension('https://microlink.io/demo.mp4')).be.true()
should(isVideoExtension('demo.mp4')).be.true()
should(isVideoExtension('demo.gif')).be.true()
})

it('.isAudioExtension', () => {
Expand All @@ -156,6 +125,7 @@ describe('metascraper-helpers', () => {
should(isImageExtension('demo.png')).be.true()
should(isImageExtension('demo.jpg')).be.true()
should(isImageExtension('demo.jpeg')).be.true()
should(isImageExtension('demo.gif')).be.true()
})

it('.description', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports['og:video 1'] = {

exports['single src 1'] = {
"image": null,
"video": "https://cdn.vox-cdn.com/thumbor/4l0C-7uGFtTfc6lWibo1ITiE2YU=/0x0:1280x720/320x213/filters:focal(538x258:742x462):gifv():no_upscale()/cdn.vox-cdn.com/uploads/chorus_image/image/58416873/2018_01_22_14_19_55.0.gif"
"video": null
}

exports['clips.twitch.tv 1'] = {
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper/src/get-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { map, fromPairs, isEmpty, isObject, isArray, mapValues, isPlainObject } = require('lodash')
const { map, fromPairs, isEmpty, isObject, isArray, mapValues } = require('lodash')
const xss = require('xss')

const getValue = async ({ htmlDom, url, conditions, meta }) => {
Expand Down

0 comments on commit 1fbdae2

Please sign in to comment.