@@ -3,14 +3,25 @@ module.exports = handler
33const bodyParser = require ( 'body-parser' )
44const debug = require ( 'debug' ) ( 'solid:put' )
55const getContentType = require ( '../utils' ) . getContentType
6+ const HTTPError = require ( '../http-error' )
7+ const { stringToStream } = require ( '../utils' )
8+ const LDP = require ( '../ldp' )
69
710async function handler ( req , res , next ) {
8- const ldp = req . app . locals . ldp
911 debug ( req . originalUrl )
1012 res . header ( 'MS-Author-Via' , 'SPARQL' )
1113
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
1223 try {
13- await ldp . put ( req , req , getContentType ( req . headers ) )
24+ await ldp . put ( req , stream , getContentType ( req . headers ) )
1425 debug ( 'succeded putting the file' )
1526
1627 res . sendStatus ( 201 )
@@ -22,51 +33,18 @@ async function handler (req, res, next) {
2233 }
2334}
2435
25- // function handler (req, res, next) {
26- // const ldp = req.app.locals.ldp
27- // debug(req.originalUrl)
28- // res.header('MS-Author-Via', 'SPARQL')
29- //
30- // const contentType = req.get('content-type')
31- // if (ldp.mimetypeIsRdf(contentType)) {
32- // return bodyParser.text({ type: () => true })(req, res, () => putText(req, res, next))
33- // }
34- // return putStream(req, res, next)
35- // }
36- //
37- //}
38- //
39- //function isAclFile (req) {
40- // const originalUrlParts = req.originalUrl.split('.')
41- // return originalUrlParts[originalUrlParts.length - 1] === 'acl'
42- //}
43- //
44- // function putText (req, res, next) {
45- // const ldp = req.app.locals.ldp
46- // const contentType = req.get('content-type')
47- // const requestUri = `${req.protocol}//${req.get('host')}${req.originalUrl}`
48- // if (ldp.isValidRdf(req.body, requestUri, contentType)) {
49- // return ldp.putText(req.hostname, req.path, req.body, handleCallback(res, next))
50- // }
51- // next(error(400, 'RDF file contains invalid syntax'))
52- // }
53- //
54- // function putStream (req, res, next) {
55- // const ldp = req.app.locals.ldp
56- // ldp.putStream(req.hostname, req.path, req, handleCallback(res, next))
57- // }
58- //
59- // function handleCallback (res, next) {
60- // return function (err) {
61- // if (err) {
62- // debug('error putting the file:' + err.message)
63- // err.message = 'Can\'t write file: ' + err.message
64- // return next(err)
65- // }
66- //
67- // debug('succeded putting the file')
68- //
69- // res.sendStatus(201)
70- // return next()
71- // }
72- // }
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