Skip to content

Commit 4e6d533

Browse files
tstoppe89anehx
authored andcommitted
fix(documents): add mimeType lookup for mimeType/file-extension check
1 parent 3b12b79 commit 4e6d533

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

addon/services/alexandria-documents.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ import { ErrorHandler } from "ember-alexandria/utils/error-handler";
1111
* Check if document upload is in allowed mime types of category
1212
* If no allowedMimeTypes are set on the category, just return true
1313
*/
14-
const fileHasValidMimeType = (file, category) =>
15-
category.allowedMimeTypes?.includes(
16-
file.type || mime.getType(file.name ?? file.title),
17-
) ?? true;
14+
function fileHasValidMimeType(file, category) {
15+
if (!category.allowedMimeTypes) {
16+
return true;
17+
}
18+
19+
if (file instanceof File) {
20+
// newly uploaded files that do not have a model yet
21+
return category.allowedMimeTypes.includes(file.type);
22+
}
1823

24+
// existing file models
25+
return category.allowedMimeTypes.includes(file.mimeType);
26+
}
1927
export default class AlexandriaDocumentsService extends Service {
2028
@service store;
2129
@service("alexandria-config") config;
@@ -160,7 +168,12 @@ export default class AlexandriaDocumentsService extends Service {
160168
return true;
161169
}
162170

163-
if (!fileHasValidMimeType(document, newCategory)) {
171+
const files = (await document.files) ?? [];
172+
if (
173+
files
174+
.filter((f) => f.variant === "original")
175+
.some((file) => !fileHasValidMimeType(file, newCategory))
176+
) {
164177
return "invalid-file-type";
165178
}
166179

tests/acceptance/documents-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ module("Acceptance | documents", function (hooks) {
170170
});
171171
});
172172
await triggerEvent("[data-test-upload] [data-test-input]", "change", {
173-
files: [new File(["Ember Rules!"], "test-file.txt")],
173+
files: [
174+
new File(["Ember Rules!"], "test-file.txt", { type: "text/plain" }),
175+
],
174176
});
175177
assert.dom("[data-test-document-list-item]").exists({ count: 1 });
176178
});

tests/integration/components/category-nav/category-test.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,30 @@ module("Integration | Component | category-nav/category", function (hooks) {
3434
test("it moves dropped documents to new category", async function (assert) {
3535
const category = this.server.create("category");
3636
const oldCategory = this.server.create("category");
37-
const documents = this.server.createList("document", 2, {
38-
categoryId: oldCategory.id,
39-
title: "test.txt",
40-
});
41-
documents[1].update({ title: "test.xlsx" });
42-
37+
const documents = [
38+
// Case 1: File with ext in name & allowed mimType
39+
this.server.create("document", {
40+
categoryId: oldCategory.id,
41+
title: "TestTextFile",
42+
files: [
43+
this.server.create("file", {
44+
name: "test.txt",
45+
mimeType: "text/plain",
46+
}),
47+
],
48+
}),
49+
// Case 2: File with not allowed mimeType
50+
this.server.create("document", {
51+
categoryId: oldCategory.id,
52+
title: "TestVid",
53+
files: [
54+
this.server.create("file", {
55+
name: "TestVid.webm",
56+
mimeType: "video/webm",
57+
}),
58+
],
59+
}),
60+
];
4361
const store = this.owner.lookup("service:store");
4462

4563
this.category = await store.findRecord("category", category.id);
@@ -59,7 +77,9 @@ module("Integration | Component | category-nav/category", function (hooks) {
5977
dataTransfer: {
6078
getData: () => documents.map((d) => d.id).join(","),
6179
// sometimes browser send a file as well (e.g. when dragging a thumbnail) - this should be ignored
62-
files: [new File(["Thumbnail"], "test-file.docx")],
80+
files: [
81+
new File(["Thumbnail"], "test-file.txt", { type: "text/plain" }),
82+
],
6383
},
6484
});
6585

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ module("Unit | Service | alexandria-documents", function (hooks) {
1515
"category",
1616
this.server.create("category").id,
1717
);
18+
19+
const docxMimeType =
20+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
1821
const files = [
19-
new File(["1"], "test1.docx"),
20-
new File(["2"], "test2.docx"),
21-
new File(["3"], "test3.docx"),
22+
new File(["1"], "test1.docx", { type: docxMimeType }),
23+
new File(["2"], "test2.docx", { type: docxMimeType }),
24+
new File(["3"], "test3.docx", { type: docxMimeType }),
2225
];
2326

2427
await service.upload(category, files);

0 commit comments

Comments
 (0)