diff --git a/Dockerfile b/Dockerfile index 2bf8e81..adb8d84 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ============================================================================= -# Asset Bridge 3D — Dockerfile for Coolify +# Upload GLTF — Dockerfile for Coolify # Node 20 Debian · Blender (headless) · Multi-stage build # ============================================================================= @@ -27,7 +27,7 @@ RUN npm run build # --- Stage 3: Production ----------------------------------------------------- FROM node:20-slim AS runner -LABEL maintainer="Asset Bridge 3D" +LABEL maintainer="La Fabrik Durable" LABEL description="Secure 3D asset upload interface with Draco compression and GitHub push" # Install Blender (headless) + tini diff --git a/README.md b/README.md index 7f0eff7..17dbb19 100644 --- a/README.md +++ b/README.md @@ -64,23 +64,27 @@ docker run -p 3000:3000 \ upload-gltf ``` -The Dockerfile includes Blender headless for automatic Draco compression. +The Docker image includes Blender headless (installed once at build time). On startup, the entrypoint checks if Blender is available and logs its version. No extra configuration is needed in production — `BLENDER_PATH` defaults to `blender` which is in the container's PATH. ## How it works 1. The user enters their access key 2. They pick a **destination** (`farm`, `map`, `powergrid`, `workshop`, `general`, `environment`) 3. They select a folder containing: - - `model.glb` or `model.gltf` (required) - - Textures: `roughness`, `normal`, `metalness`, `color`, `displace` (`.png/.jpg/.webp`, optional) + - `model.glb` or `model.gltf` (**required**) + - Textures: `roughness`, `normal`, `metalness`, `color`, `displace` (`.png/.jpg/.webp`, **optional** — missing textures show a warning but don't block the upload) 4. The model is displayed in a 3D preview 5. On clicking "Envoyer sur GitHub": - - The app checks if the folder already exists on the remote repo - - If it exists, a confirmation dialog is shown listing the existing files that will be replaced - - On confirmation (or if the folder is new), all files are sent to the `/api/upload` endpoint + - The app computes the git SHA of each local file and compares with the remote repo + - If the folder doesn't exist, upload proceeds directly + - If the folder exists and files differ, a confirmation dialog shows **only the actual changes** (modified, new, or deleted files) + - If the folder exists and nothing changed, the upload is skipped entirely ("Aucun fichier modifie") 6. For models: the file is written to `/tmp`, compressed with Blender Draco, then the compressed version is pushed 7. For textures: pushed directly without compression -8. All files are pushed in a **single commit** with a formatted message: +8. **Only changed and new files are pushed** — unchanged files are skipped to save bandwidth and API calls +9. All changes are pushed in a **single commit** with a formatted message that only lists what changed: + + **New folder:** ``` update: upload-gltf add a new model -> farm/my-model @@ -90,8 +94,19 @@ The Dockerfile includes Blender headless for automatic Draco compression. ✅ color.jpg ❌ metalness (manquant) ``` -9. If the folder already existed, orphan files (present in the old version but not in the new upload) are deleted in the same commit -10. If Blender is unavailable, the original model is pushed as-is (graceful fallback) + + **Update (only metalness changed):** + ``` + update: upload-gltf update -> general/coffeetest + + 🎨 Textures + 🔄 metalness.jpg + ``` + + Symbols: `✅` new — `🔄` modified — `❌` missing or deleted + +10. Orphan files (present on remote but not in the new upload) are deleted in the same commit +11. If Blender is unavailable, the original model is pushed as-is (graceful fallback) ## Destinations @@ -110,16 +125,18 @@ Uploaded models are pushed to `public/models///` in the ``` app/ -├── api/upload/route.ts # API: GET (check existence) + POST (compress + push) +├── api/upload/route.ts # API: GET (check + SHA diff) + POST (compress + smart push) ├── globals.css # Tailwind + Google Fonts ├── layout.tsx # Root layout └── page.tsx # Home page components/ -├── UploadZone.tsx # UI: key input, destination picker, folder picker, validation, overwrite confirmation, upload +├── UploadZone.tsx # UI: key input, destination picker, folder picker, SHA diff, overwrite confirmation, upload ├── ModelViewer.tsx # Lazy wrapper for the 3D viewer └── SceneViewer.tsx # Three.js Canvas scripts/ └── compress.py # Blender Draco compression script +Dockerfile # Multi-stage build: Node 20 slim + Blender headless + tini +docker-entrypoint.sh # Startup: Blender check + launch ``` ## Supported Formats diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts index 585e8df..13a2475 100644 --- a/app/api/upload/route.ts +++ b/app/api/upload/route.ts @@ -5,6 +5,7 @@ import { mkdir, writeFile, readFile, unlink, rm } from 'fs/promises' import { existsSync } from 'fs' import { execFile } from 'child_process' import { promisify } from 'util' +import { createHash } from 'crypto' const execFileAsync = promisify(execFile) @@ -46,6 +47,13 @@ function parseRepoUrl(): { owner: string; repo: string } { return { owner: match[1], repo: match[2] } } +/** Compute the SHA that Git would assign to a blob with this content */ +function computeGitBlobSha(content: Buffer): string { + const header = `blob ${content.length}\0` + const store = Buffer.concat([Buffer.from(header), content]) + return createHash('sha1').update(store).digest('hex') +} + // --------------------------------------------------------------------------- // Blender Draco compression // --------------------------------------------------------------------------- @@ -94,29 +102,60 @@ function buildCommitMessage( modelFilename: string, textureNames: string[], compressed: boolean, + isReplace: boolean, + fileChanges: Map, + deletedFileNames: string[], ): string { - const title = `update: upload-gltf add a new model -> ${destination}/${folderName}` + const title = isReplace + ? `update: upload-gltf update -> ${destination}/${folderName}` + : `update: upload-gltf add a new model -> ${destination}/${folderName}` + const lines: string[] = [title, ''] + + // Model section — only show if changed or new + const modelChange = fileChanges.get(modelFilename.toLowerCase()) + if (modelChange === 'new') { + lines.push('📦 Model') + lines.push(` ✅ ${modelFilename}${compressed ? ' (compressed)' : ''}`) + } else if (modelChange === 'changed') { + lines.push('📦 Model') + lines.push(` 🔄 ${modelFilename}${compressed ? ' (compressed)' : ''}`) + } + // unchanged → don't show model section at all + + // Textures section — only show lines that have changes const foundTextures = new Set( textureNames.map(t => t.toLowerCase().replace(/\.[^.]+$/, '')) ) - const textureLines = REQUIRED_TEXTURES.map(tex => { - if (foundTextures.has(tex)) { - const actual = textureNames.find(t => t.toLowerCase().replace(/\.[^.]+$/, '') === tex) - return ` ✅ ${actual}` - } - return ` ❌ ${tex} (manquant)` - }) + const textureLines: string[] = [] - const lines = [ - title, - '', - '📦 Model', - ` ✅ ${modelFilename}${compressed ? ' (compressed)' : ''}`, - '🎨 Textures', - ...textureLines, - ] + // Changed or new textures + for (const tex of REQUIRED_TEXTURES) { + if (foundTextures.has(tex)) { + const actual = textureNames.find(t => t.toLowerCase().replace(/\.[^.]+$/, '') === tex)! + const change = fileChanges.get(actual.toLowerCase()) + if (change === 'new') { + textureLines.push(` ✅ ${actual}`) + } else if (change === 'changed') { + textureLines.push(` 🔄 ${actual}`) + } + // unchanged → skip + } else if (!isReplace) { + // Only show missing textures for new uploads + textureLines.push(` ❌ ${tex} (manquant)`) + } + } + + // Deleted files (orphans removed from remote) + for (const name of deletedFileNames) { + textureLines.push(` ❌ ${name} (supprime)`) + } + + if (textureLines.length > 0) { + lines.push('🎨 Textures') + lines.push(...textureLines) + } return lines.join('\n') } @@ -313,7 +352,7 @@ export async function GET(req: NextRequest) { }) if (Array.isArray(data)) { - const existingFiles = data.map(f => f.name) + const existingFiles = data.map(f => ({ name: f.name, sha: f.sha })) return NextResponse.json({ success: true, exists: true, @@ -412,12 +451,9 @@ export async function POST(req: NextRequest) { }) } - // --- Build commit message --- - const commitMessage = buildCommitMessage(folderName, destination, modelFilename, textureNames, compressed) - - // --- Detect existing files to clean up orphans --- + // --- Detect existing files and compare SHA to classify changes --- const folderPath = `public/models/${destination}/${folderName}` - let existingFilePaths: string[] = [] + const remoteFileMap = new Map() // name -> sha try { const octokit = getOctokit() @@ -432,23 +468,78 @@ export async function POST(req: NextRequest) { }) if (Array.isArray(data)) { - existingFilePaths = data.map(f => `${folderPath}/${f.name}`) + for (const f of data) { + remoteFileMap.set(f.name.toLowerCase(), f.sha) + } } } catch { - // 404 = folder doesn't exist yet, no cleanup needed + // 404 = folder doesn't exist yet } + const isReplace = remoteFileMap.size > 0 + + // Classify each file: changed, new, or unchanged + type FileChange = 'new' | 'changed' | 'unchanged' + const fileChanges = new Map() + const changedFilesToPush: { path: string; contentBase64: string }[] = [] + + for (const f of filesToPush) { + const filename = f.path.split('/').pop()! + const localSha = computeGitBlobSha(Buffer.from(f.contentBase64, 'base64')) + const remoteSha = remoteFileMap.get(filename.toLowerCase()) + + if (!remoteSha) { + fileChanges.set(filename.toLowerCase(), 'new') + changedFilesToPush.push(f) + } else if (remoteSha !== localSha) { + fileChanges.set(filename.toLowerCase(), 'changed') + changedFilesToPush.push(f) + } else { + fileChanges.set(filename.toLowerCase(), 'unchanged') + // skip — don't push unchanged files + } + } + + // Files on remote that are not in the new upload → will be deleted (orphans) + const newFileNames = new Set(filesToPush.map(f => f.path.split('/').pop()!.toLowerCase())) + const deletedFileNames: string[] = [] + const deletePaths: string[] = [] + for (const [name] of remoteFileMap) { + if (!newFileNames.has(name)) { + deletedFileNames.push(name) + deletePaths.push(`${folderPath}/${name}`) + } + } + + // If nothing changed at all, don't create an empty commit + if (changedFilesToPush.length === 0 && deletePaths.length === 0) { + return NextResponse.json({ + success: true, + folderName, + filesCount: 0, + compressed, + compressionError: compressionError || undefined, + message: 'Aucun fichier modifie — rien a envoyer.', + }) + } + + // --- Build commit message --- + const commitMessage = buildCommitMessage( + folderName, destination, modelFilename, textureNames, + compressed, isReplace, fileChanges, deletedFileNames, + ) + // --- Push all in one commit --- try { - const { commitUrl } = await pushAllToGitHub(folderName, filesToPush, existingFilePaths, commitMessage) + const { commitUrl } = await pushAllToGitHub(folderName, changedFilesToPush, deletePaths, commitMessage) return NextResponse.json({ success: true, folderName, - filesCount: filesToPush.length, + filesCount: changedFilesToPush.length, compressed, compressionError: compressionError || undefined, - message: `${filesToPush.length} fichier(s) envoye(s) sur GitHub en un seul commit.`, + message: `${changedFilesToPush.length} fichier(s) modifie(s) envoye(s) sur GitHub en un seul commit.`, commitUrl, }) } catch (err) { diff --git a/components/UploadZone.tsx b/components/UploadZone.tsx index 89fc12f..480f371 100644 --- a/components/UploadZone.tsx +++ b/components/UploadZone.tsx @@ -88,23 +88,85 @@ function validateFolder(files: File[]): { model?: File; textures: TextureFile[]; return result } -async function checkFolderExists( - folderName: string, +/** Compute the git blob SHA1 for a file (same as `git hash-object`) */ +async function computeGitBlobSha(file: File): Promise { + const buffer = await file.arrayBuffer() + const content = new Uint8Array(buffer) + const header = new TextEncoder().encode(`blob ${content.length}\0`) + const store = new Uint8Array(header.length + content.length) + store.set(header) + store.set(content, header.length) + const hashBuffer = await crypto.subtle.digest('SHA-1', store) + const hashArray = Array.from(new Uint8Array(hashBuffer)) + return hashArray.map(b => b.toString(16).padStart(2, '0')).join('') +} + +interface FileDiff { + name: string + status: 'changed' | 'new' | 'deleted' +} + +interface CheckResult { + exists: boolean + diffs: FileDiff[] +} + +async function checkFolderDiffs( + folder: FolderEntry, destination: string, secret: string, -): Promise<{ exists: boolean; files: string[] }> { +): Promise { try { - const params = new URLSearchParams({ folderName, destination }) + const params = new URLSearchParams({ folderName: folder.folderName, destination }) const res = await fetch(`/api/upload?${params}`, { headers: { 'x-upload-secret': secret.trim() }, }) const data = await res.json() - if (data.success && data.exists) { - return { exists: true, files: data.files || [] } + if (!data.success || !data.exists) { + return { exists: false, diffs: [] } } - return { exists: false, files: [] } + + const remoteFiles: { name: string; sha: string }[] = data.files || [] + const remoteMap = new Map(remoteFiles.map(f => [f.name.toLowerCase(), f.sha])) + + // Compute SHA for all local files + const localFiles: { name: string; sha: string }[] = [] + localFiles.push({ + name: folder.modelFile.name, + sha: await computeGitBlobSha(folder.modelFile), + }) + for (const tex of folder.textures) { + localFiles.push({ + name: tex.name, + sha: await computeGitBlobSha(tex.file), + }) + } + + const diffs: FileDiff[] = [] + const localNames = new Set() + + for (const local of localFiles) { + const key = local.name.toLowerCase() + localNames.add(key) + const remoteSha = remoteMap.get(key) + if (!remoteSha) { + diffs.push({ name: local.name, status: 'new' }) + } else if (remoteSha !== local.sha) { + diffs.push({ name: local.name, status: 'changed' }) + } + // unchanged → not in diffs + } + + // Files on remote but not in local → deleted + for (const [name] of remoteMap) { + if (!localNames.has(name)) { + diffs.push({ name, status: 'deleted' }) + } + } + + return { exists: true, diffs } } catch { - return { exists: false, files: [] } + return { exists: false, diffs: [] } } } @@ -163,7 +225,7 @@ export default function UploadZone() { const [secretError, setSecretError] = useState(null) const [destination, setDestination] = useState(DESTINATIONS[0].value) const [abortController, setAbortController] = useState(null) - const [overwriteConfirm, setOverwriteConfirm] = useState<{ folderName: string; files: string[] } | null>(null) + const [overwriteConfirm, setOverwriteConfirm] = useState<{ folderName: string; diffs: FileDiff[] } | null>(null) const isSecretEmpty = !secret.trim() @@ -181,11 +243,16 @@ export default function UploadZone() { setSecretError(null) setGlobalError(null) - // Check if folder already exists on remote + // Check if folder already exists on remote and compute diffs const folder = files[0] - const check = await checkFolderExists(folder.folderName, destination, secret) + const check = await checkFolderDiffs(folder, destination, secret) if (check.exists) { - setOverwriteConfirm({ folderName: folder.folderName, files: check.files }) + if (check.diffs.length === 0) { + // Nothing changed at all + setGlobalError('Aucun fichier modifie — le dossier distant est identique.') + return + } + setOverwriteConfirm({ folderName: folder.folderName, diffs: check.diffs }) return } @@ -532,26 +599,41 @@ export default function UploadZone() {

Dossier deja existant

- {destination}/{overwriteConfirm.folderName} existe deja sur le repo. + {destination}/{overwriteConfirm.folderName} existe deja sur le repo

- {overwriteConfirm.files.length > 0 && ( -
-

Fichiers existants qui seront remplaces :

-
    - {overwriteConfirm.files.map((f) => ( -
  • {f}
  • + {overwriteConfirm.diffs.length > 0 && ( +
    +

    Modifications detectees :

    +
      + {overwriteConfirm.diffs.map((d) => ( +
    • + {d.status === 'changed' && ( + <> + 🔄 + {d.name} + + )} + {d.status === 'new' && ( + <> + + {d.name} + + )} + {d.status === 'deleted' && ( + <> + + {d.name} + + )} +
    • ))}
    )} -

    - Les anciens fichiers seront supprimes et remplaces par les nouveaux. Cette action est irreversible. -

    -