upadte: clean code + add next cloud

This commit is contained in:
Tom Boullay
2026-04-14 16:21:37 +02:00
parent 3adcf9d30e
commit 3a7a5e2eea
20 changed files with 663 additions and 131 deletions
+97 -16
View File
@@ -1,26 +1,107 @@
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import { validateUploadSecret } from '@/lib/auth'
import { parseMultiUpload } from '@/lib/parse-upload'
import {
folderExists,
mkdirRecursive,
moveFolder,
uploadFile,
findNextVersion,
} from '@/lib/nextcloud'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
// ---------------------------------------------------------------------------
// TODO: POST /api/upload/drive
// POST /api/upload/drive
//
// Upload files and push them to Google Drive.
// This route will share the same auth (lib/auth.ts), parsing (lib/parse-upload.ts),
// and Blender compression (lib/blender.ts) as the git route.
// Upload **original** files (no Blender compression) to Nextcloud Drive.
//
// Implementation steps:
// 1. Add `googleapis` package: npm install googleapis
// 2. Add env vars: GOOGLE_DRIVE_FOLDER_ID, GOOGLE_SERVICE_ACCOUNT_KEY (JSON)
// 3. Authenticate with Google Drive API using service account
// 4. Upload files to the target folder (create subfolders as needed)
// 5. Return the Drive folder URL
// FormData fields:
// - folderName, destination, files[], fileTypes[], textureNames[] (same as /api/upload/git)
// - action: "new" | "replace"
//
// Versioning logic:
// VF/{folderName} ← latest version
// V1/{folderName} ← first archive, V2/ second, etc.
//
// action="new" → just mkdir + upload into VF/
// action="replace" → archive VF → Vx, then re-upload all files into VF/
// ---------------------------------------------------------------------------
export async function POST() {
return NextResponse.json(
{ success: false, error: 'Google Drive upload not implemented yet' },
{ status: 501 },
)
export async function POST(req: NextRequest) {
// --- Auth ---
const authError = validateUploadSecret(req)
if (authError) return authError
// --- Check Nextcloud config ---
if (!process.env.NEXTCLOUD_URL || !process.env.NEXTCLOUD_USER || !process.env.NEXTCLOUD_PASSWORD) {
return NextResponse.json(
{ success: false, error: 'Nextcloud non configure sur le serveur' },
{ status: 500 },
)
}
// --- Parse files ---
// Clone the request before parseMultiUpload consumes the body stream,
// so we can read the `action` field separately.
const cloned = req.clone()
let folderName: string
let parsedFiles: Awaited<ReturnType<typeof parseMultiUpload>>['files']
let action: string
try {
const parsed = await parseMultiUpload(req)
folderName = parsed.folderName
parsedFiles = parsed.files
// Read action from the cloned request (parseMultiUpload doesn't expose it)
const formData = await cloned.formData()
action = (formData.get('action') as string | null)?.trim() || 'new'
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur inconnue'
return NextResponse.json({ success: false, error: message }, { status: 400 })
}
const basePath = process.env.NEXTCLOUD_BASE_PATH || 'Models'
const vfFolderPath = `${basePath}/VF/${folderName}`
try {
if (action === 'replace') {
// 1. Find the next available Vx
const nextVersion = await findNextVersion(basePath, folderName)
// 2. Ensure Vx/ exists
await mkdirRecursive(`${basePath}/${nextVersion}`)
// 3. Move VF/{folderName} → Vx/{folderName}
await moveFolder(vfFolderPath, `${basePath}/${nextVersion}/${folderName}`)
// 4. Re-create VF/{folderName}
await mkdirRecursive(vfFolderPath)
} else {
// action === 'new': just ensure VF/{folderName} exists
await mkdirRecursive(vfFolderPath)
}
// --- Upload all original files ---
for (const pf of parsedFiles) {
const remotePath = `${vfFolderPath}/${pf.filename}`
await uploadFile(remotePath, pf.buffer)
}
return NextResponse.json({
success: true,
folderName,
filesCount: parsedFiles.length,
message: `${parsedFiles.length} fichier(s) envoye(s) sur le Drive.`,
})
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur Nextcloud inconnue'
return NextResponse.json(
{ success: false, error: `Drive echoue: ${message}` },
{ status: 500 },
)
}
}