Skip to content

Commit

Permalink
feat: preparing for zip download implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanH90 authored and czosel committed Sep 17, 2021
1 parent ebd612a commit 6f88a4e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions addon/components/document-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@ export default class DocumentCardComponent extends Component {
}

@task *download() {
if (this.args.document) {
// TODO: Currently this only works for a single document
if (this.args.document || this.args?.documents.length === 1) {
const doc = this.args.document || this.args?.documents[0];
try {
const file = this.args.document.files.find(
(file) => file.type === "original"
);
const file = doc.files.find((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);
yield saveAs(file.downloadUrl, doc.title + extension);
} catch (error) {
this.notification.danger(this.intl.t("alexandria.errors.save-file"));
}
} else {
// Compile an array of original file PKs
const originalFilePKs = encodeURIComponent(
this.args.documents
.map((doc) =>
doc.files.toArray().find((file) => file.type === "original")
)
.map((f) => f.id)
.join(",")
);

// const url = `/api/v1/files/multi?filter%5Bfiles%5D=${originalFileIds}`;
const url = `api/v1/files/multi?files=${originalFilePKs}`;
yield saveAs(url, `Download-${this.args.documents.length}-files.zip`);
}
}
}

0 comments on commit 6f88a4e

Please sign in to comment.