Comparing version 0.1.8 to 0.1.9
0.1.9 / 2013-04-22 | ||
================== | ||
* Set the default ACL to 'public-read' for files uploaded to Amazon | ||
* add tests | ||
* add travis ci | ||
* refactor imager for better error handling and flexible syntax | ||
* remove makefile, add mocha to npm test | ||
* update node engine support - os.tmpDir works only > 0.8.x | ||
0.1.8 / 2013-04-21 | ||
@@ -3,0 +13,0 @@ ================== |
@@ -0,1 +1,2 @@ | ||
/*! | ||
@@ -40,12 +41,16 @@ * node-imager | ||
var Imager = module.exports = function Imager (config, storage) { | ||
if (config === undefined) { | ||
if (typeof config !== 'object') { | ||
throw new Error('Please provide the config'); | ||
} else if (!config) { | ||
throw new Error('Please provide the config'); | ||
} | ||
if (storage === undefined) { | ||
if (typeof storage === 'undefined') { | ||
throw new Error('Please specify the storage'); | ||
} | ||
if (config.storage[storage] === undefined) { | ||
throw new Error('The storage you have specified does not exist in the config you have provided'); | ||
if (config.storage && config.storage[storage] === undefined) { | ||
throw new Error('The storage you have specified does not exist'); | ||
} else if (!config.storage) { | ||
throw new Error('Please specify a storage'); | ||
} | ||
@@ -75,12 +80,2 @@ | ||
var self = this; | ||
var getFileInfo = function (file, cb) { | ||
var f = { | ||
size: fs.statSync(file).size, | ||
type: mime.lookup(file), | ||
name: file.split('/')[file.split('/').length - 1], | ||
path: file | ||
}; | ||
file = f; | ||
cb(null, file); | ||
}; | ||
@@ -92,4 +87,3 @@ // to check if the files are local | ||
}); | ||
} | ||
else if (!Array.isArray(files) && typeof files === 'string') { | ||
} else if (!Array.isArray(files) && typeof files === 'string') { | ||
files = files.split(); | ||
@@ -101,8 +95,9 @@ async.map(files, getFileInfo, function (err, result) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('Please provide a callback'); | ||
if (!variant) { | ||
variant = callback | ||
callback = function () {} | ||
} | ||
if (typeof variant !== 'string' && !this.config.variants.default) { | ||
throw new Error('Please specify a proper variant OR provide a default variant in your imager config'); | ||
throw new Error('Please specify a proper variant OR provide a default'); | ||
} | ||
@@ -142,3 +137,8 @@ | ||
remove: function (files, callback, variant) { | ||
if (!variant && !this.config.variants.default) { | ||
if (!variant) { | ||
variant = callback | ||
callback = function () {} | ||
} | ||
if (typeof variant !== 'string' && !this.config.variants.default) { | ||
throw new Error('Please specify a proper variant to remove the files'); | ||
@@ -151,2 +151,6 @@ } | ||
if (typeof variant === 'string' && !this.config.variants[variant]) { | ||
throw new Error('Please provide a variant which you have specified in the config file'); | ||
} | ||
var self = this; | ||
@@ -388,3 +392,3 @@ | ||
client.putFile(file, remoteFile, function (err, res) { | ||
client.putFile(file, remoteFile, { 'x-amz-acl': 'public-read' }, function (err, res) { | ||
if (err) return cb(err); | ||
@@ -510,1 +514,20 @@ log(remoteFile + ' uploaded'); | ||
} | ||
/** | ||
* Get file info | ||
* | ||
* @param {String} file | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
function getFileInfo (file, cb) { | ||
var f = { | ||
size: fs.statSync(file).size, | ||
type: mime.lookup(file), | ||
name: file.split('/')[file.split('/').length - 1], | ||
path: file | ||
}; | ||
file = f; | ||
cb(null, file); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Easy way to resize, crop and upload images to Rackspace cloudfiles and Amazon S3", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"keywords": ["upload", "image", "graphicsmagick", "imagemagick", "rackspace", "cloudfiles", "resize", "file", "crop", "amazon", "s3"], | ||
@@ -15,3 +15,3 @@ "homepage": "https://github.com/madhums/node-imager", | ||
"engines": { | ||
"node": ">= 0.4.1" | ||
"node": ">= 0.8.x" | ||
}, | ||
@@ -31,4 +31,4 @@ "dependencies": { | ||
"scripts": { | ||
"test": "make test" | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --timeout 500000 --reporter spec test/test.js" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/madhums/node-imager.png)](https://travis-ci.org/madhums/node-imager) | ||
## Imager | ||
@@ -69,11 +71,11 @@ | ||
## Debugging | ||
If you specify `debug: true` in the imager config, you can see the logs of uploaded / removed files. | ||
## To-do's | ||
* Write more tests | ||
## Tests | ||
```sh | ||
$ npm test | ||
``` | ||
**credits :** Initially inspired by [Alleup](https://github.com/tih-ra/alleup) | ||
## License | ||
@@ -80,0 +82,0 @@ (The MIT License) |
198
test/test.js
@@ -1,36 +0,155 @@ | ||
var Imager = require('../index') | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var Imager = require('../') | ||
, should = require('should') | ||
, imagerConfig = require('./imager') | ||
, imager = new Imager(imagerConfig, 'Rackspace') | ||
, filesToRemove | ||
, files = [__dirname+'/fixtures/single.jpg', __dirname+'/fixtures/multiple-3.png'] | ||
var file | ||
describe('new Imager()', function () { | ||
it('should throw if no parameters are passed', function () { | ||
(function () { | ||
var imager = new Imager() | ||
}).should.throw('Please provide the config') | ||
}) | ||
it('should throw if no config is provided', function () { | ||
(function () { | ||
var imager = new Imager('Rackspace') | ||
}).should.throw('Please provide the config') | ||
}) | ||
it('should throw if config is null or undefined', function () { | ||
(function () { | ||
var imager = new Imager(null, 'Rackspace') | ||
}).should.throw('Please provide the config') | ||
}) | ||
it('should throw if config doesnt have storage', function () { | ||
(function () { | ||
var imager = new Imager({}, 'Rackspace') | ||
}).should.throw('Please specify a storage') | ||
}) | ||
it('given proper config - should create the instance', function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.config.should.have.property('variants') | ||
imager.config.should.have.property('storage') | ||
should.strictEqual('Rackspace', imager.storage) | ||
imager.uploadedFiles.should.be.empty | ||
}) | ||
}) | ||
describe('Imager', function () { | ||
describe('Single image from disk', function () { | ||
it('should upload to rackspace', function (done) { | ||
imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload(__dirname+'/fixtures/single.jpg', function(err, cdnUri, files){ | ||
files.should.be.a('object') | ||
files.length.should.be.equal(1) | ||
done() | ||
}, 'items') | ||
describe('#upload()', function () { | ||
it('should throw if no variant is provided', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload() | ||
}).should.throw('Please specify a proper variant OR provide a default') | ||
}) | ||
}) | ||
describe('Multiple images from disk', function () { | ||
it('should upload to rackspace', function (done) { | ||
var arr = [__dirname+'/fixtures/single.jpg', __dirname+'/fixtures/multiple-3.png'] | ||
imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload(arr, function(err, cdnUri, files){ | ||
files.should.be.a('object') | ||
files.length.should.be.equal(2) | ||
filesToRemove = files | ||
it('should throw if the variant is not in the config', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload([], 'abc') | ||
}).should.throw('Please provide a variant which you have specified in the config file') | ||
}) | ||
it('should do nothing if the files are not provided', function (done) { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload([], function (err, cdnUri, files) { | ||
should.not.exist(err) | ||
should.not.exist(cdnUri) | ||
files.should.be.empty | ||
done() | ||
}, 'items') | ||
}) | ||
describe('With local files', function () { | ||
it('should throw if the file paths are incorrect', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload(['abc.png'], 'items') | ||
}).should.throw(/ENOENT/) | ||
}) | ||
it('should accept path of the file as string', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload(__dirname+'/fixtures/single.jpg', 'items') | ||
}).should.not.throw() | ||
}) | ||
describe('With invalid username/secret', function () { | ||
it('should throw with incorrect key/secret', function (done) { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
// unset the username | ||
imager.config.storage.Rackspace.auth.username = 'xyz123' | ||
imager.upload(files, function (err, cdnUri, uploaded) { | ||
should.exist(err) | ||
err.toString().should.equal('Error: Cannot make Rackspace request if not authorized') | ||
done() | ||
}, 'items') | ||
}) | ||
}) | ||
describe('With valid username/secret', function () { | ||
it('should upload the images to the cloud', function (done) { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.upload(files, function (err, cdnUri, uploaded) { | ||
// uncomment this to test | ||
// 1. make sure you substitute key/secret in ./imager.js | ||
// 2. uncomment the below lines | ||
/* | ||
should.not.exist(err) | ||
should.exist(cdnUri) | ||
uploaded.should.not.be.empty | ||
uploaded.should.have.lengthOf(2) | ||
*/ | ||
done() | ||
}, 'items') | ||
}) | ||
}) | ||
}) | ||
describe('With form files', function () { | ||
// write tests for form uploads | ||
}) | ||
}) | ||
describe('Remove images', function () { | ||
it('should remove the images from Rackspace', function (done) { | ||
imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove(filesToRemove, function(err){ | ||
describe('#remove()', function () { | ||
it('should throw if no variant is provided', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove() | ||
}).should.throw('Please specify a proper variant to remove the files') | ||
}) | ||
it('should not throw if callback is not provided', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove([], 'items') | ||
}).should.not.throw() | ||
}) | ||
it('should throw if the variant is not in the config', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove([], 'abc') | ||
}).should.throw('Please provide a variant which you have specified in the config file') | ||
}) | ||
it('should do nothing if the files are not provided', function (done) { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove([], function (err) { | ||
should.not.exist(err) | ||
@@ -40,3 +159,34 @@ done() | ||
}) | ||
it('should accept string as a file to remove', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove('123.jpg', 'items') | ||
}).should.not.throw() | ||
}) | ||
describe('With a file that does not exist in the cloud', function () { | ||
it('should not throw', function () { | ||
(function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
imager.remove(['111.jpg'], 'items') | ||
}).should.not.throw() | ||
}) | ||
}) | ||
describe('With a file that exists in the cloud', function () { | ||
it('should remove the files successfully', function () { | ||
var imager = new Imager(imagerConfig, 'Rackspace') | ||
// uncomment the below and make sure 123.jpg already exists | ||
/* | ||
imager.remove(['123.jpg'], function (err) { | ||
should.not.exist(err) | ||
done() | ||
}, 'items') | ||
*/ | ||
}) | ||
}) | ||
}) | ||
}) | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
679
90
171034
12