Spaces:
Sleeping
Sleeping
File size: 11,850 Bytes
82a25a8 | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | // βββ File Type Detection & Smart Conversion Map βββββββββββββββββββββββββββ
export type FileKind =
| "pdf"
| "jpg"
| "png"
| "webp"
| "gif"
| "bmp"
| "tiff"
| "svg"
| "docx"
| "doc"
| "xlsx"
| "pptx"
| "txt"
| "unknown";
export interface ConvertOption {
to: string; // target format label e.g. "PDF", "JPG"
toolId: string; // which tool page to use
label: string; // dropdown label e.g. "PNG β PDF"
icon: string; // SVG path for the icon
description: string; // short explanation shown under label
}
export interface DetectedFile {
kind: FileKind;
ext: string;
mime: string;
label: string; // human name e.g. "PNG Image"
convertOptions: ConvertOption[];
otherActions: { toolId: string; label: string; icon: string }[];
}
// ββ Detect kind from File object ββββββββββββββββββββββββββββββββββββββββββββ
export function detectFileKind(file: File): FileKind {
const ext = file.name.split(".").pop()?.toLowerCase() ?? "";
const mime = file.type.toLowerCase();
if (mime === "application/pdf" || ext === "pdf") return "pdf";
if (mime === "image/jpeg" || ext === "jpg" || ext === "jpeg") return "jpg";
if (mime === "image/png" || ext === "png") return "png";
if (mime === "image/webp" || ext === "webp") return "webp";
if (mime === "image/gif" || ext === "gif") return "gif";
if (mime === "image/bmp" || ext === "bmp") return "bmp";
if (mime === "image/tiff" || ext === "tiff" || ext === "tif") return "tiff";
if (mime === "image/svg+xml" || ext === "svg") return "svg";
if (
mime === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
ext === "docx"
)
return "docx";
if (mime === "application/msword" || ext === "doc") return "doc";
if (
mime === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
ext === "xlsx"
)
return "xlsx";
if (
mime ===
"application/vnd.openxmlformats-officedocument.presentationml.presentation" ||
ext === "pptx"
)
return "pptx";
if (mime === "text/plain" || ext === "txt") return "txt";
return "unknown";
}
// ββ Human-readable labels βββββββββββββββββββββββββββββββββββββββββββββββββββ
const KIND_LABELS: Record<FileKind, string> = {
pdf: "PDF Document",
jpg: "JPG Image",
png: "PNG Image",
webp: "WebP Image",
gif: "GIF Image",
bmp: "BMP Image",
tiff: "TIFF Image",
svg: "SVG Image",
docx: "Word Document",
doc: "Word Document (Legacy)",
xlsx: "Excel Spreadsheet",
pptx: "PowerPoint Presentation",
txt: "Text File",
unknown: "File",
};
// ββ Icon paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const ICONS = {
image: "M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z",
pdf: ["M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z", "M14 2v6h6"],
compress: ["M4 14h6v6", "M20 10h-6V4", "M14 10l7-7", "M3 21l7-7"],
rotate: "M1 4v6h6M23 20v-6h-6M20.49 9A9 9 0 005.64 5.64L1 10M23 14l-4.64 4.36A9 9 0 013.51 15",
split: ["M16 3h5v5", "M8 3H3v5", "M12 22v-8.3a4 4 0 00-1.172-2.872L3 3", "M12 22v-8.3a4 4 0 011.172-2.872L21 3"],
merge: ["M8 3H5a2 2 0 00-2 2v3", "M21 8V5a2 2 0 00-2-2h-3", "M3 16v3a2 2 0 002 2h3", "M16 21h3a2 2 0 002-2v-3", "M12 8v8", "M8 12h8"],
lock: ["M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10", "M9 12l2 2 4-4"],
unlock: ["M8 11V7a4 4 0 018 0", "M5 11h14a2 2 0 012 2v7a2 2 0 01-2 2H5a2 2 0 01-2-2v-7a2 2 0 012-2z", "M12 16v2"],
ocr: ["M15 3h4a2 2 0 012 2v14a2 2 0 01-2 2h-4", "M10 17l5-5-5-5", "M13.8 12H3"],
watermark:["M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10"],
organize: ["M8 6h13", "M8 12h13", "M8 18h13", "M3 6h.01", "M3 12h.01", "M3 18h.01"],
ai: ["M12 2a10 10 0 110 20A10 10 0 0112 2z", "M12 8v4l3 3"],
word: ["M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z", "M14 2v6h6", "M8 13h8", "M8 17h5"],
jpg: "M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z",
};
// ββ Conversion map (source kind β possible targets) ββββββββββββββββββββββ
const CONVERT_MAP: Record<FileKind, ConvertOption[]> = {
// ββ PDF source ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pdf: [
{ to: "JPG", toolId: "pdf2jpg", label: "PDF β JPG", icon: ICONS.jpg, description: "Convert each page to a JPG image" },
{ to: "PNG", toolId: "pdf2jpg", label: "PDF β PNG", icon: ICONS.image, description: "Convert each page to a PNG image" },
{ to: "WORD", toolId: "ocr", label: "PDF β Word", icon: Array.isArray(ICONS.word) ? ICONS.word[0] : ICONS.word, description: "Extract text into an editable Word file" },
{ to: "TXT", toolId: "ocr", label: "PDF β Text", icon: Array.isArray(ICONS.ocr) ? ICONS.ocr[0] : ICONS.ocr, description: "Extract all text content from PDF" },
],
// ββ Image sources βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
jpg: [
{ to: "PDF", toolId: "jpg2pdf", label: "JPG β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Pack one or more JPGs into a PDF" },
{ to: "PNG", toolId: "jpg2pdf", label: "JPG β PNG", icon: ICONS.image, description: "Convert JPG to lossless PNG format" },
{ to: "WEBP", toolId: "jpg2pdf", label: "JPG β WebP", icon: ICONS.image, description: "Convert to modern WebP format" },
],
png: [
{ to: "PDF", toolId: "jpg2pdf", label: "PNG β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Pack one or more PNGs into a PDF" },
{ to: "JPG", toolId: "jpg2pdf", label: "PNG β JPG", icon: ICONS.jpg, description: "Convert to compressed JPG format" },
{ to: "WEBP", toolId: "jpg2pdf", label: "PNG β WebP", icon: ICONS.image, description: "Convert to modern WebP format" },
],
webp: [
{ to: "PDF", toolId: "jpg2pdf", label: "WebP β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert WebP image to PDF" },
{ to: "JPG", toolId: "jpg2pdf", label: "WebP β JPG", icon: ICONS.jpg, description: "Convert WebP to JPG format" },
{ to: "PNG", toolId: "jpg2pdf", label: "WebP β PNG", icon: ICONS.image, description: "Convert WebP to lossless PNG" },
],
gif: [
{ to: "PDF", toolId: "jpg2pdf", label: "GIF β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert GIF frame(s) to PDF" },
{ to: "JPG", toolId: "jpg2pdf", label: "GIF β JPG", icon: ICONS.jpg, description: "Convert GIF to JPG image" },
{ to: "PNG", toolId: "jpg2pdf", label: "GIF β PNG", icon: ICONS.image, description: "Convert GIF to PNG image" },
],
bmp: [
{ to: "PDF", toolId: "jpg2pdf", label: "BMP β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert BMP to PDF" },
{ to: "JPG", toolId: "jpg2pdf", label: "BMP β JPG", icon: ICONS.jpg, description: "Convert BMP to JPG" },
{ to: "PNG", toolId: "jpg2pdf", label: "BMP β PNG", icon: ICONS.image, description: "Convert BMP to PNG" },
],
tiff: [
{ to: "PDF", toolId: "jpg2pdf", label: "TIFF β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert TIFF to PDF" },
{ to: "JPG", toolId: "jpg2pdf", label: "TIFF β JPG", icon: ICONS.jpg, description: "Convert TIFF to JPG" },
{ to: "PNG", toolId: "jpg2pdf", label: "TIFF β PNG", icon: ICONS.image, description: "Convert TIFF to PNG" },
],
svg: [
{ to: "PDF", toolId: "jpg2pdf", label: "SVG β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert SVG vector to PDF" },
{ to: "PNG", toolId: "jpg2pdf", label: "SVG β PNG", icon: ICONS.image, description: "Rasterize SVG to PNG" },
],
// ββ Document sources ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
docx: [
{ to: "PDF", toolId: "jpg2pdf", label: "Word β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert Word document to PDF" },
],
doc: [
{ to: "PDF", toolId: "jpg2pdf", label: "Doc β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert legacy Word to PDF" },
],
xlsx: [
{ to: "PDF", toolId: "jpg2pdf", label: "Excel β PDF",icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert spreadsheet to PDF" },
],
pptx: [
{ to: "PDF", toolId: "jpg2pdf", label: "PPT β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert presentation to PDF" },
{ to: "JPG", toolId: "pdf2jpg", label: "PPT β JPG", icon: ICONS.jpg, description: "Convert slides to JPG images" },
],
txt: [
{ to: "PDF", toolId: "jpg2pdf", label: "TXT β PDF", icon: Array.isArray(ICONS.pdf) ? ICONS.pdf[0] : ICONS.pdf, description: "Convert plain text to PDF" },
],
unknown: [],
};
// ββ Other actions available per kind (non-convert) βββββββββββββββββββββββββ
const OTHER_ACTIONS_MAP: Partial<Record<FileKind, { toolId: string; label: string; icon: string }[]>> = {
pdf: [
{ toolId: "compress", label: "Compress", icon: "compress" },
{ toolId: "merge", label: "Merge", icon: "merge" },
{ toolId: "split", label: "Split", icon: "split" },
{ toolId: "rotate", label: "Rotate", icon: "rotate" },
{ toolId: "watermark", label: "Watermark", icon: "watermark" },
{ toolId: "protect", label: "Protect", icon: "lock" },
{ toolId: "unlock", label: "Unlock", icon: "unlock" },
{ toolId: "ocr", label: "OCR", icon: "ocr" },
{ toolId: "organize", label: "Organize", icon: "organize" },
{ toolId: "ai", label: "AI Summary",icon: "ai" },
],
jpg: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }, { toolId: "watermark", label: "Watermark", icon: "watermark" }],
png: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }, { toolId: "watermark", label: "Watermark", icon: "watermark" }],
webp: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }],
gif: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }],
bmp: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }],
tiff: [{ toolId: "jpg2pdf", label: "Make PDF", icon: "pdf" }],
};
// ββ Main export βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function analyzeFile(file: File): DetectedFile {
const kind = detectFileKind(file);
return {
kind,
ext: file.name.split(".").pop()?.toUpperCase() ?? "FILE",
mime: file.type || "application/octet-stream",
label: KIND_LABELS[kind],
convertOptions: CONVERT_MAP[kind] ?? [],
otherActions: OTHER_ACTIONS_MAP[kind] ?? [],
};
}
|