46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
|
|
import { Request } from "express";
|
||
|
|
import { WebDAVClient } from "webdav";
|
||
|
|
import { DIRS } from "./server";
|
||
|
|
|
||
|
|
|
||
|
|
export async function statOne(fpath: string, client: WebDAVClient) {
|
||
|
|
const parent = fpath.replace(/\/[^/]+$/, '') || '/',
|
||
|
|
base = fpath.split('/').filter(Boolean).pop(),
|
||
|
|
list = await client.getDirectoryContents(parent, { deep: false }),
|
||
|
|
entry = Array.isArray(list) ? list.find((e: any) => e.basename === base) : undefined;
|
||
|
|
|
||
|
|
if (!entry) throw `not found: ${fpath}`;
|
||
|
|
return { etag: entry.etag as string, size: Number(entry.size), mime: entry.mime as string | undefined };
|
||
|
|
}
|
||
|
|
|
||
|
|
export const toRegExp = (spec: string): RegExp | null => {
|
||
|
|
// slash-delimited form: /pattern/flags
|
||
|
|
const m = spec.match(/^\/(.+)\/([a-z]*)$/i);
|
||
|
|
try {
|
||
|
|
if (m) {
|
||
|
|
const [, source, flags] = m;
|
||
|
|
return new RegExp(source, flags);
|
||
|
|
}
|
||
|
|
|
||
|
|
// raw pattern form: "pattern"
|
||
|
|
return new RegExp(spec);
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function checkIfHasPerms(req: Request) {
|
||
|
|
if (!req.body) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const { fpath, obj_type, deep, usecache } = req.body,
|
||
|
|
o = { fpath, obj_type, deep, usecache };
|
||
|
|
|
||
|
|
if (!fpath) return false;
|
||
|
|
if (!DIRS) return o;
|
||
|
|
if (fpath.includes('..')) return false;
|
||
|
|
|
||
|
|
return DIRS.find(r => fpath.match(r)) ? o : false;
|
||
|
|
}
|
||
|
|
|