Skip to content

Commit

Permalink
postFile: config function is asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinND committed Jul 17, 2014
1 parent eb89021 commit fefae73
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions lib/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

var request = require('supertest');
var util = require('util');
var async = require('async');
var rarity = require('rarity');

var isFunction = require('./helpers/is-function.js');
var isMongoId = require('./helpers/is-mongo-id.js');
Expand Down Expand Up @@ -188,41 +190,61 @@ module.exports = function addMappings(Anyfetch) {
/**
* Add a file to a previously uploaded document
*
* @param {Object} Must at least contain a `file` key, which can either be a stream (e.g. `fs.createReadStream`) or a Buffer object. Can also contains a `contentType` key (for MIME type), and a `filename`.
* @param {Object|function(cb)} config Must at least contain a `file` key, which can either be a stream (e.g. `fs.createReadStream`) or a Buffer object. Can also contains a `contentType` key (for MIME type), and a `filename`.
* config can be a function, in which case it is executed with a callback. It must be called with (err, config).
* @param {Function} cb(err, res) Callback with error if any.
*
* @warning: unfortunately, due to the variety of Stream, we can't type-check, so unexpected errors will occur if you specify weird file parameters.
*/
return function postFile(config, cb) {
return function postFile(config, finalCb) {
var self = this;

if(idMustBeValid && (!id || !isMongoId(id))) {
return cb(new Error('Argument error in ' + extendedEndpoint + '.postFile: the first argument must be a valid MongoDB ObjectId'));
return finalCb(new Error('Argument error in ' + extendedEndpoint + '.postFile: the first argument must be a valid MongoDB ObjectId'));
}

if(isFunction(config)) {
config = config();
}
if(!config.file) {
return cb(new Error('The config parameter must contain a `file` key'));
}
var makeRequest = function(config, cb) {
if(!config.file) {
return cb(new Error('The config parameter must contain a `file` key'));
}

var endpoint;
if(configuration.apiDescriptors[extendedEndpoint].requireId) {
endpoint = baseEndpoint.replace('{id}', id) + '/file';
}
if(configuration.apiDescriptors[extendedEndpoint].requireIdentifier) {
endpoint = baseEndpoint.replace('{identifier}', id) + '/file';
}
var endpoint;
if(configuration.apiDescriptors[extendedEndpoint].requireId) {
endpoint = baseEndpoint.replace('{id}', id) + '/file';
}
if(configuration.apiDescriptors[extendedEndpoint].requireIdentifier) {
endpoint = baseEndpoint.replace('{identifier}', id) + '/file';
}

var options = {
filename: config.filename || '',
contentType: config.contentType || ''
};

var options = {
filename: config.filename || '',
contentType: config.contentType || ''
request(self.apiUrl).post(endpoint)
.set('Authorization', self.authHeader)
.attach('file', config.file, options)
.expect(204)
.end(cb);
};

request(this.apiUrl).post(endpoint)
.set('Authorization', this.authHeader)
.attach('file', config.file, options)
.expect(204)
.end(cb);
// Allow the config function to deliver the config asynchronously
// Warning: we can't use async, otherwise we could loose some data from a ReadStream
if(isFunction(config)) {
config(function(err, conf) {
if(err) {
return finalCb(err);
}
if(!conf) {
return finalCb(new Error('The `config` function must invoke the cb with (err, config)'));
}

makeRequest(config, finalCb);
});
}
else {
makeRequest(config, finalCb);
}
};
};

Expand Down

0 comments on commit fefae73

Please sign in to comment.