From cf35dc5345e4628efd540e1ad7e6c9b99084028d Mon Sep 17 00:00:00 2001 From: Jinwoo Oh Date: Mon, 3 Dec 2018 14:04:13 +0900 Subject: [PATCH] Handle encoded uri path on `copyFile` fix #2578 --- browser/main/lib/dataApi/copyFile.js | 2 +- tests/dataApi/copyFile-test.js | 35 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/dataApi/copyFile-test.js diff --git a/browser/main/lib/dataApi/copyFile.js b/browser/main/lib/dataApi/copyFile.js index 2dc66309..6f23aae2 100755 --- a/browser/main/lib/dataApi/copyFile.js +++ b/browser/main/lib/dataApi/copyFile.js @@ -16,7 +16,7 @@ function copyFile (srcPath, dstPath) { const dstFolder = path.dirname(dstPath) if (!fs.existsSync(dstFolder)) fs.mkdirSync(dstFolder) - const input = fs.createReadStream(srcPath) + const input = fs.createReadStream(decodeURI(srcPath)) const output = fs.createWriteStream(dstPath) output.on('error', reject) diff --git a/tests/dataApi/copyFile-test.js b/tests/dataApi/copyFile-test.js new file mode 100644 index 00000000..412d510a --- /dev/null +++ b/tests/dataApi/copyFile-test.js @@ -0,0 +1,35 @@ +const test = require('ava') +const copyFile = require('browser/main/lib/dataApi/copyFile') + +const path = require('path') +const fs = require('fs') + +const testFile = 'test.txt' +const srcFolder = path.join(__dirname, '🤔') +const srcPath = path.join(srcFolder, testFile) +const dstFolder = path.join(__dirname, '😇') +const dstPath = path.join(dstFolder, testFile) + +test.before((t) => { + if (!fs.existsSync(srcFolder)) fs.mkdirSync(srcFolder) + + fs.writeFileSync(srcPath, 'test') +}) + +test('`copyFile` should handle encoded URI on src path', (t) => { + return copyFile(encodeURI(srcPath), dstPath) + .then(() => { + t.true(true) + }) + .catch(() => { + t.true(false) + }) +}) + +test.after((t) => { + fs.unlinkSync(srcPath) + fs.unlinkSync(dstPath) + fs.rmdirSync(srcFolder) + fs.rmdirSync(dstFolder) +}) +