Remove gm dependency and update tests.

This commit is contained in:
Jonathan Cremin 2016-05-24 21:33:09 +01:00
parent b1ceba7664
commit 75a46212da
8 changed files with 61 additions and 37 deletions

View file

@ -10,7 +10,7 @@ test:
stage: test
before_script:
- npm install
- node test/fixtures/mongo-user.js test/fixtures/mongo-file.js
- npm run init
script:
- npm test
tags:
@ -25,4 +25,3 @@ cache:
untracked: true
paths:
- node_modules
- web/public/jspm_packages

View file

@ -1,16 +0,0 @@
machine:
node:
version: iojs-3.2.0
test:
pre:
- mongo hostr test/fixtures/mongo-user.js test/fixtures/mongo-file.js
override:
- npm run cover
dependencies:
cache_directories:
- node_modules
- web/public/jspm_packages
post:
- npm run jspm

View file

@ -1,10 +1,33 @@
import debugname from 'debug';
const debug = debugname('hostr-api:resize');
import gm from 'gm';
import lwip from 'lwip';
import imageType from 'image-type';
const supported = ['jpg', 'png', 'gif'];
export default function(input, size) {
debug('Resizing');
const image = gm(input);
return image.resize(size.width, size.height, '>').stream();
const type = imageType(input);
if (!type.ext || supported.indexOf(type.ext) < 0) {
throw new Error('Not a supported image.');
}
return new Promise((resolve, reject) => {
lwip.open(input, type.ext, (errIn, image) => {
if (errIn) {
return reject(errIn);
}
image.cover(size.width, size.height, (errOut, resized) => {
if (errOut) {
return reject(errOut);
}
resized.toBuffer(type.ext, (errBuf, buffer) => {
resolve(buffer);
});
});
});
});
}

View file

@ -16,7 +16,7 @@
"init": "node -r babel/register -e \"require('./lib/storage')();\"",
"jspm": "jspm install",
"start": "npm run build && node -r babel/register app.js",
"test": "npm run test-seed && mocha -r babel/register test/**/*.spec.js",
"test": "npm run test-seed && mocha -r babel/register test/**/*.spec.js && mocha -r babel/register -r co-mocha test/unit/image-resize.spec.js",
"test-seed": "node test/fixtures/mongo-user.js && node test/fixtures/mongo-file.js",
"watch": "parallelshell \"npm run watch-js\" \"npm run watch-sass\" \"npm run watch-server\"",
"watch-js": "babel -Dw -m system -d web/public/build web/public/src",
@ -34,8 +34,9 @@
"debug": "~2.2.0",
"ejs": "~2.3.2",
"form-data": "^1.0.0-rc3",
"gm": "~1.18.1",
"http-errors": "~1.3.1",
"image-size": "^0.5.0",
"image-type": "^2.1.0",
"jspm": "~0.16.0",
"kcors": "~1.0.1",
"koa": "~1.0.0",
@ -52,6 +53,7 @@
"koa-statsd": "~0.0.2",
"koa-views": "~3.1.0",
"koa-websocket": "~1.1.0",
"lwip": "0.0.9",
"mime-types": "~2.1.5",
"moment": "~2.10.6",
"mongodb-promisified": "~1.0.3",
@ -69,6 +71,7 @@
},
"devDependencies": {
"babel-eslint": "^4.0.10",
"co-mocha": "^1.1.2",
"eslint": "~1.3.0",
"eslint-config-airbnb": "0.0.8",
"istanbul": "~0.3.18",

BIN
test/fixtures/app-icon.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
test/fixtures/kim.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

View file

@ -3,14 +3,33 @@ import path from 'path';
import assert from 'assert';
import tmp from 'tmp';
import resize from '../../lib/resize';
import imageType from 'image-type';
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'));
describe('Image resizing', function() {
it('should resize an image', function*() {
describe('Image resizing', () => {
it('should resize a jpg', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'utah-arches.jpg'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync();
fs.writeFileSync(tmpFile + '.jpg', imageBuffer);
assert(tmpFile);
const tmpFile = tmp.tmpNameSync() + '.jpg';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'jpg');
});
it('should resize a png', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'app-icon.png'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync() + '.png';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'png');
});
it('should resize a gif', function* resizeImage() {
const file = fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'kim.gif'));
const imageBuffer = yield resize(file, {height: 100, width: 100});
const tmpFile = tmp.tmpNameSync() + '.gif';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'gif');
});
});

View file

@ -1,5 +1,5 @@
import assert from 'assert';
import gm from 'gm';
import sizeOf from 'image-size';
import { agent } from 'supertest';
import app from '../../app';
@ -46,9 +46,7 @@ describe('hostr-web file', function() {
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
gm(response.body).size((err, size) => {
assert(size.width === 150);
});
assert(sizeOf(response.body).width === 150);
})
.end(done);
});
@ -61,9 +59,7 @@ describe('hostr-web file', function() {
.expect(200)
.expect('Content-type', 'image/jpeg')
.expect(function(response) {
gm(response.body).size((err, size) => {
assert(size.width === 970);
});
assert(sizeOf(response.body).width === 970);
})
.end(done);
});