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
+4
View File
@@ -9,6 +9,8 @@ export interface TextureFile {
file: File
}
export type DriveStatus = 'pending' | 'uploading' | 'success' | 'error' | 'skipped'
export interface FolderEntry {
folderName: string
modelFile: File
@@ -20,4 +22,6 @@ export interface FolderEntry {
modelUrl?: string
viewerOpen?: boolean
warnings: string[]
driveStatus?: DriveStatus
driveError?: string
}
+3 -11
View File
@@ -1,18 +1,17 @@
import { Octokit } from '@octokit/rest'
import { createHash } from 'crypto'
import type { RemoteFile } from './types'
// ---------------------------------------------------------------------------
// Octokit helpers
// ---------------------------------------------------------------------------
export function getOctokit(): Octokit {
function getOctokit(): Octokit {
const token = process.env.GITHUB_TOKEN
if (!token) throw new Error('GITHUB_TOKEN non configure')
return new Octokit({ auth: token })
}
export function parseRepoUrl(): { owner: string; repo: string } {
function parseRepoUrl(): { owner: string; repo: string } {
const url = process.env.GIT_REPO_URL
if (!url) throw new Error('GIT_REPO_URL non configure')
@@ -26,15 +25,8 @@ export 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 */
export 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')
}
// ---------------------------------------------------------------------------
// Read remote folder contents (with SHA per file)
// Read remote folder contents (with size per file)
// ---------------------------------------------------------------------------
export async function getRemoteFolder(
+135
View File
@@ -0,0 +1,135 @@
// ---------------------------------------------------------------------------
// Nextcloud WebDAV client
// Uses native fetch — no npm package needed.
// ---------------------------------------------------------------------------
function getConfig() {
const url = process.env.NEXTCLOUD_URL
const user = process.env.NEXTCLOUD_USER
const password = process.env.NEXTCLOUD_PASSWORD
const basePath = process.env.NEXTCLOUD_BASE_PATH || 'Models'
if (!url || !user || !password) {
throw new Error('Nextcloud non configure (NEXTCLOUD_URL, NEXTCLOUD_USER, NEXTCLOUD_PASSWORD)')
}
// WebDAV base: https://cloud.example.com/remote.php/dav/files/{user}/
const davBase = `${url.replace(/\/+$/, '')}/remote.php/dav/files/${encodeURIComponent(user)}`
const auth = 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64')
return { davBase, auth, basePath }
}
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`.
*/
export async function mkdirRecursive(path: string): Promise<void> {
const segments = path.replace(/^\/+/, '').replace(/\/+$/, '').split('/')
let current = ''
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)}`)
}
}
}
}
/** 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++) {
const versionPath = `${basePath}/V${i}/${folderName}`
const exists = await folderExists(versionPath)
if (!exists) return `V${i}`
}
}
-15
View File
@@ -20,18 +20,3 @@ export interface RemoteFile {
name: string
size: number
}
export type UploadResponse =
| {
success: true
folderName: string
filesCount: number
compressed: boolean
compressionError?: string
message: string
commitUrl?: string
}
| {
success: false
error: string
}