mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-23 19:02:37 +01:00
🐛 fix cdn route not working without hash
This commit is contained in:
parent
33103cc452
commit
5985917715
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user