diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 49a188f..83e8a1b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 516ad4a..0000000 --- a/circle.yml +++ /dev/null @@ -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 diff --git a/lib/resize.js b/lib/resize.js index 7e44e07..a432f3f 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -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); + }); + }); + }); + }); } diff --git a/package.json b/package.json index 935a61e..e0f1f35 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fixtures/app-icon.png b/test/fixtures/app-icon.png new file mode 100644 index 0000000..388173a Binary files /dev/null and b/test/fixtures/app-icon.png differ diff --git a/test/fixtures/kim.gif b/test/fixtures/kim.gif new file mode 100644 index 0000000..454549c Binary files /dev/null and b/test/fixtures/kim.gif differ diff --git a/test/unit/image-resize.spec.js b/test/unit/image-resize.spec.js index ee6ddc5..791fb81 100644 --- a/test/unit/image-resize.spec.js +++ b/test/unit/image-resize.spec.js @@ -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'); }); }); diff --git a/test/web/file.spec.js b/test/web/file.spec.js index cdd1c4d..3453ca3 100644 --- a/test/web/file.spec.js +++ b/test/web/file.spec.js @@ -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); });