From 592c25ae4d468c3e396126135a2ff6d8eae50aba Mon Sep 17 00:00:00 2001 From: jorgerobles Date: Sat, 30 Jun 2018 13:24:15 +0200 Subject: [PATCH 1/5] Add encodeUriComponent to file name Fix for non standard file names (with spaces, apostrophes, etc) that prevent to be used. --- src/components/widgets/FileWidget.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/widgets/FileWidget.js b/src/components/widgets/FileWidget.js index 916bbea386..52e54ba520 100644 --- a/src/components/widgets/FileWidget.js +++ b/src/components/widgets/FileWidget.js @@ -4,6 +4,7 @@ import PropTypes from "prop-types"; import { dataURItoBlob, shouldRender, setState } from "../../utils"; function addNameToDataURL(dataURL, name) { + name=encodeURIComponent(name) return dataURL.replace(";base64", `;name=${name};base64`); } From 57cacddd2502f39ddd18ebf141af14c28d1eed17 Mon Sep 17 00:00:00 2001 From: jorgerobles Date: Sat, 19 Jan 2019 21:00:28 +0100 Subject: [PATCH 2/5] Update FileWidget.js --- src/components/widgets/FileWidget.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/widgets/FileWidget.js b/src/components/widgets/FileWidget.js index 52e54ba520..3cd86df40c 100644 --- a/src/components/widgets/FileWidget.js +++ b/src/components/widgets/FileWidget.js @@ -4,8 +4,7 @@ import PropTypes from "prop-types"; import { dataURItoBlob, shouldRender, setState } from "../../utils"; function addNameToDataURL(dataURL, name) { - name=encodeURIComponent(name) - return dataURL.replace(";base64", `;name=${name};base64`); + return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`); } function processFile(file) { From c02f0cda94515398c6b9985e86ee207c1c80e3fe Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Sun, 20 Jan 2019 11:13:12 +0100 Subject: [PATCH 3/5] Added Test --- src/components/widgets/FileWidget.js | 2 +- test/StringField_test.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/components/widgets/FileWidget.js b/src/components/widgets/FileWidget.js index 3cd86df40c..87b477f81e 100644 --- a/src/components/widgets/FileWidget.js +++ b/src/components/widgets/FileWidget.js @@ -4,7 +4,7 @@ import PropTypes from "prop-types"; import { dataURItoBlob, shouldRender, setState } from "../../utils"; function addNameToDataURL(dataURL, name) { - return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`); + return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`); } function processFile(file) { diff --git a/test/StringField_test.js b/test/StringField_test.js index 8bb9d2b079..63f63c7c10 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -1501,6 +1501,9 @@ describe("StringField", () => { describe("FileWidget", () => { const initialValue = "data:text/plain;name=file1.txt;base64,dGVzdDE="; + const nonUriEncodedValue = "fileáéí óú1.txt"; + const uriEncodedValue = "file%C3%A1%C3%A9%C3%AD%20%C3%B3%C3%BA1.txt"; + it("should render a color field", () => { const { node } = createFormComponent({ schema: { @@ -1520,7 +1523,6 @@ describe("StringField", () => { default: initialValue, }, }); - expect(comp.state.formData).eql(initialValue); }); @@ -1541,13 +1543,13 @@ describe("StringField", () => { Simulate.change(node.querySelector("[type=file]"), { target: { - files: [{ name: "file1.txt", size: 1, type: "type" }], + files: [{ name: nonUriEncodedValue, size: 1, type: "type" }], }, }); return new Promise(setImmediate).then(() => expect(comp.state.formData).eql( - "data:text/plain;name=file1.txt;base64,x=" + "data:text/plain;name=" + uriEncodedValue + ";base64,x=" ) ); }); From 820552da0b2fcac2f99034e6365e4cdb0c9e7b8d Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Sun, 20 Jan 2019 17:23:01 +0100 Subject: [PATCH 4/5] Added Test --- test/StringField_test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/StringField_test.js b/test/StringField_test.js index 63f63c7c10..3370b8cf17 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -1541,6 +1541,34 @@ describe("StringField", () => { }, }); + Simulate.change(node.querySelector("[type=file]"), { + target: { + files: [{ name: "file1.txt", size: 1, type: "type" }], + }, + }); + + return new Promise(setImmediate).then(() => + expect(comp.state.formData).eql( + "data:text/plain;name=file1.txt;base64,x=" + ) + ); + }); + + it("should encode file name with encodeURIComponent", () => { + sandbox.stub(window, "FileReader").returns({ + set onload(fn) { + fn({ target: { result: "data:text/plain;base64,x=" } }); + }, + readAsDataUrl() {}, + }); + + const { comp, node } = createFormComponent({ + schema: { + type: "string", + format: "data-url", + }, + }); + Simulate.change(node.querySelector("[type=file]"), { target: { files: [{ name: nonUriEncodedValue, size: 1, type: "type" }], From ef2969c2e3b5239ab0332d022f98cc4848c09f22 Mon Sep 17 00:00:00 2001 From: Jorge Robles Date: Sun, 20 Jan 2019 17:37:43 +0100 Subject: [PATCH 5/5] Tidy up --- test/StringField_test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/StringField_test.js b/test/StringField_test.js index 3370b8cf17..f13a483746 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -1501,9 +1501,6 @@ describe("StringField", () => { describe("FileWidget", () => { const initialValue = "data:text/plain;name=file1.txt;base64,dGVzdDE="; - const nonUriEncodedValue = "fileáéí óú1.txt"; - const uriEncodedValue = "file%C3%A1%C3%A9%C3%AD%20%C3%B3%C3%BA1.txt"; - it("should render a color field", () => { const { node } = createFormComponent({ schema: { @@ -1555,6 +1552,9 @@ describe("StringField", () => { }); it("should encode file name with encodeURIComponent", () => { + const nonUriEncodedValue = "fileáéí óú1.txt"; + const uriEncodedValue = "file%C3%A1%C3%A9%C3%AD%20%C3%B3%C3%BA1.txt"; + sandbox.stub(window, "FileReader").returns({ set onload(fn) { fn({ target: { result: "data:text/plain;base64,x=" } });