Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 73e57dd

Browse files
committed
fix: wrap with directory option now works with addFromURL
1 parent f4acdee commit 73e57dd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/util/url-add.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ module.exports = (send) => {
3535
const validUrl = (url) => typeof url === 'string' && url.startsWith('http')
3636

3737
const requestWithRedirect = (url, opts, sendOneFile, callback) => {
38-
const req = request(parseUrl(url).protocol)(url, (res) => {
38+
const parsedUrl = parseUrl(url)
39+
40+
const req = request(parsedUrl.protocol)(url, (res) => {
3941
if (res.statusCode >= 400) {
4042
return callback(new Error(`Failed to download with ${res.statusCode}`))
4143
}
@@ -46,13 +48,18 @@ const requestWithRedirect = (url, opts, sendOneFile, callback) => {
4648
if (!validUrl(redirection)) {
4749
return callback(new Error('redirection url must be an http(s) url'))
4850
}
51+
4952
requestWithRedirect(redirection, opts, sendOneFile, callback)
5053
} else {
5154
const requestOpts = {
5255
qs: opts,
5356
converter: FileResultStreamConverter
5457
}
55-
sendOneFile(res, requestOpts, callback)
58+
59+
sendOneFile({
60+
content: res,
61+
path: parsedUrl.pathname.split('/').pop()
62+
}, requestOpts, callback)
5663
}
5764
})
5865

test/util.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ describe('.util', () => {
156156
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
157157
})
158158

159+
it('with wrap-with-directory=true', (done) => {
160+
ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg', {
161+
wrapWithDirectory: true
162+
}, (err, result) => {
163+
expect(err).to.not.exist()
164+
expect(result[0].hash).to.equal('QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg')
165+
expect(result[0].path).to.equal('969165.jpg')
166+
expect(result[1].hash).to.equal('QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61')
167+
expect(result.length).to.equal(2)
168+
})
169+
})
170+
159171
it('with invalid url', function (done) {
160172
ipfs.util.addFromURL('http://invalid', (err, result) => {
161173
expect(err.code).to.equal('ENOTFOUND')

0 commit comments

Comments
 (0)