34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import * as path from 'path';
|
||
|
||
/** 与 DB model_cad_files / 上传接口一致的最小字段 */
|
||
export type CadFileRow = {
|
||
format: string | null;
|
||
name: string;
|
||
filePath: string;
|
||
};
|
||
|
||
/**
|
||
* 解析扩展名:优先数据库 format,否则从文件名推断(避免 format 为空或录入错误)。
|
||
*/
|
||
export function resolveCadExtension(f: CadFileRow): string {
|
||
let ext = (f.format || '').trim().toLowerCase().replace(/^\./, '');
|
||
if (!ext && f.name) {
|
||
ext = path.extname(f.name).slice(1).toLowerCase();
|
||
}
|
||
return ext;
|
||
}
|
||
|
||
/**
|
||
* 平滑热力:优先 STL,其次 OBJ(与前端可加载格式对齐)。
|
||
*/
|
||
export function pickCadFileForHeatmap(
|
||
files: CadFileRow[],
|
||
): { file: CadFileRow; ext: 'stl' | 'obj' } | null {
|
||
const withExt = files.map((file) => ({ file, ext: resolveCadExtension(file) }));
|
||
const stl = withExt.find((x) => x.ext === 'stl');
|
||
if (stl) return { file: stl.file, ext: 'stl' };
|
||
const obj = withExt.find((x) => x.ext === 'obj');
|
||
if (obj) return { file: obj.file, ext: 'obj' };
|
||
return null;
|
||
}
|