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

Commit

Permalink
fix: ipfs add url wrap doesn't work (#750)
Browse files Browse the repository at this point in the history
* fix: wrap with directory option now works with addFromURL

* fix: add done after finishing test
  • Loading branch information
hacdias authored and daviddias committed Apr 26, 2018
1 parent f4acdee commit f6f1bf0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util/url-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ module.exports = (send) => {
const validUrl = (url) => typeof url === 'string' && url.startsWith('http')

const requestWithRedirect = (url, opts, sendOneFile, callback) => {
const req = request(parseUrl(url).protocol)(url, (res) => {
const parsedUrl = parseUrl(url)

const req = request(parsedUrl.protocol)(url, (res) => {
if (res.statusCode >= 400) {
return callback(new Error(`Failed to download with ${res.statusCode}`))
}
Expand All @@ -46,13 +48,18 @@ const requestWithRedirect = (url, opts, sendOneFile, callback) => {
if (!validUrl(redirection)) {
return callback(new Error('redirection url must be an http(s) url'))
}

requestWithRedirect(redirection, opts, sendOneFile, callback)
} else {
const requestOpts = {
qs: opts,
converter: FileResultStreamConverter
}
sendOneFile(res, requestOpts, callback)

sendOneFile({
content: res,
path: parsedUrl.pathname.split('/').pop()
}, requestOpts, callback)
}
})

Expand Down
13 changes: 13 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ describe('.util', () => {
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
})

it('with wrap-with-directory=true', (done) => {
ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg', {
wrapWithDirectory: true
}, (err, result) => {
expect(err).to.not.exist()
expect(result[0].hash).to.equal('QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg')
expect(result[0].path).to.equal('969165.jpg')
expect(result[1].hash).to.equal('QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61')
expect(result.length).to.equal(2)
done()
})
})

it('with invalid url', function (done) {
ipfs.util.addFromURL('http://invalid', (err, result) => {
expect(err.code).to.equal('ENOTFOUND')
Expand Down

0 comments on commit f6f1bf0

Please sign in to comment.