Skip to content

Commit

Permalink
Merge 0f7e668 into 5b461b3
Browse files Browse the repository at this point in the history
  • Loading branch information
philipjscott committed Aug 30, 2018
2 parents 5b461b3 + 0f7e668 commit 5a70632
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 83 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -42,7 +42,7 @@ run()

## API

#### genThumbnail(input, output, size)
#### genThumbnail(input, output, size, [config])

Returns of a `Promise` which resolves on thumbnail creation.

Expand All @@ -68,3 +68,15 @@ The dimensions of the generated thumbnail. The `size` argument may have one of t
* `150x?`: set a fixed width and compute the height automatically.
* `?x100`: set a fixed height and compute the width automatically.
* `50%`: rescale both width and height to given percentage.

#### config

Type: `Object`

A configuration object, see details below.

#### config.path

Type: `String`

The path of the `ffmpeg` binary. If omitted, the path will be set to the `FFMPEG_PATH` environment variable. If the environment variable is not set, `ffmpeg` will be invoked directly (ie. `ffmpeg [...]`).
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -106,7 +106,7 @@ function ffmpegExecute (path, args, stream = null) {
* @returns {Promise} Resolves on completion
*/
function genThumbnail (input, output, size, config = {}) {
const ffmpegPath = config.path || 'ffmpeg'
const ffmpegPath = config.path || process.env.FFMPEG_PATH || 'ffmpeg'

const parsedSize = parseSize(size)
const args = buildArgs(
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
"url": "https://github.com/ScottyFillups/simple-thumbnail.git"
},
"devDependencies": {
"array-flatten": "^2.1.1",
"chai": "^4.1.2",
"coveralls": "^3.0.2",
"dirty-chai": "^2.0.1",
Expand Down
148 changes: 67 additions & 81 deletions test/test.js
Expand Up @@ -9,9 +9,9 @@ const url = require('url')

const chai = require('chai')
const dirtyChai = require('dirty-chai')

const genThumbnail = require('../')
const looksSame = util.promisify(require('looks-same'))
const flatten = require('array-flatten')
const nock = require('nock')

const { expect } = chai
Expand All @@ -24,10 +24,18 @@ describe('simple-thumbnail creates thumbnails for videos', () => {

before(async () => {
await fs.remove(absolutePath('./out'))
await fs.mkdirp(absolutePath('./out/storage'))
await fs.mkdirp(absolutePath('./out/input-formats'))
await fs.mkdirp(absolutePath('./out/image-formats'))
await fs.mkdirp(absolutePath('./out/sizes'))

const directories = [
'./out',
'./out/storage',
'./out/input-formats',
'./out/image-formats',
'./out/bin-paths',
'./out/sizes'
]
const promises = directories.map(path => fs.mkdirp(absolutePath(path)))

await Promise.all(promises)
})

describe('invalid input', () => {
Expand Down Expand Up @@ -74,26 +82,14 @@ describe('simple-thumbnail creates thumbnails for videos', () => {
const filePath = absolutePath('./data/bunny.webm')

it('creates thumbnails for files saved on disk', async () => {
try {
await genThumbnail(filePath, absolutePath('./out/storage/disk.png'), tinySize)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(filePath, absolutePath('./out/storage/disk.png'), tinySize)
})

// Note: mp4 does not work with read streams, hence the usage of .webm
it('creates thumbnails from read streams', async () => {
const stream = fs.createReadStream(filePath)

try {
await genThumbnail(stream, absolutePath('./out/storage/stream.png'), tinySize)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(stream, absolutePath('./out/storage/stream.png'), tinySize)
})

describe('creates thumbnails for remote files', () => {
Expand All @@ -109,13 +105,7 @@ describe('simple-thumbnail creates thumbnails for videos', () => {

it(`${protocol} protocol`, () => {
it('successfully generates thumbnails', async () => {
try {
await genThumbnail(fileUrl, absolutePath(`./out/storage/${protocol}.png`), tinySize)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(fileUrl, absolutePath(`./out/storage/${protocol}.png`), tinySize)
})
})
})
Expand All @@ -129,13 +119,7 @@ describe('simple-thumbnail creates thumbnails for videos', () => {
it(`can create thumbnails for ${format}`, async () => {
const filePath = absolutePath(`./data/bunny.${format}`)

try {
await genThumbnail(filePath, absolutePath(`./out/input-formats/${format}.png`), tinySize)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(filePath, absolutePath(`./out/input-formats/${format}.png`), tinySize)
})
})
})
Expand All @@ -146,75 +130,77 @@ describe('simple-thumbnail creates thumbnails for videos', () => {

sizes.forEach((size) => {
it(`handles sizes of the form ${size}`, async () => {
try {
await genThumbnail(
filePath,
absolutePath(`./out/sizes/size-${size.replace('%', '')}.png`),
size
)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(
filePath,
absolutePath(`./out/sizes/size-${size.replace('%', '')}.png`),
size
)
})
})
})

describe('alternative ffmpeg binary paths', () => {
const filePath = absolutePath('./data/bunny.mp4')

it('operates correctly when path specified by environment variable', async () => {
process.env.FFMPEG_PATH = '/usr/bin/ffmpeg'

await genThumbnail(
filePath,
absolutePath('./out/bin-paths/env.png'),
tinySize
)

process.env.FFMPEG_PATH = ''
})

it('operates correctly when path specified by config object', async () => {
await genThumbnail(
filePath,
absolutePath('./out/bin-paths/conf.png'),
tinySize,
{ path: '/usr/bin/ffmpeg' }
)
})
})

describe('thumbnail output image formats', () => {
const filePath = absolutePath('./data/bunny.webm')
const formats = ['gif', 'jpg', 'png']

formats.forEach((format) => {
it(`can create ${format} images`, async () => {
try {
await genThumbnail(
filePath,
absolutePath(`./out/image-formats/${format}.${format}`),
tinySize
)
} catch (err) {
console.log(err)

expect.fail()
}
await genThumbnail(
filePath,
absolutePath(`./out/image-formats/${format}.${format}`),
tinySize
)
})
})
})

describe('thumbnail correctness', () => {
it('produces thumbnail images that are identical to expected output', async () => {
const config = { tolerance: 5 }

const storageFiles = await fs.readdir(absolutePath('./out/storage'))
const inputFormatFiles = await fs.readdir(absolutePath('./out/input-formats'))
const sizeFiles = await fs.readdir(absolutePath('./out/sizes'))

const storagePromises = storageFiles
.map(file => looksSame(
absolutePath('./expected/tiny.png'),
absolutePath(`./out/storage/${file}`),
config
))

const inputFormatPromises = inputFormatFiles
.map(file => looksSame(
absolutePath('./expected/tiny.png'),
absolutePath(`./out/input-formats/${file}`),
config
))

const sizePromises = sizeFiles
.map(file => looksSame(
absolutePath(`./expected/${file}`),
absolutePath(`./out/sizes/${file}`),
const directories = [
'./out/storage',
'./out/input-formats',
'./out/sizes',
'./out/bin-paths'
]

const nestedPromises = directories.map(async (path) => {
const files = await fs.readdir(absolutePath(path))

return files.map(file => looksSame(
absolutePath(`./expected/${path === './out/sizes' ? file : 'tiny.png'}`),
absolutePath(`${path}/${file}`),
config
))
})

try {
const results = await Promise.all(
storagePromises.concat(inputFormatPromises, sizePromises)
)
const results = await Promise.all(flatten(nestedPromises))

expect(results.every(x => x)).to.be.true()
} catch (err) {
Expand Down

0 comments on commit 5a70632

Please sign in to comment.