Skip to content

Commit

Permalink
add .remove() docs, api and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madhums committed Sep 21, 2014
1 parent 415c935 commit 24087ba
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports.storages = {
```js
var Imager = require('imager');
var config = require('imager-config.js');
var config = require('./imager-config.js');
var imager = new Imager(config.variants.item, config.storages.amazon);
// You can also pass only the storage without a variant which will simply
// upload the original image
Expand All @@ -118,7 +118,7 @@ var imager = new Imager(config.variants.item, config.storages.amazon);
```js
var config = require('./imager-config.js');
var imager = new Imager(config.variants.item, config.storage);
var imager = new Imager(config.variants.item, config.storages.amazon);
imager.upload(files, function (err, avatar) {
// avatar =>
// {
Expand All @@ -128,7 +128,19 @@ imager.upload(files, function (err, avatar) {
});
```
### .remove()
### .remove(files, callback)
`files` is an array of files or a single file. A file should be the file name of the image on the storage. `callback` accepts `err` as an argument.
```js
var config = require('./imager-config.js');
var imager = new Imager(config.storages.amazon);
var files = ['file-1.png']; // or just 'file-1.png'
imager.remove(files, function (err) {
});
```
### .regenerate()
## Tests
Expand All @@ -142,6 +154,7 @@ $ npm test
- Support base64 image uploads
- Implement `.remove()`
- Implement `.regenerate()`
- Test for the api's for rackspace
## License
Expand Down
47 changes: 39 additions & 8 deletions lib/imager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ function Imager (presets, storage) {
* upload
*
* @param {Array|String} files
* @param {Function} cb
* @param {Function} fn
* @api public
*/

Imager.prototype.upload = function (files, fn) {
var self = this;

if (typeof files === 'string') {
files = [files];
}

var presets = self.presets;
if (typeof files === 'string') files = [files];

presets = Object.keys(presets).map(function (key) {
presets[key].__name = key;
return presets[key];
Expand All @@ -70,7 +67,6 @@ Imager.prototype.upload = function (files, fn) {
// modified files having the same object structure
var _files = yield files.map(info);
var arr = [];

_files.forEach(function (file) {
presets.forEach(function (preset) {
arr.push({ file: file, preset: preset });
Expand All @@ -79,7 +75,6 @@ Imager.prototype.upload = function (files, fn) {

try {
var uploaded = yield arr.map(upload.bind(self));

// group by preset
var hash = {};
for (var i = 0; i < uploaded.length; i++) {
Expand All @@ -94,6 +89,28 @@ Imager.prototype.upload = function (files, fn) {
})();
};

/**
* remove
*
* @param {Array|String} files
* @param {Function} fn
* @api public
*/

Imager.prototype.remove = function (files, fn) {
var self = this;
if (typeof files === 'string') files = [files];

co(function *() {
try {
yield files.map(remove.bind(self));
fn(null);
} catch (err) {
fn(err);
}
})();
};

/**
* upload
*
Expand Down Expand Up @@ -129,6 +146,20 @@ function upload (obj) {
};
}

/**
* remove
*
* @param {Object} file
* @api private
*/

function remove (file) {
var self = this;
return function (fn) {
self.client.removeFile(self.container, file, fn);
};
}

/**
* info
*
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@ describe('imager.upload', function () {
});
});
});

describe('imager.remove', function () {
it('should remove the uploaded files', function (done) {
var imager = new Imager(config.storages.amazon);
var files = [
'original_image-1.png',
'original_image-2.jpg'
];
imager.remove(files, function (err) {
should.not.exist(err);
done();
});
});

it('should remove a single uploaded file', function (done) {
var imager = new Imager(config.storages.amazon);
var file = 'original_image-2.jpg';
imager.remove(file, function (err) {
should.not.exist(err);
done();
});
});
});

0 comments on commit 24087ba

Please sign in to comment.