|
1 | | -exports.handler = handler |
| 1 | +module.exports = handler |
2 | 2 |
|
3 | | -var debug = require('../debug').handlers |
4 | | -var header = require('../header.js') |
5 | | -var patch = require('./patch.js') |
| 3 | +var Busboy = require('busboy') |
| 4 | +var debug = require('debug')('ldnode:post') |
| 5 | +var header = require('../header') |
| 6 | +var patch = require('./patch') |
6 | 7 | var error = require('../http-error') |
7 | 8 |
|
8 | 9 | function handler (req, res, next) { |
9 | 10 | var ldp = req.app.locals.ldp |
10 | 11 | var contentType = req.get('content-type') |
11 | | - |
| 12 | + debug('content-type is ', contentType) |
12 | 13 | // Handle SPARQL(-update?) query |
13 | 14 | if (contentType === 'application/sparql' || |
14 | 15 | contentType === 'application/sparql-update') { |
15 | | - debug('POST -- Handling sparql query via PATCH') |
16 | | - return patch.handler(req, res, next) |
| 16 | + debug('switching to sparql query') |
| 17 | + return patch(req, res, next) |
17 | 18 | } |
18 | 19 |
|
| 20 | + // Handle container path |
19 | 21 | var containerPath = req.path |
20 | | - debug('POST -- On parent: ' + containerPath) |
21 | | - |
22 | | - // Not a container |
23 | 22 | if (containerPath[containerPath.length - 1] !== '/') { |
24 | 23 | containerPath += '/' |
25 | 24 | } |
26 | 25 |
|
27 | | - debug('POST -- Content Type: ' + contentType) |
28 | | - |
29 | | - var linkHeader = header.parseMetadataFromHeader(req.get('Link')) |
30 | | - |
| 26 | + // Check if container exists |
31 | 27 | ldp.exists(req.hostname, containerPath, function (err, stats) { |
32 | 28 | if (err) { |
33 | 29 | return next(error(err, 'Container not valid')) |
34 | 30 | } |
35 | 31 |
|
| 32 | + // Check if container is a directory |
36 | 33 | if (!stats.isDirectory()) { |
37 | | - debug('POST -- Path is not a container') |
38 | | - res.set('Allow', 'GET,HEAD,PUT,DELETE') |
| 34 | + debug('path is not a container, 405!') |
39 | 35 | return next(error(405, 'Requested resource is not a container')) |
40 | 36 | } |
41 | 37 |
|
42 | | - // TODO: possibly package this in ldp.post |
43 | | - ldp.getAvailablePath(req.hostname, containerPath, req.get('Slug'), function (resourcePath) { |
44 | | - debug('POST -- Will create at: ' + resourcePath) |
45 | | - var meta = '' |
46 | | - if (linkHeader.isBasicContainer) { |
47 | | - resourcePath += '/' |
48 | | - meta = ldp.suffixMeta |
49 | | - } |
| 38 | + // Dispatch to the right handler |
| 39 | + if (req.is('multipart/form-data')) { |
| 40 | + multi(req, res, next) |
| 41 | + } else { |
| 42 | + one(req, res, next) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + function multi () { |
| 47 | + debug('receving multiple files') |
| 48 | + |
| 49 | + var busboy = new Busboy({ headers: req.headers }) |
| 50 | + busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { |
| 51 | + debug('one file received via multipart: ' + filename) |
| 52 | + ldp.post( |
| 53 | + req.hostname, |
| 54 | + containerPath, |
| 55 | + filename, |
| 56 | + file, |
| 57 | + false, |
| 58 | + function (err) { |
| 59 | + if (err) { |
| 60 | + busboy.emit('error', err) |
| 61 | + } |
| 62 | + }) |
| 63 | + }) |
| 64 | + busboy.on('error', function (err) { |
| 65 | + debug('error receiving the file: ' + err.message) |
| 66 | + next(error(500, 'Error receiving the file')) |
| 67 | + }) |
| 68 | + |
| 69 | + // Handled by backpressure of streams! |
| 70 | + busboy.on('finish', function () { |
| 71 | + debug('done storing files') |
| 72 | + res.sendStatus(200) |
| 73 | + next() |
| 74 | + }) |
| 75 | + req.pipe(busboy) |
| 76 | + } |
50 | 77 |
|
51 | | - ldp.put(req.hostname, resourcePath + meta, req.text, function (err) { |
| 78 | + function one () { |
| 79 | + debug('receving one file') |
| 80 | + var linkHeader = header.parseMetadataFromHeader(req.get('Link')) |
| 81 | + ldp.post( |
| 82 | + req.hostname, |
| 83 | + containerPath, |
| 84 | + req.get('Slug'), |
| 85 | + req, |
| 86 | + linkHeader.isBasicContainer, |
| 87 | + function (err, resourcePath) { |
52 | 88 | if (err) { |
53 | 89 | return next(err) |
54 | 90 | } |
55 | | - |
| 91 | + debug('file stored on ' + resourcePath) |
56 | 92 | header.addLinks(res, linkHeader) |
57 | 93 | res.set('Location', resourcePath) |
58 | 94 | res.sendStatus(201) |
59 | | - return next() |
| 95 | + next() |
60 | 96 | }) |
61 | | - }) |
62 | | - }) |
| 97 | + } |
63 | 98 | } |
| 99 | + |
0 commit comments