From a3bd81176a05ad5e0205f65faa3478c86c81338d Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 15 May 2018 15:26:59 +0200 Subject: [PATCH] fix: util.addFromURL with URL-escaped file --- src/util/url-add.js | 3 ++- test/util.spec.js | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/util/url-add.js b/src/util/url-add.js index 39244c289..e9e4be184 100644 --- a/src/util/url-add.js +++ b/src/util/url-add.js @@ -55,10 +55,11 @@ const requestWithRedirect = (url, opts, sendOneFile, callback) => { qs: opts, converter: FileResultStreamConverter } + const fileName = decodeURIComponent(parsedUrl.pathname.split('/').pop()) sendOneFile({ content: res, - path: parsedUrl.pathname.split('/').pop() + path: fileName }, requestOpts, callback) } }) diff --git a/test/util.spec.js b/test/util.spec.js index f18612f7b..83e67e8de 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -161,7 +161,7 @@ describe('.util', () => { }) it('with wrap-with-directory=true', (done) => { - ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg', { + ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz', { wrapWithDirectory: true }, (err, result) => { expect(err).to.not.exist() @@ -173,6 +173,20 @@ describe('.util', () => { }) }) + it('with wrap-with-directory=true and URL-escaped file name', (done) => { + // Sample URL contains URL-escaped ( ) and local diacritics + ipfs.util.addFromURL('https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg/320px-Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg?foo=bar#buzz', { + wrapWithDirectory: true + }, (err, result) => { + expect(err).to.not.exist() + expect(result[0].hash).to.equal('QmRJ9ExxSMV4BLF9ZJUb2mLngupm6BXZEek755VHGTJo2Y') + expect(result[0].path).to.equal('320px-Domažlice,_Jiráskova_43_(9102).jpg') + expect(result[1].hash).to.equal('QmbxsHFU3sCfr8wszDHuDLA76C2xCv9HT8L3aC1pBwgaHk') + 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')