Files
upload-gltf/lib/nextcloud.ts
T
2026-04-27 23:43:16 +02:00

111 lines
3.3 KiB
TypeScript

const MAX_VERSIONS = 1000
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)')
}
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}`
}
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 == null ? undefined : typeof body === 'string' ? body : new Uint8Array(body),
})
return res
}
async function folderExists(path: string): Promise<boolean> {
const res = await davRequest('PROPFIND', path + '/', null, { Depth: '0' })
if (res.status === 404) return false
if (res.status >= 200 && res.status < 300) return true
const text = await res.text().catch(() => '')
throw new Error(`PROPFIND ${path} failed (${res.status}): ${text.slice(0, 200)}`)
}
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) {
const text = await res.text().catch(() => '')
throw new Error(`MKCOL ${current} failed (${res.status}): ${text.slice(0, 200)}`)
}
}
}
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)}`)
}
}
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)}`)
}
}
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})`)
}