| 380 | * 根据文件路径获取对应的 emoji 和文件名 |
| 381 | */ |
| 382 | export function getEmojiAndNameByPath(path: string): { emoji: string; name: string } { |
| 383 | let p = path.trim(); |
| 384 | // 处理可能带有的双引号或单引号 |
| 385 | if ((p.startsWith('"') && p.endsWith('"')) || (p.startsWith("'") && p.endsWith("'"))) { |
| 386 | if (p.length >= 2) { |
| 387 | p = p.slice(1, -1).trim(); |
| 388 | } |
| 389 | } |
| 390 | const normalized = p.replace(/\\/g, "/"); |
| 391 | const withoutTrailingSlash = normalized.endsWith("/") ? normalized.slice(0, -1) : normalized; |
| 392 | |
| 393 | // 对于根目录情况(如 "D:/" 或 "/") |
| 394 | const name = withoutTrailingSlash.split("/").pop() || withoutTrailingSlash; |
| 395 | |
| 396 | // 如果原路径以斜杠结尾,或者是根目录,直接认为是文件夹 |
| 397 | if (p.endsWith("/") || p.endsWith("\\") || name === p || /^[a-zA-Z]:$/.test(name)) { |
| 398 | return { emoji: "📁", name }; |
| 399 | } |
| 400 | |
| 401 | const parts = name.split("."); |
| 402 | // 判断是否有后缀名 (不以.开头且有多个部分,或者以.开头且部分大于2) |
| 403 | const hasExtension = parts.length > 1 && (!name.startsWith(".") || parts.length > 2); |
| 404 | |
| 405 | if (hasExtension) { |
| 406 | const ext = parts[parts.length - 1].toLowerCase(); |
| 407 | let emoji = "📃"; // 默认文件 |
| 408 | switch (ext) { |
| 409 | // 办公 |
| 410 | case "doc": |
| 411 | case "docx": |
| 412 | case "pages": |
| 413 | emoji = "📄"; |
| 414 | break; |
| 415 | case "xls": |
| 416 | case "xlsx": |
| 417 | case "csv": |
| 418 | case "numbers": |
| 419 | emoji = "📊"; |
| 420 | break; |
| 421 | case "ppt": |
| 422 | case "pptx": |
| 423 | case "key": |
| 424 | emoji = "📈"; |
| 425 | break; |
| 426 | case "pdf": |
| 427 | emoji = "📕"; |
| 428 | break; |
| 429 | case "txt": |
| 430 | case "md": |
| 431 | case "rtf": |
| 432 | emoji = "📝"; |
| 433 | break; |
| 434 | // 图片 |
| 435 | case "jpg": |
| 436 | case "jpeg": |
| 437 | case "png": |
| 438 | case "gif": |
| 439 | case "webp": |