MCPcopy Index your code
hub / github.com/nodeSolidServer/node-solid-server / handler

Function handler

lib/handlers/post.mjs:11–101  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

9const debug = debugModule('solid:post')
10
11export default async function handler (req, res, next) {
12 const { extensions } = mime
13 const ldp = req.app.locals.ldp
14 const contentType = getContentType(req.headers)
15 debug('content-type is ', contentType)
16 // Handle SPARQL(-update?) query
17 if (contentType === 'application/sparql' ||
18 contentType === 'application/sparql-update') {
19 debug('switching to sparql query')
20 return patch(req, res, next)
21 }
22
23 // Handle container path
24 let containerPath = req.path
25 if (containerPath[containerPath.length - 1] !== '/') {
26 containerPath += '/'
27 }
28
29 // Check if container exists
30 let stats
31 try {
32 const ret = await ldp.exists(req.hostname, containerPath, false)
33 if (ret) stats = ret.stream
34 } catch (err) {
35 return next(HTTPError(err, 'Container not valid'))
36 }
37
38 // Check if container is a directory
39 if (stats && !stats.isDirectory()) {
40 debug('Path is not a container, 405!')
41 return next(HTTPError(405, 'Requested resource is not a container'))
42 }
43
44 // Dispatch to the right handler
45 if (req.is('multipart/form-data')) {
46 multi()
47 } else {
48 one()
49 }
50
51 function multi () {
52 debug('receving multiple files')
53
54 const busboy = new Busboy({ headers: req.headers })
55 busboy.on('file', async function (fieldname, file, filename, encoding, mimetype) {
56 debug('One file received via multipart: ' + filename)
57 const { url: putUrl } = await ldp.resourceMapper.mapFileToUrl(
58 { path: ldp.resourceMapper._rootPath + path.join(containerPath, filename), hostname: req.hostname })
59 try {
60 await ldp.put(putUrl, file, mimetype)
61 } catch (err) {
62 busboy.emit('error', err)
63 }
64 })
65 busboy.on('error', function (err) {
66 debug('Error receiving the file: ' + err.message)
67 next(HTTPError(500, 'Error receiving the file'))
68 })

Callers

nothing calls this directly

Calls 5

getContentTypeFunction · 0.90
HTTPErrorFunction · 0.85
multiFunction · 0.85
oneFunction · 0.85
existsMethod · 0.80

Tested by

no test coverage detected