21 lines
773 B
TypeScript
21 lines
773 B
TypeScript
export const getHttpHeaderString = (value: unknown): string | undefined => {
|
|
if (typeof value === "string") return value;
|
|
if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
|
|
return value.join(", ");
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
export const getContentDispositionFilename = (header?: string | null): string | null => {
|
|
if (!header) return null;
|
|
const encoded = /filename\*\s*=\s*UTF-8''([^;]+)/i.exec(header)?.[1];
|
|
if (encoded) {
|
|
try {
|
|
return decodeURIComponent(encoded.trim().replace(/^"|"$/g, ""));
|
|
} catch {
|
|
// Fall through to the ASCII filename when the RFC 5987 value is malformed.
|
|
}
|
|
}
|
|
return /filename\s*=\s*"([^"]+)"|filename\s*=\s*([^;]+)/i.exec(header)?.slice(1).find(Boolean)?.trim() || null;
|
|
};
|