refactor: full codebase audit — extract modules, fix type safety, clean dead code

- Extract API helpers from UploadZone into lib/upload-api.ts (FormData builder, checkFolderDiffs, uploadDrive, uploadGit)
- Extract upload orchestration into hooks/useUploadOrchestrator.ts (UploadZone: 489 → 162 lines)
- Extract file diff classification into lib/diff-files.ts (from git route)
- Extract shared SVG icons into components/ui/icons.tsx (7 icons, 0 duplication)
- Extract shared modal wrapper into components/ui/Modal.tsx + ModalActions
- Extract DriveStatusLine sub-component from FolderCard
- Fix checkFolderDiffs silently swallowing auth/network errors (now throws)
- Fix type safety: remove as never casts, add isHttpError type guard, use discriminated union for validateFolder
- Fix nextcloud: cache getConfig, add max bound to findNextVersion, optimize mkdirRecursive (skip PROPFIND)
- Fix drive route: remove req.clone(), extend parseMultiUpload to return extra fields
- Fix commit message: model shown as unchanged with ↔️ on updates (not falsely marked as modified)
- Clean dead code: unused folderExists import, FileStatus/DriveStatus exports, ParsedFile.textureName, getConfig basePath
- Add security headers in next.config.ts (HSTS, X-Content-Type-Options, X-Frame-Options, etc.)
- Update README with new project structure
This commit is contained in:
Tom Boullay
2026-04-14 17:19:10 +02:00
parent 110d64ec33
commit 78f4aa83e0
26 changed files with 957 additions and 721 deletions
+2 -2
View File
@@ -2,14 +2,14 @@
// Client-side types — used by components and hooks (no Node.js Buffer)
// ---------------------------------------------------------------------------
export type FileStatus = 'pending' | 'uploading' | 'success' | 'error'
type FileStatus = 'pending' | 'uploading' | 'success' | 'error'
export interface TextureFile {
name: string
file: File
}
export type DriveStatus = 'pending' | 'uploading' | 'success' | 'error' | 'skipped'
type DriveStatus = 'pending' | 'uploading' | 'success' | 'error' | 'skipped'
export interface FolderEntry {
folderName: string
+2 -2
View File
@@ -8,9 +8,9 @@ export const ALL_ALLOWED_EXTENSIONS = new Set([...MODEL_EXTENSIONS, ...TEXTURE_E
export const REQUIRED_TEXTURES = ['roughness', 'normal', 'metalness', 'color', 'displace'] as const
export const VALID_DESTINATIONS = new Set([
export const VALID_DESTINATIONS = new Set<string>([
'farm', 'map', 'powergrid', 'workshop', 'general', 'environment',
] as const)
])
export const DESTINATIONS = [
{ value: 'farm', label: 'Farm' },
+83
View File
@@ -0,0 +1,83 @@
// ---------------------------------------------------------------------------
// File diff classification — compares local files against a remote file map
// ---------------------------------------------------------------------------
import { MODEL_EXTENSIONS } from './constants'
import type { FileChange } from './types'
interface PushFile {
path: string
contentBase64: string
}
export interface DiffResult {
/** Map of lowercase filename → change status (for commit message) */
fileChanges: Map<string, FileChange>
/** Files that actually need to be pushed (new or changed) */
changedFilesToPush: PushFile[]
/** Filenames that were on remote but not in the new upload */
deletedFileNames: string[]
/** Full paths for deletion on remote */
deletePaths: string[]
}
/**
* Classify each file as new, changed, or unchanged by comparing against
* the remote file map.
*
* Rules:
* - Models: always re-pushed (compression makes size comparison unreliable),
* but marked as 'unchanged' in the commit message when the folder already
* exists (we can't know if the model really changed after Blender).
* - Textures: compared by size (not compressed, reliable).
* - Orphan remote files: classified as deletions.
*/
export function classifyFileChanges(
filesToPush: PushFile[],
remoteFileMap: Map<string, number>,
folderPath: string,
): DiffResult {
const fileChanges = new Map<string, FileChange>()
const changedFilesToPush: PushFile[] = []
for (const f of filesToPush) {
const filename = f.path.split('/').pop() ?? ''
const ext = filename.slice(filename.lastIndexOf('.')).toLowerCase()
const isModel = MODEL_EXTENSIONS.has(ext)
if (isModel) {
// Model: always re-push since compression makes size comparison unreliable.
// Mark as 'unchanged' for the commit message when the folder already exists.
const remoteSize = remoteFileMap.get(filename.toLowerCase())
fileChanges.set(filename.toLowerCase(), remoteSize === undefined ? 'new' : 'unchanged')
changedFilesToPush.push(f)
} else {
// Texture: compare by size
const localSize = Buffer.from(f.contentBase64, 'base64').length
const remoteSize = remoteFileMap.get(filename.toLowerCase())
if (remoteSize === undefined) {
fileChanges.set(filename.toLowerCase(), 'new')
changedFilesToPush.push(f)
} else if (remoteSize !== localSize) {
fileChanges.set(filename.toLowerCase(), 'changed')
changedFilesToPush.push(f)
} else {
fileChanges.set(filename.toLowerCase(), 'unchanged')
}
}
}
// Files on remote not in the new upload → 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}`)
}
}
return { fileChanges, changedFilesToPush, deletedFileNames, deletePaths }
}
+1 -1
View File
@@ -3,7 +3,7 @@
// ---------------------------------------------------------------------------
export function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B'
if (bytes <= 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
+5 -2
View File
@@ -5,6 +5,10 @@ import type { RemoteFile } from './types'
// Octokit helpers
// ---------------------------------------------------------------------------
function isHttpError(err: unknown): err is { status: number } {
return typeof err === 'object' && err !== null && 'status' in err && typeof (err as Record<string, unknown>).status === 'number'
}
function getOctokit(): Octokit {
const token = process.env.GITHUB_TOKEN
if (!token) throw new Error('GITHUB_TOKEN non configure')
@@ -53,8 +57,7 @@ export async function getRemoteFolder(
return { exists: false, files: [] }
} catch (err: unknown) {
const status = (err as { status?: number })?.status
if (status === 404) {
if (isHttpError(err) && err.status === 404) {
return { exists: false, files: [] }
}
throw err
+17 -12
View File
@@ -3,11 +3,17 @@
// Uses native fetch — no npm package needed.
// ---------------------------------------------------------------------------
const MAX_VERSIONS = 1000
// Lazy-cached config to avoid recomputing on every request
let cachedConfig: { davBase: string; auth: string } | null = null
function getConfig() {
if (cachedConfig) return cachedConfig
const url = process.env.NEXTCLOUD_URL
const token = process.env.NEXTCLOUD_SHARE_TOKEN
const password = process.env.NEXTCLOUD_SHARE_PASSWORD || ''
const basePath = process.env.NEXTCLOUD_BASE_PATH || 'Models'
if (!url || !token) {
throw new Error('Nextcloud non configure (NEXTCLOUD_URL, NEXTCLOUD_SHARE_TOKEN)')
@@ -17,7 +23,8 @@ function getConfig() {
const davBase = `${url.replace(/\/+$/, '')}/public.php/webdav`
const auth = 'Basic ' + Buffer.from(`${token}:${password}`).toString('base64')
return { davBase, auth, basePath }
cachedConfig = { davBase, auth }
return cachedConfig
}
function davUrl(davBase: string, path: string): string {
@@ -66,7 +73,7 @@ export async function folderExists(path: string): Promise<boolean> {
/**
* Create a folder and all parent segments if they don't exist.
* Like `mkdir -p`.
* Like `mkdir -p`. Attempts MKCOL directly and handles 405 (already exists).
*/
export async function mkdirRecursive(path: string): Promise<void> {
const segments = path.replace(/^\/+/, '').replace(/\/+$/, '').split('/')
@@ -74,14 +81,11 @@ export async function mkdirRecursive(path: string): Promise<void> {
for (const seg of segments) {
current += '/' + seg
const exists = await folderExists(current)
if (!exists) {
const res = await davRequest('MKCOL', current + '/')
if (res.status !== 201 && res.status !== 405) {
// 405 = already exists (race condition), that's fine
const text = await res.text().catch(() => '')
throw new Error(`MKCOL ${current} failed (${res.status}): ${text.slice(0, 200)}`)
}
const res = await davRequest('MKCOL', current + '/')
if (res.status !== 201 && res.status !== 405) {
// 201 = created, 405 = already exists — both are fine
const text = await res.text().catch(() => '')
throw new Error(`MKCOL ${current} failed (${res.status}): ${text.slice(0, 200)}`)
}
}
}
@@ -127,9 +131,10 @@ export async function findNextVersion(
basePath: string,
folderName: string,
): Promise<string> {
for (let i = 1; ; i++) {
for (let i = 1; i <= MAX_VERSIONS; i++) {
const versionPath = `${basePath}/V${i}/${folderName}`
const exists = await folderExists(versionPath)
if (!exists) return `V${i}`
}
throw new Error(`Nombre maximum de versions atteint (V${MAX_VERSIONS})`)
}
+24 -9
View File
@@ -4,21 +4,27 @@ import { sanitizeFilename } from './sanitize'
import { ALL_ALLOWED_EXTENSIONS, MODEL_EXTENSIONS, VALID_DESTINATIONS, MAX_FILE_SIZE } from './constants'
import type { ParsedFile } from './types'
/**
* Parse a multi-file FormData upload request.
* Validates destination, file extensions, file sizes, and returns parsed files.
*/
export async function parseMultiUpload(req: NextRequest): Promise<{
export interface ParsedUpload {
folderName: string
destination: string
files: ParsedFile[]
}> {
/** Any extra string fields from the FormData (e.g. "action") */
extra: Record<string, string>
}
/**
* Parse a multi-file FormData upload request.
* Validates destination, file extensions, file sizes, and returns parsed files.
* Extra string fields (beyond folderName, destination, files, fileTypes, textureNames)
* are returned in `extra`.
*/
export async function parseMultiUpload(req: NextRequest): Promise<ParsedUpload> {
const formData = await req.formData()
const folderName = (formData.get('folderName') as string | null)?.trim() || 'assets'
const safeFolderName = sanitizeFilename(folderName).replace(/[^a-zA-Z0-9-_]/g, '-')
const rawDestination = (formData.get('destination') as string | null)?.trim() || 'general'
if (!VALID_DESTINATIONS.has(rawDestination as never)) {
if (!VALID_DESTINATIONS.has(rawDestination)) {
throw new Error(`Destination invalide: "${rawDestination}"`)
}
const destination = rawDestination
@@ -27,6 +33,15 @@ export async function parseMultiUpload(req: NextRequest): Promise<{
const fileTypes = formData.getAll('fileTypes') as string[]
const textureNames = formData.getAll('textureNames') as string[]
// Collect extra string fields
const knownKeys = new Set(['folderName', 'destination', 'files', 'fileTypes', 'textureNames'])
const extra: Record<string, string> = {}
for (const [key, value] of formData.entries()) {
if (!knownKeys.has(key) && typeof value === 'string') {
extra[key] = value
}
}
// Runtime validation: ensure entries are actual File objects
const fileEntries: File[] = []
for (const entry of rawFiles) {
@@ -73,8 +88,8 @@ export async function parseMultiUpload(req: NextRequest): Promise<{
const isModel = MODEL_EXTENSIONS.has(ext)
const buffer = Buffer.from(await file.arrayBuffer())
parsed.push({ filename, buffer, isModel, textureName: texName || undefined })
parsed.push({ filename, buffer, isModel })
}
return { folderName: safeFolderName, destination, files: parsed }
return { folderName: safeFolderName, destination, files: parsed, extra }
}
-1
View File
@@ -6,7 +6,6 @@ export interface ParsedFile {
filename: string
buffer: Buffer
isModel: boolean
textureName?: string
}
export type FileChange = 'new' | 'changed' | 'unchanged'
+182
View File
@@ -0,0 +1,182 @@
// ---------------------------------------------------------------------------
// Client-side API helpers for upload operations
// ---------------------------------------------------------------------------
import type { FolderEntry } from './client-types'
import type { FileDiff } from './types'
export interface CheckResult {
exists: boolean
diffs: FileDiff[]
}
// ---------------------------------------------------------------------------
// Shared FormData builder
// ---------------------------------------------------------------------------
function buildUploadFormData(
folder: FolderEntry,
destination: string,
extra?: Record<string, string>,
): FormData {
const formData = new FormData()
formData.append('folderName', folder.folderName)
formData.append('destination', destination)
if (extra) {
for (const [key, value] of Object.entries(extra)) {
formData.append(key, value)
}
}
formData.append('files', folder.modelFile)
formData.append('fileTypes', 'model')
formData.append('textureNames', '')
for (const tex of folder.textures) {
formData.append('files', tex.file)
formData.append('fileTypes', 'texture')
formData.append('textureNames', tex.name)
}
return formData
}
// ---------------------------------------------------------------------------
// Check folder diffs against remote (GitHub)
// ---------------------------------------------------------------------------
/**
* Check whether a folder already exists on the remote repo and compute diffs.
* Throws on auth/network errors so callers can surface them to the user.
*/
export async function checkFolderDiffs(
folder: FolderEntry,
destination: string,
secret: string,
signal?: AbortSignal,
): Promise<CheckResult> {
const params = new URLSearchParams({ folderName: folder.folderName, destination })
const res = await fetch(`/api/upload/check?${params}`, {
headers: { 'x-upload-secret': secret.trim() },
signal,
})
const data = await res.json()
// Surface auth/server errors to the caller
if (!res.ok) {
throw new Error(data.error || `Erreur serveur (${res.status})`)
}
if (!data.success || !data.exists) {
return { exists: false, diffs: [] }
}
const remoteFiles: { name: string; size: number }[] = data.files || []
const remoteMap = new Map(remoteFiles.map((f) => [f.name.toLowerCase(), f.size]))
const diffs: FileDiff[] = []
const localNames = new Set<string>()
// Model: skip size comparison (compression changes the size).
const modelKey = folder.modelFile.name.toLowerCase()
localNames.add(modelKey)
if (!remoteMap.has(modelKey)) {
diffs.push({ name: folder.modelFile.name, status: 'new' })
}
// Textures: compare by size
for (const tex of folder.textures) {
const key = tex.name.toLowerCase()
localNames.add(key)
const remoteSize = remoteMap.get(key)
if (remoteSize === undefined) {
diffs.push({ name: tex.name, status: 'new' })
} else if (remoteSize !== tex.file.size) {
diffs.push({ name: tex.name, status: 'changed' })
}
}
// Deleted
for (const [name] of remoteMap) {
if (!localNames.has(name)) {
diffs.push({ name, status: 'deleted' })
}
}
return { exists: true, diffs }
}
// ---------------------------------------------------------------------------
// Upload original files to Nextcloud Drive
// ---------------------------------------------------------------------------
/** Upload original files to Nextcloud Drive (no Blender compression). */
export async function uploadDrive(
folder: FolderEntry,
secret: string,
destination: string,
action: 'new' | 'replace',
signal?: AbortSignal,
): Promise<{ success: boolean; error?: string }> {
const formData = buildUploadFormData(folder, destination, { action })
try {
const res = await fetch('/api/upload/drive', {
method: 'POST',
headers: { 'x-upload-secret': secret.trim() },
body: formData,
signal,
})
const data = await res.json()
if (!data.success) return { success: false, error: data.error }
return { success: true }
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') {
return { success: false, error: 'Upload annule' }
}
return { success: false, error: 'Erreur reseau (Drive)' }
}
}
// ---------------------------------------------------------------------------
// Upload files to GitHub (with Blender compression)
// ---------------------------------------------------------------------------
/** Upload files to GitHub (with Blender compression). */
export async function uploadGit(
folder: FolderEntry,
secret: string,
destination: string,
onProgress: (pct: number) => void,
signal?: AbortSignal,
): Promise<{ success: boolean; filename?: string; error?: string }> {
const formData = buildUploadFormData(folder, destination)
onProgress(10)
try {
const res = await fetch('/api/upload/git', {
method: 'POST',
headers: { 'x-upload-secret': secret.trim() },
body: formData,
signal,
})
onProgress(80)
const data = await res.json()
if (!data.success) {
return { success: false, error: data.error }
}
onProgress(100)
return { success: true, filename: folder.folderName }
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') {
return { success: false, error: 'Upload annule' }
}
return { success: false, error: 'Erreur reseau' }
}
}
+18 -23
View File
@@ -13,22 +13,15 @@ function getTextureType(filename: string): string | null {
return null
}
export function validateFolder(files: File[]): {
model?: File
textures: TextureFile[]
errors: string[]
warnings: string[]
} {
const result: {
model?: File
textures: TextureFile[]
errors: string[]
warnings: string[]
} = {
textures: [],
errors: [],
warnings: [],
}
/** Discriminated union: either valid (with model) or invalid (with errors). */
export type ValidationResult =
| { ok: true; model: File; textures: TextureFile[]; warnings: string[] }
| { ok: false; errors: string[] }
export function validateFolder(files: File[]): ValidationResult {
const textures: TextureFile[] = []
const warnings: string[] = []
const errors: string[] = []
const modelFiles = files.filter((f) => {
const name = f.name.toLowerCase()
@@ -36,9 +29,7 @@ export function validateFolder(files: File[]): {
})
if (modelFiles.length === 0) {
result.errors.push('model.glb ou model.gltf manquant (obligatoire)')
} else {
result.model = modelFiles[0]
return { ok: false, errors: ['model.glb ou model.gltf manquant (obligatoire)'] }
}
const textureFiles = files.filter((f) => {
@@ -47,17 +38,21 @@ export function validateFolder(files: File[]): {
})
for (const tf of textureFiles) {
result.textures.push({ name: tf.name, file: tf })
textures.push({ name: tf.name, file: tf })
}
const foundTextures = new Set(
result.textures.map((t) => t.name.toLowerCase().replace(/\.[^.]+$/, '')),
textures.map((t) => t.name.toLowerCase().replace(/\.[^.]+$/, '')),
)
for (const req of REQUIRED_TEXTURES) {
if (!foundTextures.has(req)) {
result.warnings.push(`${req}.webp/png/jpg manquant`)
warnings.push(`${req}.webp/png/jpg manquant`)
}
}
return result
if (errors.length > 0) {
return { ok: false, errors }
}
return { ok: true, model: modelFiles[0], textures, warnings }
}