Skip to content

Commit 02f7374

Browse files
committed
fix(download): always refresh document before download
1 parent ee6ef7a commit 02f7374

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

addon/helpers/download.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { DateTime } from "luxon";
2+
3+
export function isDownloadUrlExpired(url) {
4+
const expiryRegex = /expires=([0-9]+)/;
5+
const match = url.match(expiryRegex);
6+
return DateTime.fromSeconds(Number(match[1])) < DateTime.now();
7+
}

addon/models/file.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Model, { attr, belongsTo, hasMany } from "@ember-data/model";
22
import { task } from "ember-concurrency";
3-
import { saveAs } from "file-saver";
4-
import { DateTime } from "luxon";
53

4+
import { isDownloadUrlExpired } from "ember-alexandria/helpers/download";
65
import { ErrorHandler } from "ember-alexandria/helpers/error-handler";
76

87
export default class FileModel extends Model {
@@ -28,28 +27,16 @@ export default class FileModel extends Model {
2827
@hasMany("file", { inverse: "original", async: true }) renderings;
2928

3029
get isDownloadUrlExpired() {
31-
const expiryRegex = /expires=([0-9]+)/;
32-
const match = this.downloadUrl.match(expiryRegex);
33-
return DateTime.fromSeconds(Number(match[1])) < DateTime.now();
30+
return isDownloadUrlExpired(this.downloadUrl);
3431
}
3532

3633
download = task({ drop: true }, async () => {
3734
try {
3835
if (this.isDownloadUrlExpired) {
3936
await this.reload();
4037
}
41-
const extension = this.name.includes(".")
42-
? `.${this.name.split(".").slice(-1)[0]}`
43-
: "";
44-
const documentTitle = this.document.get("title");
45-
const fileName = documentTitle.endsWith(extension)
46-
? documentTitle
47-
: documentTitle + extension;
48-
49-
// keep in mind that for the filename to be considered, the request needs
50-
// to be same-origin (i.e. object storage needs to be deployed with a proxy)
51-
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a?retiredLocale=de#attributes
52-
saveAs(this.downloadUrl, fileName);
38+
39+
open(this.downloadUrl);
5340
} catch (error) {
5441
new ErrorHandler(this, error).notify("alexandria.errors.save-file");
5542
}

addon/services/alexandria-documents.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,16 @@ export default class AlexandriaDocumentsService extends Service {
151151
try {
152152
if (documents.length === 1) {
153153
const doc = documents[0];
154+
await doc.reload(); // always reload the document to get the latest file (webdav)
155+
await doc.latestFile.promise;
154156
const file = doc.latestFile.value;
155-
await file.download.perform();
156-
return;
157+
return await file.download.perform();
157158
}
158159

159160
// If we want to save a zip of files we need to do some more stuff
160161
// Compile an array of original file PKs
161162
const originalFilePKs = encodeURIComponent(
162-
documents
163-
.map((doc) => doc.files.find((file) => file.variant === "original"))
164-
.map((f) => f.id)
165-
.join(","),
163+
documents.map((doc) => doc.latestFile.value.id).join(","),
166164
);
167165

168166
let url = this.config?.zipDownloadHost || ""; // in case we need to send the zipDownload to another URL

tests/unit/services/alexandria-documents-test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { setupTest } from "dummy/tests/helpers";
22
import { setupMirage } from "ember-cli-mirage/test-support";
3-
import * as fileSaver from "file-saver";
43
import { module, test } from "qunit";
5-
import { stub } from "sinon";
64

75
module("Unit | Service | alexandria-documents", function (hooks) {
86
setupTest(hooks);
@@ -71,7 +69,6 @@ module("Unit | Service | alexandria-documents", function (hooks) {
7169
test("it downloads documents", async function (assert) {
7270
const service = this.owner.lookup("service:alexandria-documents");
7371
const store = this.owner.lookup("service:store");
74-
const fileSaverStub = stub(fileSaver, "saveAs");
7572

7673
const document = this.server.create("document");
7774
const file = this.server.create("file", {
@@ -85,7 +82,12 @@ module("Unit | Service | alexandria-documents", function (hooks) {
8582
documentModel.latestFile = { value: fileModel };
8683
await service.download.perform([documentModel]);
8784

88-
assert.strictEqual(fileSaverStub.args[0][0], file.downloadUrl);
89-
assert.strictEqual(fileSaverStub.args[0][1], document.title.en);
85+
const requests = this.server.pretender.handledRequests;
86+
87+
assert.strictEqual(requests.length, 4);
88+
assert.ok(requests[0].url.includes("documents"));
89+
assert.ok(requests[1].url.includes("files"));
90+
assert.ok(requests[2].url.includes("documents"));
91+
assert.ok(requests[3].url.includes("files"));
9092
});
9193
});

0 commit comments

Comments
 (0)