130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
import { existsSync, promises as fs } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { resolveFigureSwfUrl } from '@/lib/services/figure-source'
|
|
import { listFigureLibraries } from '@/lib/services/figuremap'
|
|
import { downloadFile } from '@/lib/services/import/core/download'
|
|
import { convertSwfToNitro } from '@/lib/services/swf-to-nitro'
|
|
|
|
export interface FigureListItem {
|
|
lib: string
|
|
revision: number
|
|
nitroExists: boolean
|
|
// First part the library provides ({type,id}) — used to render an avatar
|
|
// preview wearing it. Undefined if the library declares no parts.
|
|
previewType?: string
|
|
previewId?: number
|
|
}
|
|
|
|
export interface ImportFigureResult {
|
|
ok: boolean
|
|
lib: string
|
|
warnings: string[]
|
|
error?: string
|
|
}
|
|
|
|
export const FIGURE_NITRO_DIR = path.join(
|
|
/*turbopackIgnore: true*/ process.cwd(),
|
|
'public/nitro-assets/bundled/figure',
|
|
)
|
|
export const FIGURE_SWF_DIR = path.join(
|
|
/*turbopackIgnore: true*/ process.cwd(),
|
|
'public/swf/gordon',
|
|
)
|
|
|
|
export async function ensureFigureDirs(): Promise<void> {
|
|
await Promise.all(
|
|
[FIGURE_NITRO_DIR, FIGURE_SWF_DIR]
|
|
.filter((dir) => !existsSync(/*turbopackIgnore: true*/ dir))
|
|
.map((dir) => fs.mkdir(/*turbopackIgnore: true*/ dir, { recursive: true })),
|
|
)
|
|
}
|
|
|
|
function nitroPathFor(lib: string): string {
|
|
return path.join(/*turbopackIgnore: true*/ FIGURE_NITRO_DIR, `${lib}.nitro`)
|
|
}
|
|
|
|
export async function importSingleFigure(params: {
|
|
lib: string
|
|
onProgress?: (status: string) => void
|
|
}): Promise<ImportFigureResult> {
|
|
const { lib, onProgress } = params
|
|
const warnings: string[] = []
|
|
await ensureFigureDirs()
|
|
|
|
onProgress?.('downloading')
|
|
const swfPath = path.join(/*turbopackIgnore: true*/ FIGURE_SWF_DIR, `${lib}.swf`)
|
|
let swfUrl: string
|
|
try {
|
|
swfUrl = await resolveFigureSwfUrl(lib)
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
lib,
|
|
warnings,
|
|
error: `Could not resolve SWF URL: ${(err as Error).message}`,
|
|
}
|
|
}
|
|
const dl = await downloadFile(swfUrl, swfPath, { validate: 'swf' })
|
|
if (!dl.ok) return { ok: false, lib, warnings, error: `SWF download failed: ${swfUrl}` }
|
|
|
|
onProgress?.('converting')
|
|
try {
|
|
const swfBuffer = await fs.readFile(/*turbopackIgnore: true*/ swfPath)
|
|
const result = convertSwfToNitro(swfBuffer, lib)
|
|
await fs.writeFile(/*turbopackIgnore: true*/ nitroPathFor(lib), result.bundle)
|
|
if (result.warnings?.length) warnings.push(...result.warnings)
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
lib,
|
|
warnings,
|
|
error: `SWF→Nitro conversion failed: ${(err as Error).message}`,
|
|
}
|
|
}
|
|
|
|
return { ok: true, lib, warnings }
|
|
}
|
|
|
|
export async function deleteImportedFigure(lib: string): Promise<{ deletedFile: boolean }> {
|
|
const p = nitroPathFor(lib)
|
|
if (existsSync(/*turbopackIgnore: true*/ p)) {
|
|
await fs.unlink(/*turbopackIgnore: true*/ p).catch(() => {})
|
|
return { deletedFile: true }
|
|
}
|
|
return { deletedFile: false }
|
|
}
|
|
|
|
export async function getFigureList(params: {
|
|
search: string
|
|
page: number
|
|
perPage: number
|
|
}): Promise<{ items: FigureListItem[]; meta: { page: number; perPage: number; total: number } }> {
|
|
const { search, page, perPage } = params
|
|
const libs = await listFigureLibraries()
|
|
const term = search.trim().toLowerCase()
|
|
const filtered = term ? libs.filter((l) => l.id.toLowerCase().includes(term)) : libs
|
|
const total = filtered.length
|
|
const start = (Math.max(page, 1) - 1) * perPage
|
|
const items = filtered.slice(start, start + perPage).map((l) => ({
|
|
lib: l.id,
|
|
revision: l.revision,
|
|
nitroExists: existsSync(/*turbopackIgnore: true*/ nitroPathFor(l.id)),
|
|
previewType: l.parts?.[0]?.type,
|
|
previewId: l.parts?.[0]?.id,
|
|
}))
|
|
return { items, meta: { page: Math.max(page, 1), perPage, total } }
|
|
}
|
|
|
|
export async function getFigureStats(): Promise<{
|
|
total: number
|
|
imported: number
|
|
missing: number
|
|
}> {
|
|
const libs = await listFigureLibraries()
|
|
const imported = libs.filter((l) =>
|
|
existsSync(/*turbopackIgnore: true*/ nitroPathFor(l.id)),
|
|
).length
|
|
return { total: libs.length, imported, missing: libs.length - imported }
|
|
}
|
|
|