This commit is contained in:
Nobody committed 2026-11-12 18:34:33 +08:00
1 parent 5977b1b272
commit 09af3bd8c0
18 files changed
+57 -30

No files matched your search

+307
View File
@@ -0,0 +1,307 @@
import { basename, dirname, join, relative, resolve } from "node:path";
import { cfg } from "../main.ts";
type WebDavOptions = Record<string, unknown>;
type WebDavConfig = {
baseUrl: string;
username?: string;
password?: string;
remoteDir: string;
};
async function readDotEnv(): Promise<Record<string, string>> {
const result: Record<string, string> = {};
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<WebDavConfig> {
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<void> {
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<string[]> {
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<void> {
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<void> {
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<void> {
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) => ({
"&amp;": "&",
"&lt;": "<",
"&gt;": ">",
"&quot;": '"',
"&apos;": "'",
}[entity] ?? entity));
}
export async function clean_local_files(options: WebDavOptions): Promise<void> {
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;
}
}
}
+149
View File
@@ -0,0 +1,149 @@
import { assert, assertEquals, assertRejects } from "@std/assert";
import { clean_local_files, sync_webdav, upload_webdav } from "./webdav.ts";
function propfindResponse(
entries: Array<{
href: string;
collection?: boolean;
}>,
): Response {
const responses = entries.map(({ href, collection }) =>
`<d:response><d:href>${href}</d:href><d:propstat><d:prop>$
${collection ? "<d:resourcetype><d:collection/></d:resourcetype>" : ""}
</d:prop></d:propstat></d:response>`
).join("");
return new Response(
`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">${responses}</d:multistatus>`,
{ status: 207 },
);
}
Deno.test("WebDAV sync maps assets and cache into their local roots", async () => {
const base = "https://dav.example.test/remote.php/dav/files/user";
const root = await Deno.makeTempDir();
const assetsRoot = `${root}/assets`;
const cacheRoot = `${root}/publication`;
const seen: string[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const url = String(input);
const method = (init as RequestInit | undefined)?.method ?? "GET";
seen.push(`${method} ${url}`);
if (method === "PROPFIND" && url.endsWith("/gp_upload/assets")) {
return propfindResponse([
{ href: `${base}/gp_upload/assets/` },
{ href: `${base}/gp_upload/assets/pictures/`, collection: true },
{
href:
"https://evil.example.test/remote.php/dav/files/user/gp_upload/assets/outside.png",
},
]);
}
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache/26.11.0")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/26.11.0/` },
{ href: `${base}/gp_upload/cache/26.11.0/.cache.toml` },
{
href: `${base}/gp_upload/cache/26.11.0/.local-cache/`,
collection: true,
},
]);
}
if (method === "PROPFIND" && url.endsWith("/gp_upload/cache")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/` },
{ href: `${base}/gp_upload/cache/26.11.0/`, collection: true },
]);
}
if (method === "PROPFIND" && url.endsWith("/assets/pictures")) {
return propfindResponse([
{ href: `${base}/gp_upload/assets/pictures/` },
{ href: `${base}/gp_upload/assets/pictures/rsf.png` },
]);
}
if (method === "PROPFIND" && url.endsWith("/.local-cache")) {
return propfindResponse([
{ href: `${base}/gp_upload/cache/26.11.0/.local-cache/` },
{
href: `${base}/gp_upload/cache/26.11.0/.local-cache/a.png`,
},
]);
}
if (method === "GET") return new Response("ok");
throw new Error(`Unexpected WebDAV request: ${method} ${url}`);
};
try {
await sync_webdav({
url: base,
"remote-dir": "gp_upload",
type: "all",
localRoots: [
{ local: assetsRoot, remote: "assets" },
{ local: cacheRoot, remote: "cache/26.11.0" },
],
});
assertEquals(
await Deno.readTextFile(`${assetsRoot}/pictures/rsf.png`),
"ok",
);
assertEquals(await Deno.readTextFile(`${cacheRoot}/.cache.toml`), "ok");
assertEquals(
await Deno.readTextFile(`${cacheRoot}/.local-cache/a.png`),
"ok",
);
assertEquals(
seen.some((request) => request.includes("outside.png")),
false,
);
} finally {
globalThis.fetch = originalFetch;
await Deno.remove(root, { recursive: true });
}
});
Deno.test("WebDAV upload creates remote collections and uploads only cache files", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
await Deno.mkdir(`${publication}/.local-cache`, { recursive: true });
await Deno.writeTextFile(`${publication}/.cache.toml`, '"a" = "b"');
await Deno.writeTextFile(`${publication}/.local-cache/a.png`, "data");
await Deno.writeTextFile(`${publication}/article.md`, "must not upload");
const methods: string[] = [];
const uploads: string[] = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
const method = (init as RequestInit | undefined)?.method ?? "GET";
methods.push(method);
if (method === "PUT") uploads.push(String(input));
return new Response(null, { status: 201 });
};
try {
await upload_webdav({
url: "https://dav.example.test/root",
type: "cache",
localRoots: [
{ local: publication, remote: "cache/test-publication" },
],
});
assert(methods.includes("MKCOL"));
assert(methods.includes("PUT"));
assertEquals(uploads.length, 2);
assertEquals(uploads.some((url) => url.endsWith("article.md")), false);
} finally {
globalThis.fetch = originalFetch;
await Deno.remove(root, { recursive: true });
}
});
Deno.test("WebDAV commands reject invalid types and unsafe remote roots", async () => {
await assertRejects(
() => sync_webdav({ url: "https://dav.example.test", type: "other" }),
Error,
"--type must be all, cache, or assets",
);
await assertRejects(
() => clean_local_files({ type: "invalid" }),
Error,
"clean --type must be all, cache, or assets",
);
});