78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
/** 数据资源预览:扩展名分流与展示名。 */
|
|
|
|
export type DataResourcePreviewKind = "excel" | "text" | "table";
|
|
|
|
export const EXCEL_EXTENSIONS = [".xlsx", ".xls"] as const;
|
|
export const TEXT_EXTENSIONS = [".txt", ".json"] as const;
|
|
export const TABLE_EXTENSIONS = [".csv", ".tsv"] as const;
|
|
|
|
/** Excel 整文件拉取上限。 */
|
|
export const EXCEL_PREVIEW_MAX_BYTES = 80 * 1024 * 1024;
|
|
/** 文本预览拉取上限。 */
|
|
export const TEXT_PREVIEW_MAX_BYTES = 5 * 1024 * 1024;
|
|
|
|
export type DataResourcePreviewTarget = {
|
|
resourceId: string;
|
|
resourceName: string;
|
|
fileExtension: string | null;
|
|
sizeBytes: number;
|
|
kind: DataResourcePreviewKind;
|
|
};
|
|
|
|
function lowerName(name: string | null | undefined): string {
|
|
return (name ?? "").toLowerCase();
|
|
}
|
|
|
|
function matchesExt(name: string, exts: readonly string[]): boolean {
|
|
return exts.some((ext) => name.endsWith(ext));
|
|
}
|
|
|
|
export function isExcelFileName(name: string | null | undefined): boolean {
|
|
return matchesExt(lowerName(name), EXCEL_EXTENSIONS);
|
|
}
|
|
|
|
export function isTextPreviewFileName(name: string | null | undefined): boolean {
|
|
return matchesExt(lowerName(name), TEXT_EXTENSIONS);
|
|
}
|
|
|
|
export function isTablePreviewFileName(name: string | null | undefined): boolean {
|
|
return matchesExt(lowerName(name), TABLE_EXTENSIONS);
|
|
}
|
|
|
|
export function previewKindFromFileName(
|
|
name: string | null | undefined,
|
|
): DataResourcePreviewKind | null {
|
|
const lower = lowerName(name);
|
|
if (matchesExt(lower, EXCEL_EXTENSIONS)) return "excel";
|
|
if (matchesExt(lower, TEXT_EXTENSIONS)) return "text";
|
|
if (matchesExt(lower, TABLE_EXTENSIONS)) return "table";
|
|
return null;
|
|
}
|
|
|
|
export function canPreviewDataResource(name: string | null | undefined): boolean {
|
|
return previewKindFromFileName(name) !== null;
|
|
}
|
|
|
|
export function resourceDisplayName(
|
|
resourceName: string,
|
|
fileExtension: string | null | undefined,
|
|
): string {
|
|
if (!fileExtension) return resourceName;
|
|
const ext = fileExtension.startsWith(".")
|
|
? fileExtension
|
|
: `.${fileExtension}`;
|
|
if (resourceName.toLowerCase().endsWith(ext.toLowerCase())) {
|
|
return resourceName;
|
|
}
|
|
return `${resourceName}${ext}`;
|
|
}
|
|
|
|
/** @deprecated use resourceDisplayName */
|
|
export const excelDisplayName = resourceDisplayName;
|
|
|
|
export function monacoLanguageFromFileName(name: string): string {
|
|
const lower = lowerName(name);
|
|
if (lower.endsWith(".json")) return "json";
|
|
return "plaintext";
|
|
}
|