import { basename, dirname, join, relative, resolve } from "node:path"; import { cfg } from "../main.ts"; type WebDavOptions = Record; type WebDavConfig = { baseUrl: string; username?: string; password?: string; remoteDir: string; }; async function readDotEnv(): Promise> { const result: Record = {}; try { for (const line of (await Deno.readTextFile(".env")).split(/\r?\n/)) { const match = line.match( /^\s*(?:export\s+)?([A-Za-z_][\w]*)\s*=\s*(.*)\s*$/, ); if (!match) continue; result[match[1]] = match[2].replace(/^(["'])(.*)\1$/, "$2"); } } catch (error) { if (!(error instanceof Deno.errors.NotFound)) throw error; } return result; } async function webDavConfig(options: WebDavOptions): Promise { const env = await readDotEnv(); const envValue = (...keys: string[]) => { for (const key of keys) { const value = env[key] ?? Deno.env.get(key); if (value) return value; } return undefined; }; const baseUrl = String( options.url ?? envValue("WEBDAV_URL", "WEBDAV_BASE_URL") ?? "", ); if (!baseUrl) { throw new Error("WebDAV URL required: --url or WEBDAV_URL in .env"); } const parsedBaseUrl = new URL(baseUrl); if (!/^https?:$/.test(parsedBaseUrl.protocol)) { throw new Error("WebDAV URL must use HTTP or HTTPS"); } const remoteDir = String( options["remote-dir"] ?? envValue("WEBDAV_REMOTE_DIR") ?? "gp_upload", ).replace(/^\/+|\/+$/g, ""); if (remoteDir.split("/").some((part) => part === "." || part === "..")) { throw new Error("WebDAV remote directory cannot contain dot segments"); } return { baseUrl: baseUrl.replace(/\/+$/, ""), username: String( options.user ?? envValue("WEBDAV_USERNAME", "WEBDAV_USER") ?? "", ) || undefined, password: String(options.password ?? envValue("WEBDAV_PASSWORD") ?? "") || undefined, remoteDir, }; } function authHeaders(config: WebDavConfig): HeadersInit { if (!config.username && !config.password) return {}; const token = btoa(`${config.username ?? ""}:${config.password ?? ""}`); return { authorization: `Basic ${token}` }; } function remoteUrl(config: WebDavConfig, path: string): string { const base = new URL(`${config.baseUrl}/`); const suffix = [config.remoteDir, path].filter(Boolean).join("/"); return new URL(suffix.split("/").map(encodeURIComponent).join("/"), base) .href; } async function ensureCollection( config: WebDavConfig, path: string, ): Promise { const url = remoteUrl(config, path); const response = await fetch(url, { method: "MKCOL", headers: authHeaders(config), }); if ( response.ok || response.status === 405 || response.status === 301 || response.status === 302 ) return; if (response.status === 409) { const parent = dirname(path); if (parent !== "." && parent !== path) { await ensureCollection(config, parent); } const retry = await fetch(url, { method: "MKCOL", headers: authHeaders(config), }); if (retry.ok || retry.status === 405) return; throw new Error(`WebDAV MKCOL ${path} failed: HTTP ${retry.status}`); } throw new Error(`WebDAV MKCOL ${path} failed: HTTP ${response.status}`); } async function walkFiles(root: string): Promise { const files: string[] = []; for (const entry of Deno.readDirSync(root)) { const path = join(root, entry.name); if (entry.isDirectory) files.push(...await walkFiles(path)); else if (entry.isFile) files.push(path); } return files; } function roots( options: WebDavOptions, ): Array<{ local: string; remote: string }> { const type = String(options.type ?? "all"); if (!new Set(["all", "assets", "cache"]).has(type)) { throw new Error("--type must be all, cache, or assets"); } const injectedRoots = options.localRoots; if (Array.isArray(injectedRoots)) { return injectedRoots.filter(( root, ): root is { local: string; remote: string } => root && typeof root.local === "string" && typeof root.remote === "string" && ((type === "all" && (root.remote === "assets" || root.remote.startsWith("cache/"))) || (type === "assets" && root.remote === "assets") || (type === "cache" && root.remote.startsWith("cache/"))) ); } const roots: Array<{ local: string; remote: string }> = []; if (type === "all" || type === "assets") { roots.push({ local: resolve("../../assets"), remote: "assets" }); } if (type === "all" || type === "cache") { for (const setting of cfg.settings) { const publication = resolve(setting.journal.from_directory); roots.push({ local: publication, remote: `cache/${basename(publication)}`, }); } } return roots; } export async function upload_webdav(options: WebDavOptions): Promise { const config = await webDavConfig(options); await ensureCollection(config, ""); for (const root of roots(options)) { try { await Deno.stat(root.local); } catch (error) { if (error instanceof Deno.errors.NotFound) continue; throw error; } await ensureCollection(config, root.remote); for (const file of await walkFiles(root.local)) { const path = relative(root.local, file).split("\\").join("/"); if ( root.remote.startsWith("cache/") && path !== ".cache.toml" && !path.startsWith(".local-cache/") ) continue; const parent = dirname(path); if (parent !== ".") { await ensureCollection(config, `${root.remote}/${parent}`); } const response = await fetch( remoteUrl(config, `${root.remote}/${path}`), { method: "PUT", headers: { ...authHeaders(config), "content-type": "application/octet-stream", }, body: await Deno.readFile(file), }, ); if (!response.ok) { throw new Error(`WebDAV PUT ${path} failed: HTTP ${response.status}`); } console.log(`Uploaded ${file}`); } } } export async function sync_webdav(options: WebDavOptions): Promise { const config = await webDavConfig(options); for (const root of roots(options)) { await Deno.mkdir(root.local, { recursive: true }); await downloadCollection(config, root.remote, root.local); } } async function downloadCollection( config: WebDavConfig, remotePath: string, localPath: string, ): Promise { const response = await fetch(remoteUrl(config, remotePath), { method: "PROPFIND", headers: { ...authHeaders(config), Depth: "1" }, }); if (response.status === 404) return; if (!response.ok) { throw new Error( `WebDAV PROPFIND ${remotePath} failed: HTTP ${response.status}`, ); } const xml = await response.text(); const requestUrl = new URL(remoteUrl(config, remotePath)); const basePath = requestUrl.pathname.replace(/\/+$/, ""); const responses = [ ...xml.matchAll(/<[^>]*response\b[^>]*>([\s\S]*?)<\/[^>]*response>/gi), ]; for (const [, responseXml] of responses) { const hrefMatch = responseXml.match(/<[^>]*href[^>]*>(.*?)<\/[^>]*href>/i); if (!hrefMatch) continue; const href = decodeXmlText(hrefMatch[1].trim()); let parsed: URL; try { parsed = new URL(href, config.baseUrl); } catch { continue; } if (parsed.origin !== requestUrl.origin) continue; const entry = parsed.pathname.replace(/\/+$/, ""); if (entry === basePath || !entry.startsWith(`${basePath}/`)) continue; const relativePath = entry.slice(basePath.length + 1); let name: string; try { name = decodeURIComponent(relativePath); } catch { continue; } if (name.includes("/")) continue; if (!name || name === "." || name === ".." || name.includes("\\")) { continue; } const isDirectory = /<[^>]*collection\b/i.test(responseXml); if (isDirectory) { await Deno.mkdir(join(localPath, name), { recursive: true }); await downloadCollection( config, `${remotePath}/${name}`, join(localPath, name), ); } else { const fileResponse = await fetch(new URL(href, config.baseUrl), { headers: authHeaders(config), }); if (!fileResponse.ok) { throw new Error( `WebDAV GET ${name} failed: HTTP ${fileResponse.status}`, ); } await Deno.writeFile( join(localPath, name), new Uint8Array(await fileResponse.arrayBuffer()), ); console.log(`Downloaded ${join(localPath, name)}`); } } } function decodeXmlText(text: string): string { return text.replace(/&(?:amp|lt|gt|quot|apos);/g, (entity) => ({ "&": "&", "<": "<", ">": ">", """: '"', "'": "'", }[entity] ?? entity)); } export async function clean_local_files(options: WebDavOptions): Promise { const type = String(options.type ?? "all"); if (!new Set(["all", "cache", "assets"]).has(type)) { throw new Error("clean --type must be all, cache, or assets"); } const targets: string[] = []; if (type === "all" || type === "assets") { targets.push(resolve("../../assets")); } if (type === "all" || type === "cache") { for (const setting of cfg.settings) { targets.push( join(resolve(setting.journal.from_directory), ".local-cache"), ); targets.push( join(resolve(setting.journal.from_directory), ".cache.toml"), ); } } for (const target of new Set(targets)) { try { await Deno.remove(target, { recursive: true }); console.log(`Removed ${target}`); } catch (error) { if (!(error instanceof Deno.errors.NotFound)) throw error; } } }