chore: merge master into cdn-s3

This commit is contained in:
Hayden Young
2021-10-15 09:42:31 +00:00
committed by GitHub
91 changed files with 19959 additions and 16844 deletions
+10 -4
View File
@@ -1,5 +1,5 @@
import { Server, ServerOptions } from "lambert-server";
import { Config, initDatabase } from "@fosscord/util";
import { Config, initDatabase, registerRoutes } from "@fosscord/util";
import path from "path";
import avatarsRoute from "./routes/avatars";
import bodyParser from "body-parser";
@@ -23,13 +23,19 @@ export class CDNServer extends Server {
"Content-security-policy",
"default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';"
);
res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*");
res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*");
res.set(
"Access-Control-Allow-Headers",
req.header("Access-Control-Request-Headers") || "*"
);
res.set(
"Access-Control-Allow-Methods",
req.header("Access-Control-Request-Methods") || "*"
);
next();
});
this.app.use(bodyParser.json({ inflate: true, limit: "10mb" }));
await this.registerRoutes(path.join(__dirname, "routes/"));
await registerRoutes(this, path.join(__dirname, "routes/"));
this.app.use("/icons/", avatarsRoute);
this.log("verbose", "[Server] Route /icons registered");
+15
View File
@@ -58,6 +58,21 @@ router.post(
}
);
router.get("/:user_id", async (req: Request, res: Response) => {
var { user_id } = req.params;
user_id = user_id.split(".")[0]; // remove .file extension
const path = `avatars/${user_id}`;
const file = await storage.get(path);
if (!file) throw new HTTPError("not found", 404);
const type = await FileType.fromBuffer(file);
res.set("Content-Type", type?.mime);
res.set("Cache-Control", "public, max-age=31536000");
return res.send(file);
});
router.get("/:user_id/:hash", async (req: Request, res: Response) => {
var { user_id, hash } = req.params;
hash = hash.split(".")[0]; // remove .file extension
+11 -3
View File
@@ -13,16 +13,24 @@ function getPath(path: string) {
const root = process.env.STORAGE_LOCATION || "../";
var filename = join(root, path);
if (path.indexOf("\0") !== -1 || !filename.startsWith(root)) throw new Error("invalid path");
if (path.indexOf("\0") !== -1 || !filename.startsWith(root))
throw new Error("invalid path");
return filename;
}
export class FileStorage implements Storage {
async get(path: string): Promise<Buffer | null> {
path = getPath(path);
try {
return fs.readFileSync(getPath(path));
return fs.readFileSync(path);
} catch (error) {
return null;
try {
const files = fs.readdirSync(path);
if (!files.length) return null;
return fs.readFileSync(join(path, files[0]));
} catch (error) {
return null;
}
}
}