Skip to content

Commit d52c54d

Browse files
committed
Improve MIME type handling for uploaded files
Refactored plain text MIME type detection to use a configurable allowlist and a new matching function. Updated modal width for better display of images and text files.
1 parent ce49d41 commit d52c54d

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/lib/components/chat/UploadedFile.svelte

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import AudioPlayer from "../players/AudioPlayer.svelte";
1010
import EosIconsLoading from "~icons/eos-icons/loading";
1111
import { base } from "$app/paths";
12+
import { TEXT_MIME_ALLOWLIST } from "$lib/constants/mime";
1213
1314
interface Props {
1415
file: MessageFile;
@@ -43,20 +44,28 @@
4344
const isVideo = (mime: string) =>
4445
mime.startsWith("video/") || mime === "mp4" || mime === "x-mpeg";
4546
47+
function matchesAllowed(contentType: string, allowed: readonly string[]): boolean {
48+
const ct = contentType.split(";")[0]?.trim().toLowerCase();
49+
if (!ct) return false;
50+
const [ctType, ctSubtype] = ct.split("/");
51+
for (const a of allowed) {
52+
const [aType, aSubtype] = a.toLowerCase().split("/");
53+
const typeOk = aType === "*" || aType === ctType;
54+
const subOk = aSubtype === "*" || aSubtype === ctSubtype;
55+
if (typeOk && subOk) return true;
56+
}
57+
return false;
58+
}
59+
4660
const isPlainText = (mime: string) =>
47-
mime === "text/plain" ||
48-
mime === "text/csv" ||
49-
mime === "text/markdown" ||
50-
mime === "application/json" ||
51-
mime === "application/xml" ||
52-
mime === "application/vnd.chatui.clipboard";
61+
mime === "application/vnd.chatui.clipboard" || matchesAllowed(mime, TEXT_MIME_ALLOWLIST);
5362
5463
let isClickable = $derived(isImage(file.mime) || isPlainText(file.mime));
5564
</script>
5665

5766
{#if showModal && isClickable}
5867
<!-- show the image file full screen, click outside to exit -->
59-
<Modal width="sm:max-w-[800px]" onclose={() => (showModal = false)}>
68+
<Modal width="xl:max-w-[75dvw]" onclose={() => (showModal = false)}>
6069
{#if isImage(file.mime)}
6170
{#if file.type === "hash"}
6271
<img
@@ -182,7 +191,7 @@
182191
{/if}
183192
</dl>
184193
</div>
185-
{:else if file.mime === "application/octet-stream"}
194+
{:else if file.mime === "application/octet-stream"}
186195
<div
187196
class="flex h-14 w-72 items-center gap-2 overflow-hidden rounded-xl border border-gray-200 bg-white p-2 dark:border-gray-800 dark:bg-gray-900"
188197
class:file-hoverable={isClickable}

0 commit comments

Comments
 (0)