|
| 1 | +import { readdirSync, statSync, existsSync } from "node:fs"; |
| 2 | +import { join, extname, basename } from "node:path"; |
| 3 | +import { readManifest, appendRecord, nextId } from "./manifest.mjs"; |
| 4 | +import { regenerateIndex } from "./index-gen.mjs"; |
| 5 | +import { probe } from "./probe.mjs"; |
| 6 | + |
| 7 | +const AUDIO_EXT = new Set([".mp3", ".wav", ".ogg", ".m4a", ".aac"]); |
| 8 | +const IMAGE_EXT = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg", ".ico"]); |
| 9 | +const VIDEO_EXT = new Set([".mp4", ".webm", ".mov"]); |
| 10 | + |
| 11 | +function inferType(filePath) { |
| 12 | + const ext = extname(filePath).toLowerCase(); |
| 13 | + if (AUDIO_EXT.has(ext)) { |
| 14 | + const lower = filePath.toLowerCase(); |
| 15 | + if (lower.includes("/bgm/") || lower.includes("/music/") || lower.startsWith("bgm/")) return "bgm"; |
| 16 | + if (lower.includes("/sfx/") || lower.includes("/sound") || lower.startsWith("sfx/")) return "sfx"; |
| 17 | + if (lower.includes("/voice/") || lower.includes("/narrat") || lower.startsWith("voice/")) return "voice"; |
| 18 | + return "bgm"; |
| 19 | + } |
| 20 | + if (IMAGE_EXT.has(ext)) { |
| 21 | + if (ext === ".svg" || ext === ".ico") return "icon"; |
| 22 | + return "image"; |
| 23 | + } |
| 24 | + if (VIDEO_EXT.has(ext)) return "video"; |
| 25 | + return null; |
| 26 | +} |
| 27 | + |
| 28 | +function walkDir(dir, base = "") { |
| 29 | + const files = []; |
| 30 | + if (!existsSync(dir)) return files; |
| 31 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 32 | + const rel = base ? `${base}/${entry.name}` : entry.name; |
| 33 | + if (entry.isDirectory()) { |
| 34 | + files.push(...walkDir(join(dir, entry.name), rel)); |
| 35 | + } else { |
| 36 | + files.push(rel); |
| 37 | + } |
| 38 | + } |
| 39 | + return files; |
| 40 | +} |
| 41 | + |
| 42 | +export function scanExistingAssets(projectDir) { |
| 43 | + const assetsDir = join(projectDir, "assets"); |
| 44 | + if (!existsSync(assetsDir)) return []; |
| 45 | + |
| 46 | + const files = walkDir(assetsDir); |
| 47 | + const found = []; |
| 48 | + for (const rel of files) { |
| 49 | + const type = inferType(rel); |
| 50 | + if (!type) continue; |
| 51 | + const fullPath = join(assetsDir, rel); |
| 52 | + const stat = statSync(fullPath); |
| 53 | + const meta = probe(fullPath); |
| 54 | + found.push({ |
| 55 | + relativePath: `assets/${rel}`, |
| 56 | + type, |
| 57 | + size: stat.size, |
| 58 | + name: basename(rel, extname(rel)), |
| 59 | + ...meta, |
| 60 | + }); |
| 61 | + } |
| 62 | + return found; |
| 63 | +} |
| 64 | + |
| 65 | +export function adoptExistingAssets(projectDir) { |
| 66 | + const existing = scanExistingAssets(projectDir); |
| 67 | + if (existing.length === 0) return []; |
| 68 | + |
| 69 | + const manifest = readManifest(projectDir); |
| 70 | + const knownPaths = new Set(manifest.map((r) => r.path)); |
| 71 | + |
| 72 | + const adopted = []; |
| 73 | + for (const asset of existing) { |
| 74 | + if (knownPaths.has(asset.relativePath)) continue; |
| 75 | + |
| 76 | + const id = nextId(projectDir, asset.type); |
| 77 | + const record = { |
| 78 | + id, |
| 79 | + type: asset.type, |
| 80 | + path: asset.relativePath, |
| 81 | + source: "existing", |
| 82 | + description: asset.name.replace(/[-_]/g, " "), |
| 83 | + ...(asset.duration != null && { duration: asset.duration }), |
| 84 | + ...(asset.width != null && { width: asset.width }), |
| 85 | + ...(asset.height != null && { height: asset.height }), |
| 86 | + provenance: { provider: "local", adopted: true }, |
| 87 | + }; |
| 88 | + appendRecord(projectDir, record); |
| 89 | + adopted.push(record); |
| 90 | + } |
| 91 | + |
| 92 | + if (adopted.length > 0) regenerateIndex(projectDir); |
| 93 | + return adopted; |
| 94 | +} |
| 95 | + |
| 96 | +export function findExistingAsset(projectDir, intent, type) { |
| 97 | + const assetsDir = join(projectDir, "assets"); |
| 98 | + if (!existsSync(assetsDir)) return null; |
| 99 | + const lower = intent.toLowerCase(); |
| 100 | + for (const rel of walkDir(assetsDir)) { |
| 101 | + const t = inferType(rel); |
| 102 | + if (!t || (type && t !== type)) continue; |
| 103 | + const name = basename(rel, extname(rel)).toLowerCase().replace(/[-_]/g, " "); |
| 104 | + if (name.includes(lower) || lower.includes(name)) { |
| 105 | + return { relativePath: `assets/${rel}`, type: t, name: basename(rel, extname(rel)) }; |
| 106 | + } |
| 107 | + } |
| 108 | + return null; |
| 109 | +} |
0 commit comments