blob: a1ce5d9764c42f8337549878004c8d204609669f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import { walk, ensureDirSync, existsSync } from "https://deno.land/std/fs/mod.ts";
import type { Context } from "https://deno.land/x/abc@master/mod.ts";
import { getUserCookies } from "./user.ts";
export const cleanPath = (path: string, uid: number): string => {
return (
"data/" +
uid +
"/" +
path
.replace(/\/files\/?/, "")
.replace("../", "") // TODO: Fix relative ../
.replace("./", "")
.replace(/([^:]\/)\/+/g, "$1")
);
}
export const getFiles = async (c: Context) => {
const path = c.path ? c.path : "";
const uid = getUserCookies(c).uid;
createUserDirectory(uid); // TODO: Consider doing this in db/user/createUser => performance?
const dataPath: string = cleanPath(path, uid);
if (!existsSync(dataPath)) return [];
const files = [];
for await (const entry of walk(dataPath)) {
files.push(entry.path);
}
return files;
}
export const createUserDirectory = (uid: number) => {
ensureDirSync("data/" + uid);
// TODO: Give user access to dir
}
|