Skip to content

Commit

Permalink
chore(media-provider): add content-type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 9, 2023
1 parent 76c643f commit f754d81
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
41 changes: 25 additions & 16 deletions packages/metascraper-media-provider/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ const {

const createGetMedia = require('./get-media')

const isProtocol = value => ({ url }) => eq(protocolFn(url), value)
const isProtocol =
value =>
({ url }) =>
eq(protocolFn(url), value)

const isHttps = isProtocol('https')

const isMIME = extension => ({ ext, url }) =>
ext ? eq(ext, extension) : eq(extensionFn(url), extension)
const isMIME =
extension =>
({ ext, url, format }) =>
ext !== 'unknown_video'
? eq(ext, extension)
: eq(extensionFn(url), extension) || format.includes(extension)

const isMp4 = isMIME('mp4')
const isMp3 = isMIME('mp3')
Expand Down Expand Up @@ -60,19 +67,21 @@ const hasVideo = format =>
const isDownloadable = ({ url }) =>
new URL(url).searchParams.get('download') === '1'

const getFormatUrls = ({ orderBy }) => (input, filters) => {
const formats = get(input, 'formats') ||
get(input, 'entries[0].formats') || [input]

const url = chain(formats)
.filter(overEvery(filters))
.orderBy(orderBy, 'asc')
.map('url')
.last()
.value()

return !isEmpty(url) ? url : undefined
}
const getFormatUrls =
({ orderBy }) =>
(input, filters) => {
const formats = get(input, 'formats') ||
get(input, 'entries[0].formats') || [input]

const url = chain(formats)
.filter(overEvery(filters))
.orderBy(orderBy, 'asc')
.map('url')
.last()
.value()

return !isEmpty(url) ? url : undefined
}

const getVideoUrls = getFormatUrls({ orderBy: 'tbr' })

Expand Down
28 changes: 28 additions & 0 deletions packages/metascraper-media-provider/test/video/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const test = require('ava')

const { metascraper } = require('../helpers')

test('from file url content type', async t => {
const url =
'https://app.croct.dev/assets/workspace/customer-assets/9d97037d-64a2-4c25-b443-bfc2972f3c9e/a0442e2b-a384-4f2b-b443-9f34c2215e16'

const metadata = await metascraper({ url })

t.is(
metadata.video,
'https://app.croct.dev/assets/workspace/customer-assets/9d97037d-64a2-4c25-b443-bfc2972f3c9e/a0442e2b-a384-4f2b-b443-9f34c2215e16'
)
})

test('from file url extension', async t => {
const metadata = await metascraper({
url: 'https://cdn-microlink.vercel.app/file-examples/sample.mp4'
})

t.is(
metadata.video,
'https://cdn-microlink.vercel.app/file-examples/sample.mp4'
)
})

0 comments on commit f754d81

Please sign in to comment.