forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathput.js
More file actions
56 lines (49 loc) · 1.9 KB
/
Copy pathput.js
File metadata and controls
56 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module.exports = handler
const bodyParser = require('body-parser')
const debug = require('debug')('solid:put')
const getContentType = require('../utils').getContentType
const HTTPError = require('../http-error')
const { stringToStream } = require('../utils')
async function handler (req, res, next) {
debug(req.originalUrl)
res.header('MS-Author-Via', 'SPARQL')
const contentType = req.get('content-type')
if (isAuxiliary(req)) {
if (contentType === 'text/turtle') {
return bodyParser.text({ type: () => true })(req, res, () => putAuxiliary(req, res, next))
} else return next(new HTTPError(415, 'RDF file contains invalid syntax'))
}
return putStream(req, res, next)
}
// TODO could be renamed as putResource (it now covers container and non-container)
async function putStream (req, res, next, stream = req) {
const ldp = req.app.locals.ldp
try {
debug('test ' + req.get('content-type') + getContentType(req.headers))
await ldp.put(req, stream, getContentType(req.headers))
debug('succeded putting the file/folder')
res.sendStatus(201)
return next()
} catch (err) {
debug('error putting the file/folder:' + err.message)
err.message = 'Can\'t write file/folder: ' + err.message
return next(err)
}
}
// needed to avoid breaking access with bad acl
// or breaking containement triples for meta
function putAuxiliary (req, res, next) {
const ldp = req.app.locals.ldp
const contentType = req.get('content-type')
const requestUri = ldp.resourceMapper.getRequesturl(req)
if (ldp.isValidRdf(req.body, requestUri, contentType)) {
const stream = stringToStream(req.body)
return putStream(req, res, next, stream)
}
next(new HTTPError(400, 'RDF file contains invalid syntax'))
}
function isAuxiliary (req) {
const originalUrlParts = req.originalUrl.split('.')
const ext = originalUrlParts[originalUrlParts.length - 1]
return (ext === 'acl' || ext === 'meta')
}