Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions lib/handlers/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = handler

var Busboy = require('busboy')
var each = require('async').each
var debug = require('debug')('ldnode:post')
var header = require('../header')
var patch = require('./patch')
Expand All @@ -10,7 +9,7 @@ var error = require('../http-error')
function handler (req, res, next) {
var ldp = req.app.locals.ldp
var contentType = req.get('content-type')

debug('content-type is ', contentType)
// Handle SPARQL(-update?) query
if (contentType === 'application/sparql' ||
contentType === 'application/sparql-update') {
Expand All @@ -37,7 +36,7 @@ function handler (req, res, next) {
}

// Dispatch to the right handler
if (contentType === 'multipart/form-data') {
if (req.is('multipart/form-data')) {
multi(req, res, next)
} else {
one(req, res, next)
Expand All @@ -46,30 +45,34 @@ function handler (req, res, next) {

function multi () {
debug('receving multiple files')
var busboy = new Busboy({ headers: req.headers })
var files = []

var busboy = new Busboy({ headers: req.headers })
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
debug('one file received via multipart: ' + filename)
files.push({stream: file, name: filename})
ldp.post(
req.hostname,
containerPath,
filename,
file,
false,
function (err) {
if (err) {
busboy.emit('error', err)
}
})
})
busboy.on('error', function (err) {
debug('error receiving the file: ' + err.message)
next(error(500, 'Error receiving the file'))
})

// Handled by backpressure of streams!
busboy.on('finish', function () {
each(
files,
function (file, callback) {
ldp.post(
req.hostname,
containerPath,
file.filename,
file.stream,
false,
callback)
}, function (err) {
debug('done storing files' + (err ? 'with error' + err.message : 'with no error'))
res.sendStatus(err ? 500 : 200)
next()
})
debug('done storing files')
res.sendStatus(200)
next()
})
req.pipe(busboy)
}

function one () {
Expand All @@ -85,6 +88,7 @@ function handler (req, res, next) {
if (err) {
return next(err)
}
debug('file stored on ' + resourcePath)
header.addLinks(res, linkHeader)
res.set('Location', resourcePath)
res.sendStatus(201)
Expand Down
2 changes: 1 addition & 1 deletion lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ LDP.prototype.get = function (host, reqPath, baseUri, includeBody, contentType,
ldp.stat(filename, function (err, stats) {
// File does not exist
if (err) {
return callback(error(err, 'Can\t find resource requested'))
return callback(error(err, 'Can\'t find resource requested'))
}

// Just return, since resource exists
Expand Down
24 changes: 24 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,28 @@ describe('HTTP APIs', function () {
.expect(200, done)
})
})
describe('POST (multipart)', function () {
it('should create as many files as the ones passed in multipart', function (done) {
server.post('/sampleContainer/')
.attach('timbl', __dirname + '/resources/timbl.jpg')
.attach('nicola', __dirname + '/resources/nicola.jpg')
.expect(200)
.end(function (err) {
if (err) return done(err)

var sizeNicola = fs.statSync(__dirname + '/resources/nicola.jpg').size
var sizeTim = fs.statSync(__dirname + '/resources/timbl.jpg').size
var sizeNicolaLocal = fs.statSync(__dirname + '/resources/sampleContainer/nicola.jpg').size
var sizeTimLocal = fs.statSync(__dirname + '/resources/sampleContainer/timbl.jpg').size

if (sizeNicola === sizeNicolaLocal && sizeTim === sizeTimLocal) {
return done()
} else {
return done(new Error('Either the size (remote/local) don\'t match or files are not stored'))
}
rm('sampleContainer/nicola.jpg')
rm('sampleContainer/timbl.jpg')
})
})
})
})
Binary file added test/resources/nicola.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/timbl.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.