From 24087ba4b0193dcdbd36d2e7e8a37b70f6392350 Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Sun, 21 Sep 2014 22:42:08 +0200 Subject: [PATCH] add .remove() docs, api and tests --- README.md | 19 ++++++++++++++++--- lib/imager.js | 47 +++++++++++++++++++++++++++++++++++++++-------- test/test.js | 23 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 511bb8f..a81aa80 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 => // { @@ -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 @@ -142,6 +154,7 @@ $ npm test - Support base64 image uploads - Implement `.remove()` - Implement `.regenerate()` +- Test for the api's for rackspace ## License diff --git a/lib/imager.js b/lib/imager.js index 4315b27..ff17fe9 100644 --- a/lib/imager.js +++ b/lib/imager.js @@ -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]; @@ -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 }); @@ -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++) { @@ -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 * @@ -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 * diff --git a/test/test.js b/test/test.js index 04d5bbf..5d406de 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); + }); + }); +});