@@ -6,6 +6,7 @@ const tempy = require('tempy')
6
6
const del = require ( 'del' )
7
7
const StreamConcat = require ( 'stream-concat' )
8
8
const boom = require ( 'boom' )
9
+ const pump = require ( 'pump' )
9
10
const glob = require ( 'fast-glob' )
10
11
const multipart = require ( 'ipfs-multipart' )
11
12
const toPull = require ( 'stream-to-pull-stream' )
@@ -21,8 +22,10 @@ const filesDir = tempy.directory()
21
22
22
23
const createMultipartReply = ( readStream , boundary , ipfs , query , reply , cb ) => {
23
24
const fileAdder = pushable ( )
24
- const parser = new multipart . Parser ( { boundary : boundary } )
25
+ let parser = null
25
26
27
+ // use the other multipart factory for non chunked to get the boundary
28
+ parser = new multipart . Parser ( { boundary : boundary } )
26
29
readStream . pipe ( parser )
27
30
28
31
parser . on ( 'file' , ( fileName , fileStream ) => {
@@ -147,19 +150,38 @@ module.exports = (server) => {
147
150
config : {
148
151
payload : {
149
152
parse : false ,
150
- maxBytes : 10485760
153
+ output : 'stream' ,
154
+ maxBytes : 1000 * 1024 * 1024
155
+ // maxBytes: 10485760
151
156
} ,
152
157
handler : ( request , reply ) => {
153
158
// console.log('received')
154
159
// console.log(request.headers['content-range'])
155
160
// console.log(request.headers['x-ipfs-chunk-index'])
156
161
// console.log(request.headers['x-ipfs-chunk-group-uuid'])
157
- const boundary = request . headers [ 'x-ipfs-chunk-boundary' ]
158
162
const id = request . headers [ 'x-ipfs-chunk-group-uuid' ]
163
+ const boundary = request . headers [ 'x-ipfs-chunk-boundary' ]
164
+ const ipfs = request . server . app . ipfs
165
+
166
+ // non chunked
167
+
168
+ if ( ! id ) {
169
+ createMultipartReply (
170
+ request . payload ,
171
+ boundary ,
172
+ ipfs ,
173
+ request . query ,
174
+ reply ,
175
+ ( ) => {
176
+ console . log ( 'Finished adding' )
177
+ }
178
+ )
179
+
180
+ return
181
+ }
159
182
const index = Number ( request . headers [ 'x-ipfs-chunk-index' ] )
160
183
const file = path . join ( filesDir , id ) + '-' + index
161
184
const match = request . headers [ 'content-range' ] . match ( / ( \d + ) - ( \d + ) \/ ( \d + | \* ) / )
162
- const ipfs = request . server . app . ipfs
163
185
164
186
if ( ! match || ! match [ 1 ] || ! match [ 2 ] || ! match [ 3 ] ) {
165
187
return boom . badRequest ( 'malformed content-range header' )
@@ -199,11 +221,16 @@ module.exports = (server) => {
199
221
}
200
222
)
201
223
} else {
202
- const stream = fs . createWriteStream ( file )
203
- stream . write ( request . payload )
204
-
205
- // TODO handle errors
206
- reply ( { Bytes : request . payload . length } )
224
+ pump (
225
+ request . payload ,
226
+ fs . createWriteStream ( file ) ,
227
+ ( err ) => {
228
+ if ( err ) {
229
+ reply ( err )
230
+ }
231
+ reply ( { Bytes : total } )
232
+ }
233
+ )
207
234
}
208
235
}
209
236
}
0 commit comments