-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathcopy.js
More file actions
38 lines (36 loc) · 1.3 KB
/
copy.js
File metadata and controls
38 lines (36 loc) · 1.3 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
module.exports = handler
const debug = require('../debug')
const error = require('../http-error')
const ldpCopy = require('../ldp-copy')
const utils = require('../utils')
const url = require('url')
/**
* Handles HTTP COPY requests to import a given resource (specified in the
* `Source:` header) to a destination (specified in request path).
* For the moment, you can copy from public resources only (no auth delegation
* is implemented), and is mainly intended for use with
* "Save an external resource to Solid" type apps.
* @method handler
*/
function handler (req, res, next) {
const copyFrom = req.header('Source')
if (!copyFrom) {
return next(error(400, 'Source header required'))
}
const fromExternal = !!url.parse(copyFrom).hostname
const serverRoot = utils.getBaseUri(req)
const copyFromUrl = fromExternal ? copyFrom : serverRoot + copyFrom
const copyTo = res.locals.path || req.path
const copyToPath = utils.reqToPath(req)
ldpCopy(copyToPath, copyFromUrl, function (err) {
if (err) {
let statusCode = err.statusCode || 500
let errorMessage = err.statusMessage || err.message
debug.handlers('Error with COPY request:' + errorMessage)
return next(error(statusCode, errorMessage))
}
res.set('Location', copyTo)
res.sendStatus(201)
next()
})
}