This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
send-files-stream.js
149 lines (122 loc) · 3.6 KB
/
send-files-stream.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict'
const Duplex = require('stream').Duplex
const eachSeries = require('async/eachSeries')
const isStream = require('is-stream')
const once = require('once')
const prepareFile = require('./prepare-file')
const Multipart = require('./multipart')
const Converter = require('./converter').ConverterStream
function headers (file) {
const name = file.path
? encodeURIComponent(file.path)
: ''
const header = { 'Content-Disposition': `file; filename="${name}"` }
if (!file.content) {
header['Content-Type'] = 'application/x-directory'
} else if (file.symlink) {
header['Content-Type'] = 'application/symlink'
} else {
header['Content-Type'] = 'application/octet-stream'
}
return header
}
module.exports = (send, path) => {
return (options) => {
let request
let ended = false
let writing = false
options = options || {}
const multipart = new Multipart()
const retStream = new Duplex({ objectMode: true })
retStream._read = (n) => {}
retStream._write = (file, enc, _next) => {
const next = once(_next)
try {
const files = prepareFile(file, Object.assign({}, options, options.qs))
.map((file) => Object.assign({headers: headers(file)}, file))
writing = true
eachSeries(
files,
(file, cb) => multipart.write(file, enc, cb),
(err) => {
writing = false
if (err) {
return next(err)
}
if (ended) {
multipart.end()
}
next()
})
} catch (err) {
next(err)
}
}
retStream.once('finish', () => {
if (!ended) {
ended = true
if (!writing) {
multipart.end()
}
}
})
const qs = options.qs || {}
if (options['cid-version'] != null) {
qs['cid-version'] = options['cid-version']
} else if (options.cidVersion != null) {
qs['cid-version'] = options.cidVersion
}
if (options['raw-leaves'] != null) {
qs['raw-leaves'] = options['raw-leaves']
} else if (options.rawLeaves != null) {
qs['raw-leaves'] = options.rawLeaves
}
if (options.hash != null) {
qs.hash = options.hash
} else if (options.hashAlg != null) {
qs.hash = options.hashAlg
}
const args = {
path: path,
qs: qs,
args: options.args,
multipart: true,
multipartBoundary: multipart._boundary,
stream: true,
recursive: true,
progress: options.progress
}
multipart.on('error', (err) => {
retStream.emit('error', err)
})
request = send(args, (err, response) => {
if (err) {
return retStream.emit('error', err)
}
if (!response) {
// no response, which means everything is ok, so we end the retStream
return retStream.push(null) // early
}
if (!isStream(response)) {
retStream.push(response)
retStream.push(null)
return
}
response.on('error', (err) => retStream.emit('error', err))
response.on('data', (d) => {
if (d.Bytes && options.progress) {
options.progress(d.Bytes)
}
})
const convertedResponse = new Converter()
convertedResponse.once('end', () => retStream.push(null))
convertedResponse.on('data', (d) => retStream.push(d))
response.pipe(convertedResponse)
})
// signal the multipart that the underlying stream has drained and that
// it can continue producing data..
request.on('drain', () => multipart.emit('drain'))
multipart.pipe(request)
return retStream
}
}