Files
upload-gltf/lib/nextcloud.ts
T
Tom Boullay 78f4aa83e0 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
2026-04-14 17:19:10 +02:00

141 lines
4.5 KiB
TypeScript

// ---------------------------------------------------------------------------
// Nextcloud WebDAV client
// 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 || ''
if (!url || !token) {
throw new Error('Nextcloud non configure (NEXTCLOUD_URL, NEXTCLOUD_SHARE_TOKEN)')
}
// Public share WebDAV: https://cloud.example.com/public.php/webdav/
const davBase = `${url.replace(/\/+$/, '')}/public.php/webdav`
const auth = 'Basic ' + Buffer.from(`${token}:${password}`).toString('base64')
cachedConfig = { davBase, auth }
return cachedConfig
}
function davUrl(davBase: string, path: string): string {
const clean = path.replace(/^\/+/, '').replace(/\/+$/, '')
return `${davBase}/${clean}`
}
// ---------------------------------------------------------------------------
// Low-level WebDAV helpers
// ---------------------------------------------------------------------------
async function davRequest(
method: string,
path: string,
body?: Buffer | string | null,
extraHeaders?: Record<string, string>,
): Promise<Response> {
const { davBase, auth } = getConfig()
const url = davUrl(davBase, path)
const res = await fetch(url, {
method,
headers: {
Authorization: auth,
...extraHeaders,
},
body: body ?? undefined,
})
return res
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/** Check if a folder exists on the Nextcloud instance. */
export async function folderExists(path: string): Promise<boolean> {
try {
const res = await davRequest('PROPFIND', path + '/', null, { Depth: '0' })
return res.status >= 200 && res.status < 300
} catch {
return false
}
}
/**
* Create a folder and all parent segments if they don't exist.
* 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('/')
let current = ''
for (const seg of segments) {
current += '/' + seg
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)}`)
}
}
}
/** Upload a file (overwrite if exists). */
export async function uploadFile(path: string, content: Buffer): Promise<void> {
const res = await davRequest('PUT', path, content, {
'Content-Type': 'application/octet-stream',
'Content-Length': String(content.length),
})
if (res.status < 200 || res.status >= 300) {
const text = await res.text().catch(() => '')
throw new Error(`PUT ${path} failed (${res.status}): ${text.slice(0, 200)}`)
}
}
/** Move (rename) a folder or file. */
export async function moveFolder(from: string, to: string): Promise<void> {
const { davBase } = getConfig()
const destination = davUrl(davBase, to) + '/'
const res = await davRequest('MOVE', from + '/', null, {
Destination: destination,
Overwrite: 'F',
})
if (res.status < 200 || res.status >= 300) {
const text = await res.text().catch(() => '')
throw new Error(`MOVE ${from} -> ${to} failed (${res.status}): ${text.slice(0, 200)}`)
}
}
// ---------------------------------------------------------------------------
// High-level: find next available version folder
// ---------------------------------------------------------------------------
/**
* Find the next available Vx folder for archiving.
* E.g. if V1/coffeetest exists but V2/coffeetest doesn't, returns "V2".
*/
export async function findNextVersion(
basePath: string,
folderName: string,
): Promise<string> {
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})`)
}