Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encodeUriComponent to file name #967

Merged
merged 5 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/widgets/FileWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from "prop-types";
import { dataURItoBlob, shouldRender, setState } from "../../utils";

function addNameToDataURL(dataURL, name) {
return dataURL.replace(";base64", `;name=${name};base64`);
return dataURL.replace(";base64", `;name=${encodeURIComponent(name)};base64`);
}

function processFile(file) {
Expand Down
32 changes: 31 additions & 1 deletion test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,6 @@ describe("StringField", () => {
default: initialValue,
},
});

expect(comp.state.formData).eql(initialValue);
});

Expand Down Expand Up @@ -1552,6 +1551,37 @@ 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=" } });
},
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" }],
},
});

return new Promise(setImmediate).then(() =>
expect(comp.state.formData).eql(
"data:text/plain;name=" + uriEncodedValue + ";base64,x="
)
);
});

it("should render the widget with the expected id", () => {
const { node } = createFormComponent({
schema: {
Expand Down