Something went wrong. Try again.
Self-hosted file management and sharing system, supports multiple storage providers. forked from tdvorak.tngl.sh/cloudreve
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import { CrUri } from "@cloudreve/sdk/files";import { CliError } from "./output/errors.js";
export type Operand = | { kind: "local"; path: string } | { kind: "root"; path: "/" | "/share/" } | { kind: "remote"; uri: CrUri; trailingSlash: boolean };
export const roots = [ { path: "/my/", context: "personal" }, { path: "/trash/", context: "trash" }, { path: "/shared_with_me/", context: "received shares" }, { path: "/share/", context: "requires share ID" },];
function invalid(): never { throw new CliError("path", "Invalid remote path or traversal above namespace root");}
export function resolveOperand(input: string, cwd = "/my/"): Operand { if (input.includes("\0")) { invalid(); }
if (input.startsWith("local:")) { const path = input.slice(6);
if (!path) { invalid(); }
return { kind: "local", path }; }
if (input.startsWith("cloudreve://")) { try { const rawPath = input .slice("cloudreve://".length) .replace(/^[^/]*/, "") .split(/[?#]/)[0] ?? "";
let depth = 0;
for (const raw of rawPath.split("/")) { const segment = decodeURIComponent(raw);
if (segment === "..") { if (depth === 0) { invalid(); }
depth--; } else if (segment && segment !== ".") { depth++; } }
if (/^[^/]*:[^/]*@/.test(input.slice("cloudreve://".length))) { invalid(); }
const url = new URL(input); const uri = new CrUri(input);
if ( url.password || url.port || url.hash || !["my", "trash", "shared_with_me", "share"].includes(uri.fs()) || (uri.fs() !== "share" && uri.id()) || (uri.fs() === "share" && !uri.id()) || /%2f|%5c/i.test(url.pathname) ) { invalid(); }
uri.elements();
if (uri.elements().some((p) => p.includes("\0"))) { invalid(); }
return { kind: "remote", uri, trailingSlash: input.endsWith("/") }; } catch { return invalid(); } }
if (!cwd.startsWith("/") || cwd.startsWith("//")) { invalid(); }
const base = input.startsWith("/") ? input : `${cwd.replace(/\/$/, "")}/${input}`; const raw = base.split("/").filter((p) => p !== "" && p !== "."); const namespace = raw.shift();
if (namespace === undefined) { return { kind: "root", path: "/" }; }
if (!["my", "trash", "shared_with_me", "share"].includes(namespace)) { invalid(); }
const id = namespace === "share" ? raw.shift() : undefined;
if (namespace === "share" && !id) { return { kind: "root", path: "/share/" }; }
if (id === "..") { invalid(); }
const parts: string[] = [];
for (const part of raw) { if (part === "..") { if (!parts.length) { invalid(); }
parts.pop(); } else { parts.push(part); } }
const uri = new CrUri(`cloudreve://${id ? `${encodeURIComponent(id)}@` : ""}${namespace}/`).join( ...parts, );
return { kind: "remote", uri, trailingSlash: input.endsWith("/") };}
export function remote(input: string, cwd = "/my/", mutation = false): CrUri { const operand = resolveOperand(input, cwd);
if (operand.kind !== "remote") { throw new CliError("path", "This command requires a remote path inside a namespace"); }
if (mutation && (operand.uri.isRoot() || operand.uri.isSearch() || operand.uri.category())) { throw new CliError("path", "Namespace roots and query views cannot be mutation targets"); }
return operand.uri;}
export function displayPath(input: string): string | undefined { try { if (!input.startsWith("cloudreve://")) { return undefined; }
const operand = resolveOperand(input);
if (operand.kind !== "remote") { return undefined; }
const u = operand.uri;
return `/${u.fs()}${u.fs() === "share" ? `/${u.id()}` : ""}${u.path()}`; } catch { return undefined; }}