diff --git a/README.md b/README.md index b6140bd..739cfaf 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ DevTools 中 `preflight` 是浏览器发送的 OPTIONS 跨域预检,`fetch` - 文件:目录浏览、预览(文本/图片)、在线编辑提交、新建文件/目录、删除文件、多文件/文件夹上传(不设独立单文件上限,单次合计 ≤ 500 MiB、最多 500 个文件,含选择、限额与路径预检、进度反馈)、鉴权下载 - 提交历史,并可只读浏览任一历史版本 - URDF 目录上传绑定(签名 → 并发 OSS 直传 → 确认)与移除;项目详情和迁移页均不设独立单文件上限、合计 ≤ 500 MiB、最多 500 个文件;按扩展名保留 URDF、模型、材质、贴图及模型数据,过滤代码、文档、配置、隐藏文件和依赖目录,数量/大小限制仅计算待上传资源 + - 项目上传和迁移上传均在签名前按文件清单把 URDF 的 mesh/texture 引用改为相对主文件的真实路径,兼容嵌套 ROS 包、旧来源目录、Windows 路径和唯一的大小写匹配;缺失或歧义资源会阻止签名。签名、OSS 上传和确认使用改写后的同一份文件及字节数,其他资源的目录与内容保持原样。 - STEP 文件管理:URDF 右侧 STEP tab,支持 .step/.stp 单文件上传(≤ 500 MiB)、替换、移除绑定和直接下载。已按后端文档 §8.4 对接签名、OSS 直传和确认;确认异常保留原请求,手动重试前提示重新写入绑定。下载直接使用 `stepMainUrl`,不新增接口、不携带管理 Token。详见 [STEP 接口对接说明](docs/step-backend-requirements.md)。 - GitHub 迁移:预检 → 补充信息(可选上传封面、URDF)→ 创建任务并绑定资源 → 3 秒串行轮询阶段 → 检查后手动发布;重复来源跳转已有项目 - 迁移重试:`FAILED`、`REVIEW` 始终提供手动重试入口,不按 `canRetry` 隐藏;列表点击重试即进入任务确认框,展示 `retryMessage`,由重试接口判断是否受理;支持 `CLEANING` 清理阶段,409 后刷新状态,网络异常时先查询确认结果 diff --git a/dist.zip b/dist.zip new file mode 100644 index 0000000..2afb855 Binary files /dev/null and b/dist.zip differ diff --git a/src/api/index.ts b/src/api/index.ts index 1c91488..77a58b8 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,6 @@ import { downloadAdmin, queryString, requestAdmin, requestAdminUpload, setToken, uploadOssFile } from './request'; import { compressCoverImage } from '@/utils/compressImage'; +import { normalizeUrdfSelection } from '@/utils/urdfPaths'; import type { ExperiencePublicationInput } from './experience-types'; import type { AdminView, @@ -210,6 +211,7 @@ async function uploadUrdfBatch(signature: UrdfUploadSignature, selected: Selecte /** 项目 URDF:签名 → OSS 直传 → PUT 确认绑定 */ export async function uploadUrdf(code: string, selected: SelectedFile[], onProgress?: Progress): Promise { + selected = await normalizeUrdfSelection(selected); const files = toUrdfFiles(selected); const signature = await requestAdmin(`/projects/${enc(code)}/urdf/signature`, { method: 'POST', @@ -228,6 +230,7 @@ export async function uploadImportUrdf( selected: SelectedFile[], onProgress?: Progress, ): Promise { + selected = await normalizeUrdfSelection(selected); const files = toUrdfFiles(selected); const signature = await requestAdmin('/project-imports/urdf/signature', { method: 'POST', diff --git a/src/components/project/urdfFiles.ts b/src/components/project/urdfFiles.ts index be6cdab..f248f6a 100644 --- a/src/components/project/urdfFiles.ts +++ b/src/components/project/urdfFiles.ts @@ -1,5 +1,5 @@ // URDF 预览可能依赖模型、材质、外部二进制数据和贴图;按扩展名保留资源, -// 不改写相对路径,避免破坏 URDF → mesh → 材质/贴图之间的引用。 +// 保留资源的目录结构;签名前按清单修正 URDF 引用,不修改 mesh 内部的材质/贴图引用。 const RENDER_EXTENSIONS = new Set([ 'urdf', 'stl', 'dae', 'obj', 'gltf', 'glb', 'fbx', 'ply', '3ds', '3mf', diff --git a/src/utils/urdfPaths.ts b/src/utils/urdfPaths.ts new file mode 100644 index 0000000..44e335c --- /dev/null +++ b/src/utils/urdfPaths.ts @@ -0,0 +1,95 @@ +import type { SelectedFile } from '@/api/types'; + +const dirname = (path: string) => path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''; + +function normalize(path: string, clamp = false): string | null { + const out: string[] = []; + for (const part of path.split('/')) { + if (!part || part === '.') continue; + if (part === '..') { + if (!out.length) { if (clamp) continue; return null; } + out.pop(); + } else out.push(part); + } + return out.join('/'); +} + +function relative(fromDir: string, to: string): string { + const a = fromDir ? fromDir.split('/') : []; + const b = to.split('/'); + while (a.length && b.length > 1 && a[0] === b[0]) { a.shift(); b.shift(); } + return [...a.map(() => '..'), ...b].join('/') || '.'; +} + +function localPath(filename: string, mainPath: string): string | null { + const value = filename.trim().replace(/\\/g, '/'); + const pkg = value.match(/^package:\/\/[^/]*\/(.*)$/i); + if (pkg) return normalize(pkg[1], true); + if (/^https?:\/\//i.test(value) || /^(data|blob):/i.test(value)) return null; + if (/^file:\/\//i.test(value)) return normalize(value.replace(/^file:\/+/i, ''), true); + if (/^[a-z]:\//i.test(value)) return normalize(value.slice(3), true); + return normalize(value.startsWith('/') ? value.slice(1) : `${dirname(mainPath)}/${value}`, true); +} + +/** 按上传清单解析引用;优先匹配 ROS 包目录,仅接受唯一的后缀或大小写匹配。 */ +export function resolveUrdfResourcePath(filename: string, mainPath: string, availablePaths: string[]): string | null { + const target = localPath(filename, mainPath); + if (!target) return null; + const available = [...new Set(availablePaths.map((path) => normalize(path)).filter((path): path is string => !!path))]; + const pkg = filename.trim().replace(/\\/g, '/').match(/^package:\/\/([^/]+)\/(.*)$/i); + const qualified = pkg && pkg[1] !== '..' ? normalize(`${pkg[1]}/${pkg[2]}`) : null; + const packageMatches = qualified ? available.filter((path) => path === qualified || path.endsWith(`/${qualified}`)) : []; + let match = packageMatches.length === 1 ? packageMatches[0] : available.includes(target) ? target : null; + if (!match) { + const targetParts = target.split('/'); + let bestScore = 1; + let best: string[] = []; + for (const path of available) { + const parts = path.split('/'); + let score = 0; + while (score < targetParts.length && score < parts.length + && targetParts[targetParts.length - score - 1] === parts[parts.length - score - 1]) score++; + if (score > bestScore) { bestScore = score; best = [path]; } + else if (score === bestScore && score > 1) best.push(path); + } + if (bestScore > 1 && best.length === 1) match = best[0]; + else if (bestScore <= 1) { + const basename = targetParts.at(-1); + const sameName = available.filter((path) => path.split('/').at(-1) === basename); + if (sameName.length === 1) match = sameName[0]; + } + } + if (!match) { + const lowerTarget = target.toLowerCase(); + const caseMatches = available.filter((path) => path.toLowerCase() === lowerTarget + || path.toLowerCase().endsWith(`/${lowerTarget}`)); + if (caseMatches.length === 1) match = caseMatches[0]; + } + return match ? relative(dirname(mainPath), match) : null; +} + +/** 在签名前改写 URDF 的 mesh/texture 引用;其他资源及上传目录结构保持原样。 */ +export async function normalizeUrdfSelection(selected: SelectedFile[]): Promise { + const mains = selected.filter((entry) => /\.urdf$/i.test(entry.relativePath)); + if (mains.length !== 1) throw new Error('目录中必须有且仅有一个 .urdf 文件'); + const main = mains[0]; + const doc = new DOMParser().parseFromString(await main.file.text(), 'application/xml'); + if (doc.querySelector('parsererror') || !doc.querySelector('robot')) throw new Error('URDF 文件内容无效,请检查上传文件'); + const paths = selected.map((entry) => entry.relativePath); + const missing = new Set(); + let changed = false; + doc.querySelectorAll('mesh[filename], texture[filename]').forEach((node) => { + const filename = node.getAttribute('filename')!; + if (!filename.trim()) return; + if (/^(https?:\/\/|data:|blob:)/i.test(filename.trim())) return; + const resolved = resolveUrdfResourcePath(filename, main.relativePath, paths); + if (!resolved) { missing.add(filename); return; } + if (resolved !== filename) { node.setAttribute('filename', resolved); changed = true; } + }); + if (missing.size) throw new Error(`URDF 引用的资源缺失或路径不唯一:${[...missing].join('、')}`); + if (!changed) return selected; + const file = new File([new XMLSerializer().serializeToString(doc)], main.file.name, { + type: main.file.type || 'application/xml', lastModified: main.file.lastModified, + }); + return selected.map((entry) => entry === main ? { ...entry, file } : entry); +} diff --git a/tests/urdf.spec.ts b/tests/urdf.spec.ts index 64fc882..6c160e0 100644 --- a/tests/urdf.spec.ts +++ b/tests/urdf.spec.ts @@ -14,6 +14,12 @@ const unrelated = [ 'README.md', 'src/main.py', 'package.xml', 'config/settings.yaml', 'archive.zip', '.git/backup.urdf', '.DS_Store', '__MACOSX/._base.stl', 'node_modules/example/mesh.obj', ]; +const fixtureContent = (path: string) => /\.urdf$/i.test(path) ? '' : 'test'; +const fixtureDirectories = new Set(); +test.afterEach(async () => { + await Promise.all([...fixtureDirectories].map((directory) => rm(directory, { recursive: true, force: true }))); + fixtureDirectories.clear(); +}); test('URDF 不设独立单文件上限,合计允许 500 MiB 并拒绝多一个字节', async ({ page }) => { await prepare(page, 'project'); @@ -59,11 +65,14 @@ async function prepare(page: Page, entry: 'project' | 'import') { await page.addInitScript(() => localStorage.setItem('vibemake_admin_token', 'test-token')); await page.addInitScript(() => { const originalFetch = window.fetch.bind(window); - window.fetch = (input, init) => { + window.fetch = async (input, init) => { const url = typeof input === 'string' ? input : input instanceof Request ? input.url : input.href; if (new URL(url, window.location.href).pathname === '/mock-oss' && init?.body instanceof FormData) { + const fields = Object.fromEntries([...init.body.entries()].map(([key, value]) => [key, value instanceof File ? value.name : value])); + const file = init.body.get('file'); + const fileContent = file instanceof File ? await file.text() : ''; const uploads = JSON.parse(localStorage.getItem('urdf_test_uploads') ?? '[]'); - uploads.push(Object.fromEntries([...init.body.entries()].map(([key, value]) => [key, value instanceof File ? value.name : value]))); + uploads.push({ ...fields, fileContent }); localStorage.setItem('urdf_test_uploads', JSON.stringify(uploads)); } return originalFetch(input, init); @@ -119,20 +128,17 @@ async function prepare(page: Page, entry: 'project' | 'import') { async function chooseDirectory(page: Page, paths: string[], label = '选择 URDF 目录') { const parent = await mkdtemp(join(tmpdir(), 'urdf-filter-')); + fixtureDirectories.add(parent); const directory = join(parent, 'robot'); - try { - await Promise.all(paths.map(async (path) => { - const fullPath = join(directory, path); - await mkdir(dirname(fullPath), { recursive: true }); - await writeFile(fullPath, 'test'); - })); - const chooser = page.waitForEvent('filechooser'); - await page.getByRole('button', { name: new RegExp(label) }).click(); - await (await chooser).setFiles(directory); - await expect(page.getByRole('button', { name: '重新选择目录' })).toBeVisible(); - } finally { - await rm(parent, { recursive: true, force: true }); - } + await Promise.all(paths.map(async (path) => { + const fullPath = join(directory, path); + await mkdir(dirname(fullPath), { recursive: true }); + await writeFile(fullPath, fixtureContent(path)); + })); + const chooser = page.waitForEvent('filechooser'); + await page.getByRole('button', { name: new RegExp(label) }).click(); + await (await chooser).setFiles(directory); + await expect(page.getByRole('button', { name: '重新选择目录' })).toBeVisible(); } for (const entry of ['project', 'import'] as const) { @@ -148,7 +154,8 @@ for (const entry of ['project', 'import'] as const) { await expect(page.locator('.file-list-main')).toHaveText('robot/robot.URDF'); if (entry === 'project') await keepRoot.uncheck(); const expected = resources.map((relativePath) => ({ - relativePath: entry === 'import' ? `robot/${relativePath}` : relativePath, sizeBytes: 4, + relativePath: entry === 'import' ? `robot/${relativePath}` : relativePath, + sizeBytes: Buffer.byteLength(fixtureContent(relativePath)), })).sort((a, b) => a.relativePath.localeCompare(b.relativePath)); await page.getByRole('button', { name: entry === 'project' ? '上传并绑定' : '创建迁移任务', exact: true }).click(); await expect.poll(() => requests.confirmed.length).toBe(1); @@ -162,6 +169,66 @@ for (const entry of ['project', 'import'] as const) { }); } +for (const entry of ['project', 'import'] as const) { + test(`${entry} 在签名前修正嵌套 ROS 包、跨包同名文件和大小写引用,按改写后的字节数上传`, async ({ page }) => { + const requests = await prepare(page, entry); + const result = await page.evaluate(async (entry) => { + const moduleUrl = '/src/api/index.ts'; + const api = await import(moduleUrl); + const source = ``; + const main = 'urdf_pkg/Alexbot_simple_urdf/urdf/robot.urdf'; + const selected = [ + { relativePath: main, file: new File([source], 'robot.urdf') }, + { relativePath: 'urdf_pkg/Alexbot_simple_urdf/meshes/base_link.STL', file: new File(['mesh'], 'base_link.STL') }, + { relativePath: 'urdf_pkg/Alexbot_simple_urdf/textures/body.png', file: new File(['image'], 'body.png') }, + { relativePath: 'camera/meshes/base_link.STL', file: new File(['camera'], 'base_link.STL') }, + ]; + if (entry === 'project') await api.uploadUrdf('test-robot', selected); + else await api.uploadImportUrdf('https://github.com/example/robot', selected); + return { main, original: source, sourceAfter: await selected[0].file.text() }; + }, entry); + expect(result.sourceAfter).toBe(result.original); + expect(requests.signed).toHaveLength(1); + const uploads = await page.evaluate(() => JSON.parse(localStorage.getItem('urdf_test_uploads') ?? '[]')) as Record[]; + const urdf = uploads.find(({ key }) => key === result.main)!; + expect(urdf.fileContent).toContain('filename="../meshes/base_link.STL"'); + expect(urdf.fileContent).toContain('filename="../textures/body.png"'); + expect(urdf.fileContent).toContain('filename="../../../camera/meshes/base_link.STL"'); + expect(requests.signed[0].find(({ relativePath }) => relativePath === result.main)?.sizeBytes).toBe(Buffer.byteLength(urdf.fileContent)); + expect(uploads.find(({ key }) => key.endsWith('/textures/body.png'))?.fileContent).toBe('image'); + if (entry === 'project') expect(requests.confirmed[0]).toEqual(requests.signed[0]); + }); +} + +test('缺失和歧义资源在签名前报错,外部 URL 保留原值', async ({ page }) => { + const requests = await prepare(page, 'project'); + const errors = await page.evaluate(async () => { + const moduleUrl = '/src/api/index.ts'; + const api = await import(moduleUrl); + const errors: string[] = []; + for (const paths of [[], ['left/body.stl', 'right/body.stl']]) { + const selected = [ + { relativePath: 'robot.urdf', file: new File([''], 'robot.urdf') }, + ...paths.map((relativePath) => ({ relativePath, file: new File(['mesh'], 'body.stl') })), + ]; + try { await api.uploadUrdf('test-robot', selected); } catch (error) { errors.push((error as Error).message); } + } + return errors; + }); + expect(errors).toHaveLength(2); + expect(errors.every((error) => error.includes('资源缺失或路径不唯一'))).toBe(true); + expect(requests.signed).toHaveLength(0); + expect(requests.confirmed).toHaveLength(0); + const unchanged = await page.evaluate(async () => { + const moduleUrl = '/src/utils/urdfPaths.ts'; + const { normalizeUrdfSelection } = await import(moduleUrl); + const file = new File([''], 'robot.urdf'); + const selected = [{ relativePath: 'robot.urdf', file }]; + return (await normalizeUrdfSelection(selected))[0].file === file; + }); + expect(unchanged).toBe(true); +}); + test('过滤后仍校验多个或缺失的 URDF,全部过滤时阻止创建,清除后可继续', async ({ page }) => { const requests = await prepare(page, 'import'); await chooseDirectory(page, ['first.urdf', 'second.URDF', 'README.md']); diff --git a/vibemake-admin-urdf-fix.zip b/vibemake-admin-urdf-fix.zip new file mode 100644 index 0000000..46576b3 Binary files /dev/null and b/vibemake-admin-urdf-fix.zip differ