Skip to content

Commit

Permalink
fix(document-card): add file extension from last upload to filename
Browse files Browse the repository at this point in the history
The document title has no extension but the user's OS wants one.
  • Loading branch information
fkm-adfinis authored and czosel committed Apr 14, 2021
1 parent beb422f commit 7bee075
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
16 changes: 10 additions & 6 deletions addon/components/document-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ export default class DocumentCardComponent extends Component {

@task *download() {
try {
// There is a known issue with file-saver and urls. The filename passed as the second argument is ignored.
// https://github.com/eligrey/FileSaver.js/issues/670
yield saveAs(
this.args.document.files.filter((file) => file.type === "original")[0]
.downloadUrl,
this.args.document.title
const [file] = this.args.document.files.filter(
(file) => file.type === "original"
);
const extension = file.name.includes(".")
? `.${file.name.split(".").slice(-1)[0]}`
: "";

// There is a known issue with file-saver and urls.
// The filename passed as the second argument is ignored.
// https://github.com/eligrey/FileSaver.js/issues/670
yield saveAs(file.downloadUrl, this.args.document.title + extension);
} catch (error) {
this.notification.danger(this.intl.t("alexandria.errors.save-file"));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/components/document-card-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module("Integration | Component | document-card", function (hooks) {

this.document = {
title,
files: [{ type: "original", downloadUrl }],
files: [{ name: "foo.txt", type: "original", downloadUrl }],
};
await render(hbs`<DocumentCard @document={{this.document}}/>`);

Expand All @@ -49,7 +49,7 @@ module("Integration | Component | document-card", function (hooks) {
);
assert.equal(
stub.args[0][1],
title,
`${title}.txt`,
"saveAs was called with correct file name"
);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/components/document-details-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module("Integration | Component | document-details", function (hooks) {

this.selectedDocument = {
title,
files: [{ type: "original", downloadUrl }],
files: [{ name: "foo.txt", type: "original", downloadUrl }],
};

await render(hbs`<DocumentDetails @document={{this.selectedDocument}} />`);
Expand All @@ -75,7 +75,7 @@ module("Integration | Component | document-details", function (hooks) {
);
assert.equal(
stub.args[0][1],
title,
`${title}.txt`,
"saveAs was called with correct file name"
);
});
Expand Down

0 comments on commit 7bee075

Please sign in to comment.