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:
@@ -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' }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user