11module . exports = handler
22
3+ const bodyParser = require ( 'body-parser' )
34const debug = require ( 'debug' ) ( 'solid:put' )
45const getContentType = require ( '../utils' ) . getContentType
6+ const HTTPError = require ( '../http-error' )
7+ const { stringToStream } = require ( '../utils' )
8+ const LDP = require ( '../ldp' )
59
610async function handler ( req , res , next ) {
7- const ldp = req . app . locals . ldp
811 debug ( req . originalUrl )
912 res . header ( 'MS-Author-Via' , 'SPARQL' )
1013
14+ const contentType = req . get ( 'content-type' )
15+ if ( LDP . mimeTypeIsRdf ( contentType ) && isAclFile ( req ) ) {
16+ return bodyParser . text ( { type : ( ) => true } ) ( req , res , ( ) => putAcl ( req , res , next ) )
17+ }
18+ return putStream ( req , res , next )
19+ }
20+
21+ async function putStream ( req , res , next , stream = req ) {
22+ const ldp = req . app . locals . ldp
1123 try {
12- await ldp . put ( req , req , getContentType ( req . headers ) )
24+ await ldp . put ( req , stream , getContentType ( req . headers ) )
1325 debug ( 'succeded putting the file' )
1426
1527 res . sendStatus ( 201 )
@@ -20,3 +32,19 @@ async function handler (req, res, next) {
2032 return next ( err )
2133 }
2234}
35+
36+ async function putAcl ( req , res , next ) {
37+ const ldp = req . app . locals . ldp
38+ const contentType = req . get ( 'content-type' )
39+ const requestUri = `${ req . protocol } //${ req . get ( 'host' ) } ${ req . originalUrl } `
40+ if ( ldp . isValidRdf ( req . body , requestUri , contentType ) ) {
41+ const stream = stringToStream ( req . body )
42+ return putStream ( req , res , next , stream )
43+ }
44+ next ( new HTTPError ( 400 , 'RDF file contains invalid syntax' ) )
45+ }
46+
47+ function isAclFile ( req ) {
48+ const originalUrlParts = req . originalUrl . split ( '.' )
49+ return originalUrlParts [ originalUrlParts . length - 1 ] === 'acl'
50+ }
0 commit comments