Refactor uploads

This commit is contained in:
Jonathan Cremin 2016-05-25 21:03:07 +01:00
parent 75a46212da
commit f59b9a5d22
14 changed files with 297 additions and 269 deletions

View file

@ -1,35 +1,35 @@
import fs from 'fs';
import path from 'path';
import fs from 'mz/fs';
import { join } from 'path';
import assert from 'assert';
import tmp from 'tmp';
import resize from '../../lib/resize';
import imageType from 'image-type';
import sizeOf from 'image-size';
function testResize(path, done) {
const size = sizeOf(path);
resize(path, size.type, size, {width: 100, height: 100}).then((image) => {
const tmpFile = tmp.tmpNameSync() + '.' + size.type;
fs.writeFile(tmpFile, image).then(() => {
const newSize = sizeOf(fs.readFileSync(tmpFile));
assert(newSize.type === size.type);
done();
});
});
}
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() + '.jpg';
fs.writeFileSync(tmpFile, imageBuffer);
const type = imageType(fs.readFileSync(tmpFile));
assert(type.ext === 'jpg');
it('should resize a jpg', (done) => {
const path = join(__dirname, '..', 'fixtures', 'utah-arches.jpg');
testResize(path, done);
});
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 png', (done) => {
const path = join(__dirname, '..', 'fixtures', 'app-icon.png');
testResize(path, done);
});
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');
it('should resize a gif', (done) => {
const path = join(__dirname, '..', 'fixtures', 'kim.gif');
testResize(path, done);
});
});