-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathldp-copy.js
More file actions
73 lines (70 loc) · 2.82 KB
/
ldp-copy.js
File metadata and controls
73 lines (70 loc) · 2.82 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module.exports = copy
const debug = require('./debug')
const fs = require('fs')
const mkdirp = require('fs-extra').mkdirp
const error = require('./http-error')
const path = require('path')
const http = require('http')
const https = require('https')
const getContentType = require('./utils').getContentType
/**
* Cleans up a file write stream (ends stream, deletes the file).
* @method cleanupFileStream
* @private
* @param stream {WriteStream}
*/
function cleanupFileStream (stream) {
const streamPath = stream.path
stream.destroy()
fs.unlinkSync(streamPath)
}
/**
* Performs an LDP Copy operation, imports a remote resource to a local path.
* @param resourceMapper {ResourceMapper} A resource mapper instance.
* @param copyToUri {Object} The location (in the current domain) to copy to.
* @param copyFromUri {String} Location of remote resource to copy from
* @return A promise resolving when the copy operation is finished
*/
function copy (resourceMapper, copyToUri, copyFromUri) {
return new Promise((resolve, reject) => {
const request = /^https:/.test(copyFromUri) ? https : http
request.get(copyFromUri)
.on('error', function (err) {
debug.handlers('COPY -- Error requesting source file: ' + err)
this.end()
return reject(new Error('Error writing data: ' + err))
})
.on('response', function (response) {
if (response.statusCode !== 200) {
debug.handlers('COPY -- HTTP error reading source file: ' + response.statusMessage)
this.end()
const error = new Error('Error reading source file: ' + response.statusMessage)
error.statusCode = response.statusCode
return reject(error)
}
// Grab the content type from the source
const contentType = getContentType(response.headers)
resourceMapper.mapUrlToFile({ url: copyToUri, createIfNotExists: true, contentType })
.then(({ path: copyToPath }) => {
mkdirp(path.dirname(copyToPath), function (err) {
if (err) {
debug.handlers('COPY -- Error creating destination directory: ' + err)
return reject(new Error('Failed to create the path to the destination resource: ' + err))
}
const destinationStream = fs.createWriteStream(copyToPath)
.on('error', function (err) {
cleanupFileStream(this)
return reject(new Error('Error writing data: ' + err))
})
.on('finish', function () {
// Success
debug.handlers('COPY -- Wrote data to: ' + copyToPath)
resolve()
})
response.pipe(destinationStream)
})
})
.catch(() => reject(error(500, 'Could not find target file to copy')))
})
})
}