File size: 1,699 Bytes
e81f4e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import { sanitizeUploadFilename } from "./mime";
import type { AttachmentDrop, AttachmentKind } from "./types";
export function droppedAttachmentNote(
drops: readonly AttachmentDrop[] | null | undefined,
): string {
if (!drops?.length) return "";
const groups = new Map<
string,
{ kind: AttachmentKind; message: string; count: number }
>();
for (const drop of drops) {
const key = `${drop.kind}\x00${drop.message}`;
const existing = groups.get(key);
if (existing) existing.count += 1;
else groups.set(key, { kind: drop.kind, message: drop.message, count: 1 });
}
return [...groups.values()]
.map(
(group) =>
`\n\n[Note: ${group.count} ${group.kind}(s) were provided but ignored - ${group.message}.]`,
)
.join("");
}
export function attachmentDrop(
kind: AttachmentKind,
code: AttachmentDrop["code"],
message?: string,
filename?: unknown,
): AttachmentDrop {
const drop: AttachmentDrop = {
kind,
code,
message: message || defaultDropMessage(code),
};
const safeName = sanitizeUploadFilename(filename);
if (safeName) drop.filename = safeName;
return drop;
}
function defaultDropMessage(code: AttachmentDrop["code"]): string {
switch (code) {
case "invalid_image_input":
return "invalid image input";
case "invalid_file_input":
return "invalid file input";
case "invalid_base64":
return "invalid base64 payload";
case "invalid_remote_url":
return "invalid remote URL";
case "file_too_large":
return "file attachment is too large";
case "image_too_large":
return "image attachment is too large";
case "too_many_files":
return "too many attachments";
case "upload_failed":
return "attachment upload failed";
}
}
|