Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0d4581a

Browse files
committed
feat: handle non chunked
1 parent eee889b commit 0d4581a

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/http/api/routes/files.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const tempy = require('tempy')
66
const del = require('del')
77
const StreamConcat = require('stream-concat')
88
const boom = require('boom')
9+
const pump = require('pump')
910
const glob = require('fast-glob')
1011
const multipart = require('ipfs-multipart')
1112
const toPull = require('stream-to-pull-stream')
@@ -21,8 +22,10 @@ const filesDir = tempy.directory()
2122

2223
const createMultipartReply = (readStream, boundary, ipfs, query, reply, cb) => {
2324
const fileAdder = pushable()
24-
const parser = new multipart.Parser({ boundary: boundary })
25+
let parser = null
2526

27+
// use the other multipart factory for non chunked to get the boundary
28+
parser = new multipart.Parser({ boundary: boundary })
2629
readStream.pipe(parser)
2730

2831
parser.on('file', (fileName, fileStream) => {
@@ -147,19 +150,38 @@ module.exports = (server) => {
147150
config: {
148151
payload: {
149152
parse: false,
150-
maxBytes: 10485760
153+
output: 'stream',
154+
maxBytes: 1000 * 1024 * 1024
155+
// maxBytes: 10485760
151156
},
152157
handler: (request, reply) => {
153158
// console.log('received')
154159
// console.log(request.headers['content-range'])
155160
// console.log(request.headers['x-ipfs-chunk-index'])
156161
// console.log(request.headers['x-ipfs-chunk-group-uuid'])
157-
const boundary = request.headers['x-ipfs-chunk-boundary']
158162
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+
}
159182
const index = Number(request.headers['x-ipfs-chunk-index'])
160183
const file = path.join(filesDir, id) + '-' + index
161184
const match = request.headers['content-range'].match(/(\d+)-(\d+)\/(\d+|\*)/)
162-
const ipfs = request.server.app.ipfs
163185

164186
if (!match || !match[1] || !match[2] || !match[3]) {
165187
return boom.badRequest('malformed content-range header')
@@ -199,11 +221,16 @@ module.exports = (server) => {
199221
}
200222
)
201223
} 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+
)
207234
}
208235
}
209236
}

0 commit comments

Comments
 (0)