|
| 1 | +import { ImageItem, Settings } from "./src/types"; |
| 2 | + |
| 3 | +// 模拟逻辑测试函数 (简化版,不包含 DOM 操作) |
| 4 | +function simulateConvertLogic(blobType: string, img: ImageItem, settings: Settings) { |
| 5 | + const targetFormat = settings.downloadLogic?.targetFormat || "original"; |
| 6 | + const gifStrategy = settings.gifStrategy || "keep"; |
| 7 | + |
| 8 | + const originalFormat = img.format.toLowerCase(); |
| 9 | + const actualMimeType = blobType.toLowerCase(); |
| 10 | + |
| 11 | + const isGif = originalFormat === "gif" || actualMimeType === "image/gif"; |
| 12 | + |
| 13 | + let shouldConvert = false; |
| 14 | + let extension = "png"; |
| 15 | + |
| 16 | + // 模拟后缀逻辑 |
| 17 | + const urlExt = img.url.split(".").pop()?.split(/[?#]/)[0]?.toLowerCase(); |
| 18 | + if (originalFormat !== "unknown" && originalFormat.length <= 5) { |
| 19 | + extension = originalFormat; |
| 20 | + } else { |
| 21 | + extension = urlExt && urlExt.length <= 5 ? urlExt : "png"; |
| 22 | + } |
| 23 | + if (extension === "jpeg") extension = "jpg"; |
| 24 | + |
| 25 | + console.log(`- 输入: URL=${img.url}, SnifferFormat=${img.format}, BlobType=${blobType}`); |
| 26 | + console.log(`- 识别结果: isGif=${isGif}, 策略=${gifStrategy}, 目标格式=${targetFormat}`); |
| 27 | + |
| 28 | + if (isGif) { |
| 29 | + if (gifStrategy === "keep") { |
| 30 | + return { action: "KEEP", extension: "gif" }; |
| 31 | + } else if (gifStrategy === "firstFrame") { |
| 32 | + shouldConvert = true; |
| 33 | + } else if (gifStrategy === "skip") { |
| 34 | + return { action: "SKIP" }; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (targetFormat !== "original") { |
| 39 | + if (targetFormat !== originalFormat || isGif) { |
| 40 | + shouldConvert = true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return { action: shouldConvert ? "CONVERT" : "DOWNLOAD", extension }; |
| 45 | +} |
| 46 | + |
| 47 | +// 测试用例 |
| 48 | +const settings: Settings = { |
| 49 | + gifStrategy: "keep", |
| 50 | + downloadLogic: { targetFormat: "webp", quality: 80 } |
| 51 | +} as any; |
| 52 | + |
| 53 | +console.log("场景 1: GIF 保持原图 (即使开启了 WebP 转换)"); |
| 54 | +console.log(simulateConvertLogic("image/gif", { url: "test.gif", format: "GIF" } as any, settings)); |
| 55 | + |
| 56 | +console.log("\n场景 2: 伪装 URL 的 GIF (Sniffer 识别错误)"); |
| 57 | +console.log(simulateConvertLogic("image/gif", { url: "test.jpg", format: "JPG" } as any, settings)); |
| 58 | + |
| 59 | +console.log("\n场景 3: 跳过 GIF"); |
| 60 | +console.log(simulateConvertLogic("image/gif", { url: "test.gif", format: "GIF" } as any, { ...settings, gifStrategy: "skip" } as any)); |
| 61 | + |
| 62 | +console.log("\n场景 4: GIF 提取首帧"); |
| 63 | +console.log(simulateConvertLogic("image/gif", { url: "test.gif", format: "GIF" } as any, { ...settings, gifStrategy: "firstFrame" } as any)); |
0 commit comments