revert fb8e77bb68
Local Build and Deploy / deploy (push) Successful in 1m11s
Local Build and Deploy / deploy (push) Successful in 1m11s
revert style: clean up code with prettier and eslint
This commit is contained in:
1 parent
27de078f54
commit
e85e4d74ea
378 files changed
+20710
-22428
No files matched your search
@@ -1,123 +1,128 @@
|
||||
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";
|
||||
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;
|
||||
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;
|
||||
previewType?: string
|
||||
previewId?: number
|
||||
}
|
||||
|
||||
export interface ImportFigureResult {
|
||||
ok: boolean;
|
||||
lib: string;
|
||||
warnings: string[];
|
||||
error?: string;
|
||||
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");
|
||||
'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`);
|
||||
return path.join(/*turbopackIgnore: true*/ FIGURE_NITRO_DIR, `${lib}.nitro`)
|
||||
}
|
||||
|
||||
export async function importSingleFigure(params: {
|
||||
lib: string;
|
||||
onProgress?: (status: string) => void;
|
||||
lib: string
|
||||
onProgress?: (status: string) => void
|
||||
}): Promise<ImportFigureResult> {
|
||||
const { lib, onProgress } = params;
|
||||
const warnings: string[] = [];
|
||||
await ensureFigureDirs();
|
||||
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;
|
||||
onProgress?.('downloading')
|
||||
const swfPath = path.join(/*turbopackIgnore: true*/ FIGURE_SWF_DIR, `${lib}.swf`)
|
||||
let swfUrl: string
|
||||
try {
|
||||
swfUrl = await resolveFigureSwfUrl(lib);
|
||||
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}` };
|
||||
const dl = await downloadFile(swfUrl, swfPath, { validate: 'swf' })
|
||||
if (!dl.ok) return { ok: false, lib, warnings, error: `SWF download failed: ${swfUrl}` }
|
||||
|
||||
onProgress?.("converting");
|
||||
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);
|
||||
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 };
|
||||
return { ok: true, lib, warnings }
|
||||
}
|
||||
|
||||
export async function deleteImportedFigure(lib: string): Promise<{ deletedFile: boolean }> {
|
||||
const p = nitroPathFor(lib);
|
||||
const p = nitroPathFor(lib)
|
||||
if (existsSync(/*turbopackIgnore: true*/ p)) {
|
||||
await fs.unlink(/*turbopackIgnore: true*/ p).catch(() => {});
|
||||
return { deletedFile: true };
|
||||
await fs.unlink(/*turbopackIgnore: true*/ p).catch(() => {})
|
||||
return { deletedFile: true }
|
||||
}
|
||||
return { deletedFile: false };
|
||||
return { deletedFile: false }
|
||||
}
|
||||
|
||||
export async function getFigureList(params: {
|
||||
search: string;
|
||||
page: number;
|
||||
perPage: number;
|
||||
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 { 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 } };
|
||||
}))
|
||||
return { items, meta: { page: Math.max(page, 1), perPage, total } }
|
||||
}
|
||||
|
||||
export async function getFigureStats(): Promise<{
|
||||
total: number;
|
||||
imported: number;
|
||||
missing: number;
|
||||
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 };
|
||||
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 }
|
||||
}
|
||||
Reference in new issue
Block a user