Skip to content

Commit d939f7c

Browse files
committed
Rebased on v5.0.0
1 parent b5cfcf6 commit d939f7c

File tree

4 files changed

+42
-67
lines changed

4 files changed

+42
-67
lines changed

lib/handlers/get.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ const allow = require('./allow')
1414
const translate = require('../utils.js').translate
1515
const error = require('../http-error')
1616

17-
const RDFs = require('../ldp').RDF_MIME_TYPES
18-
// const LDP = require('../ldp')
19-
// const RDFs = LDP.mimeTypesAsArray()
17+
const RDFs = require('../ldp').mimeTypesAsArray()
2018

2119
async function handler (req, res, next) {
2220
const ldp = req.app.locals.ldp

lib/handlers/put.js

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ module.exports = handler
33
const bodyParser = require('body-parser')
44
const debug = require('debug')('solid:put')
55
const getContentType = require('../utils').getContentType
6+
const HTTPError = require('../http-error')
7+
const { stringToStream } = require('../utils')
8+
const LDP = require('../ldp')
69

710
async 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+
}

lib/ldp.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const parse = require('./utils').parse
1717
const fetch = require('node-fetch')
1818
const { promisify } = require('util')
1919

20-
// TODO: apply new Set
21-
const RDF_MIME_TYPES = [
20+
const RDF_MIME_TYPES = new Set([
2221
'text/turtle', // .ttl
2322
'text/n3', // .n3
2423
'text/html', // RDFa
@@ -204,16 +203,16 @@ class LDP {
204203
return await this.put(path, stream, contentType)
205204
}
206205

207-
// isValidRdf (body, requestUri, contentType) {
208-
// const resourceGraph = $rdf.graph()
209-
// try {
210-
// $rdf.parse(body, resourceGraph, requestUri, contentType)
211-
// } catch (err) {
212-
// debug.handlers('VALIDATE -- Error parsing data: ' + err)
213-
// return false
214-
// }
215-
// return true
216-
// }
206+
isValidRdf (body, requestUri, contentType) {
207+
const resourceGraph = $rdf.graph()
208+
try {
209+
$rdf.parse(body, resourceGraph, requestUri, contentType)
210+
} catch (err) {
211+
debug.ldp('VALIDATE -- Error parsing data: ' + err)
212+
return false
213+
}
214+
return true
215+
}
217216

218217
async put (url, stream, contentType) {
219218
// PUT requests not supported on containers. Use POST instead

test/integration/http-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,10 @@ describe('HTTP APIs', function () {
473473
.expect(hasHeader('acl', 'baz.ttl' + suffixAcl))
474474
.expect(201, done)
475475
})
476-
it('should fail when trying to put to a container',
476+
it('should return 409 code when trying to put to a container',
477477
function (done) {
478478
server.put('/')
479-
.expect(405, done)
479+
.expect(409, done)
480480
}
481481
)
482482
// Cleanup

0 commit comments

Comments
 (0)