Skip to content

Commit

Permalink
Merge pull request #40 from EvilDrW/master
Browse files Browse the repository at this point in the history
add single file get
  • Loading branch information
NicolasRitouet committed Nov 16, 2015
2 parents 9bb8b50 + ed4902f commit be67a72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
38 changes: 22 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Fileupload.prototype.handle = function (ctx, next) {
uploadDir = this.config.fullDirectory,
resultFiles = [],
remainingFile = 0,
storedObject = {},
storedProperties = {},
uniqueFilename = false,
subdir;

Expand Down Expand Up @@ -115,9 +115,9 @@ Fileupload.prototype.handle = function (ctx, next) {

// Store any param in the object
try {
storedObject[propertyName] = JSON.parse(req.query[propertyName]);
storedProperties[propertyName] = JSON.parse(req.query[propertyName]);
} catch (e) {
storedObject[propertyName] = req.query[propertyName];
storedProperties[propertyName] = req.query[propertyName];
}
}
}
Expand All @@ -128,6 +128,7 @@ Fileupload.prototype.handle = function (ctx, next) {
fs.rename(file.path, path.join(uploadDir, file.name), function(err) {
if (err) return processDone(err);
debug("File renamed after event.upload.run: %j", err || path.join(uploadDir, file.name));
var storedObject = _.clone(storedProperties);
storedObject.filename = file.name;
if (uniqueFilename) {
storedObject.originalFilename = file.originalFilename;
Expand Down Expand Up @@ -183,14 +184,20 @@ Fileupload.prototype.handle = function (ctx, next) {
return req.resume();
} else if (req.method === "GET") {

if (this.events.get) {
this.events.get.run(ctx, domain, function(err) {
if (err) return ctx.done(err);
self.get(ctx, next);
});
} else {
this.get(ctx, next);
}
this.get(ctx, function(err, result) {
if (err) return ctx.done(err);
else if (self.events.get) {
domain.data = result;
domain['this'] = result;

self.events.get.run(ctx, domain, function(err) {
if (err) return ctx.done(err);
ctx.done(null, result);
});
} else {
ctx.done(err, result);
}
});

} else if (req.method === "DELETE") {

Expand All @@ -210,12 +217,11 @@ Fileupload.prototype.handle = function (ctx, next) {

Fileupload.prototype.get = function(ctx, next) {
var self = this;
var id = ctx.url.split('/')[1];
if (id.length > 0)
ctx.query.id = id;

if (!ctx.query.id) {
self.store.find(ctx.query, function(err, result) {
ctx.done(err, result);
});
}
self.store.find(ctx.query, next);
};

// Delete a file
Expand Down
21 changes: 20 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,26 @@ describe('Integration tests for dpd-fileupload', function() {
done();
});
});


it('get one file info', function(done) {
request.get(endpoint, function(err, response, body) {
if (err) throw err;
body = JSON.parse(body);
expect(response.statusCode).to.be.equal(200);
expect(body).to.be.length(2);
expect(body[0].id).to.be.defined;

request.get(endpoint + body[0].id, function(err, response, body) {
body = JSON.parse(body);
expect(response.statusCode).to.be.equal(200);
expect(body).to.be.instanceof(Object);
expect(body.id).to.be.defined;
expect(body.filename).to.be.defined;
expect([imageFilename, txtFilename]).to.include(body.filename);
done();
});
});
});

it('delete the uploaded files', function(done) {
request.get(endpoint, function(err, response, body) {
Expand Down

0 comments on commit be67a72

Please sign in to comment.