@@ -3,9 +3,11 @@ module.exports = copy
33const debug = require ( './debug' )
44const fs = require ( 'fs' )
55const mkdirp = require ( 'fs-extra' ) . mkdirp
6+ const error = require ( './http-error' )
67const path = require ( 'path' )
78const http = require ( 'http' )
89const https = require ( 'https' )
10+ const getContentType = require ( './utils' ) . getContentType
911
1012/**
1113 * Cleans up a file write stream (ends stream, deletes the file).
@@ -21,47 +23,51 @@ function cleanupFileStream (stream) {
2123
2224/**
2325 * Performs an LDP Copy operation, imports a remote resource to a local path.
24- * @param copyToPath {String} Local path to copy the resource into
26+ * @param resourceMapper {ResourceMapper} A resource mapper instance.
27+ * @param copyToUri {Object} The location (in the current domain) to copy to.
2528 * @param copyFromUri {String} Location of remote resource to copy from
26- * @param callback {Function} Node error callback
29+ * @return A promise resolving when the copy operation is finished
2730 */
28- function copy ( copyToPath , copyFromUri , callback ) {
29- mkdirp ( path . dirname ( copyToPath ) , function ( err ) {
30- if ( err ) {
31- debug . handlers ( 'COPY -- Error creating destination directory: ' + err )
32- return callback (
33- new Error ( 'Failed to create the path to the destination resource: ' +
34- err ) )
35- }
36- const destinationStream = fs . createWriteStream ( copyToPath )
37- . on ( 'error' , function ( err ) {
38- cleanupFileStream ( this )
39- return callback ( new Error ( 'Error writing data: ' + err ) )
40- } )
41- . on ( 'finish' , function ( ) {
42- // Success
43- debug . handlers ( 'COPY -- Wrote data to: ' + copyToPath )
44- callback ( )
45- } )
31+ function copy ( resourceMapper , copyToUri , copyFromUri ) {
32+ return new Promise ( ( resolve , reject ) => {
4633 const request = / ^ h t t p s : / . test ( copyFromUri ) ? https : http
4734 request . get ( copyFromUri )
4835 . on ( 'error' , function ( err ) {
4936 debug . handlers ( 'COPY -- Error requesting source file: ' + err )
5037 this . end ( )
51- cleanupFileStream ( destinationStream )
52- return callback ( new Error ( 'Error writing data: ' + err ) )
38+ return reject ( new Error ( 'Error writing data: ' + err ) )
5339 } )
5440 . on ( 'response' , function ( response ) {
5541 if ( response . statusCode !== 200 ) {
56- debug . handlers ( 'COPY -- HTTP error reading source file: ' +
57- response . statusMessage )
42+ debug . handlers ( 'COPY -- HTTP error reading source file: ' + response . statusMessage )
5843 this . end ( )
59- cleanupFileStream ( destinationStream )
6044 let error = new Error ( 'Error reading source file: ' + response . statusMessage )
6145 error . statusCode = response . statusCode
62- return callback ( error )
46+ return reject ( error )
6347 }
64- response . pipe ( destinationStream )
48+ // Grab the content type from the source
49+ const contentType = getContentType ( response . headers )
50+ resourceMapper . mapUrlToFile ( { url : copyToUri , createIfNotExists : true , contentType } )
51+ . then ( ( { path : copyToPath } ) => {
52+ mkdirp ( path . dirname ( copyToPath ) , function ( err ) {
53+ if ( err ) {
54+ debug . handlers ( 'COPY -- Error creating destination directory: ' + err )
55+ return reject ( new Error ( 'Failed to create the path to the destination resource: ' + err ) )
56+ }
57+ const destinationStream = fs . createWriteStream ( copyToPath )
58+ . on ( 'error' , function ( err ) {
59+ cleanupFileStream ( this )
60+ return reject ( new Error ( 'Error writing data: ' + err ) )
61+ } )
62+ . on ( 'finish' , function ( ) {
63+ // Success
64+ debug . handlers ( 'COPY -- Wrote data to: ' + copyToPath )
65+ resolve ( )
66+ } )
67+ response . pipe ( destinationStream )
68+ } )
69+ } )
70+ . catch ( ( ) => reject ( error ( 500 , 'Could not find target file to copy' ) ) )
6571 } )
6672 } )
6773}
0 commit comments