11module . exports = handler
22
3- var debug = require ( '../debug' ) . handlers
3+ var Busboy = require ( 'busboy' )
4+ var each = require ( 'async' ) . each
5+ var debug = require ( 'debug' ) ( 'ldnode:post' )
46var header = require ( '../header' )
57var patch = require ( './patch' )
68var error = require ( '../http-error' )
@@ -12,52 +14,80 @@ function handler (req, res, next) {
1214 // Handle SPARQL(-update?) query
1315 if ( contentType === 'application/sparql' ||
1416 contentType === 'application/sparql-update' ) {
15- debug ( 'POST -- Handling sparql query via PATCH ' )
17+ debug ( 'switching to sparql query' )
1618 return patch ( req , res , next )
1719 }
1820
21+ // Handle container path
1922 var containerPath = req . path
20- debug ( 'POST -- On parent: ' + containerPath )
21-
22- // Not a container
2323 if ( containerPath [ containerPath . length - 1 ] !== '/' ) {
2424 containerPath += '/'
2525 }
2626
27- debug ( 'POST -- Content Type: ' + contentType )
28-
29- var linkHeader = header . parseMetadataFromHeader ( req . get ( 'Link' ) )
30-
27+ // Check if container exists
3128 ldp . exists ( req . hostname , containerPath , function ( err , stats ) {
3229 if ( err ) {
3330 return next ( error ( err , 'Container not valid' ) )
3431 }
3532
33+ // Check if container is a directory
3634 if ( ! stats . isDirectory ( ) ) {
37- debug ( 'POST -- Path is not a container' )
38- res . set ( 'Allow' , 'GET,HEAD,PUT,DELETE' )
35+ debug ( 'path is not a container, 405!' )
3936 return next ( error ( 405 , 'Requested resource is not a container' ) )
4037 }
4138
42- // TODO: possibly package this in ldp.post
43- ldp . getAvailablePath ( req . hostname , containerPath , req . get ( 'Slug' ) , function ( resourcePath ) {
44- debug ( 'POST -- Will create at: ' + resourcePath )
45- var meta = ''
46- if ( linkHeader . isBasicContainer ) {
47- resourcePath += '/'
48- meta = ldp . suffixMeta
49- }
39+ // Dispatch to the right handler
40+ if ( contentType === 'multipart/form-data' ) {
41+ multi ( req , res , next )
42+ } else {
43+ one ( req , res , next )
44+ }
45+ } )
46+
47+ function multi ( ) {
48+ debug ( 'receving multiple files' )
49+ var busboy = new Busboy ( { headers : req . headers } )
50+ var files = [ ]
5051
51- ldp . put ( req . hostname , resourcePath + meta , req . text , function ( err ) {
52+ busboy . on ( 'file' , function ( fieldname , file , filename , encoding , mimetype ) {
53+ debug ( 'one file received via multipart: ' + filename )
54+ files . push ( { stream : file , name : filename } )
55+ } )
56+ busboy . on ( 'finish' , function ( ) {
57+ each (
58+ files ,
59+ function ( file , callback ) {
60+ ldp . post (
61+ req . hostname ,
62+ containerPath ,
63+ file . filename ,
64+ file . stream ,
65+ false ,
66+ callback )
67+ } , function ( err ) {
68+ debug ( 'done storing files' + ( err ? 'with error' + err . message : 'with no error' ) )
69+ res . sendStatus ( err ? 500 : 200 )
70+ } )
71+ } )
72+ }
73+
74+ function one ( ) {
75+ debug ( 'receving one file' )
76+ var linkHeader = header . parseMetadataFromHeader ( req . get ( 'Link' ) )
77+ ldp . post (
78+ req . hostname ,
79+ containerPath ,
80+ req . get ( 'Slug' ) ,
81+ req ,
82+ linkHeader . isBasicContainer ,
83+ function ( err , resourcePath ) {
5284 if ( err ) {
53- return next ( err )
85+ next ( err )
5486 }
55-
5687 header . addLinks ( res , linkHeader )
5788 res . set ( 'Location' , resourcePath )
5889 res . sendStatus ( 201 )
59- return next ( )
6090 } )
61- } )
62- } )
91+ }
6392}
93+
0 commit comments