1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

🐛 fix file storage dir

This commit is contained in:
Flam3rboy 2021-08-07 18:52:22 +02:00
parent 007e6fef78
commit b4e409f5c6
2 changed files with 16 additions and 15 deletions

View File

@ -7,10 +7,9 @@ import { multer } from "../util/multer";
import crypto from "crypto"; import crypto from "crypto";
// TODO: check premium and animated pfp are allowed in the config // TODO: check premium and animated pfp are allowed in the config
// TODO: generate different sizes of avatar // TODO: generate different sizes of icon
// TODO: generate different image types of avatar // TODO: generate different image types of icon
// TODO: delete old avatars // TODO: delete old icons
// TODO: check request signature for modify methods
const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"]; const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"];
const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/svg+xml", "image/svg"]; const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/svg+xml", "image/svg"];
@ -25,27 +24,29 @@ router.post("/:user_id", multer.single("file"), async (req: Request, res: Respon
const { buffer, mimetype, size, originalname, fieldname } = req.file; const { buffer, mimetype, size, originalname, fieldname } = req.file;
const { user_id } = req.params; const { user_id } = req.params;
const id = crypto.createHash("md5").update(Snowflake.generate()).digest("hex"); var hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex");
const type = await FileType.fromBuffer(buffer); const type = await FileType.fromBuffer(buffer);
if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type"); if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type");
const path = `avatars/${user_id}/${id}`; if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
const path = `avatars/${user_id}/${hash}`;
const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
await storage.set(path, buffer); await storage.set(path, buffer);
return res.json({ return res.json({
id, hash,
content_type: type.mime, content_type: type.mime,
size, size,
url: `${endpoint}${req.baseUrl}/${user_id}/${id}`, url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
}); });
}); });
router.get("/:user_id/:id", async (req: Request, res: Response) => { router.get("/:user_id/:hash", async (req: Request, res: Response) => {
var { user_id, id } = req.params; var { user_id, hash } = req.params;
id = id.split(".")[0]; hash = hash.split(".")[0]; // remove .file extension
const path = `avatars/${user_id}/${id}`; const path = `avatars/${user_id}/${hash}`;
const file = await storage.get(path); const file = await storage.get(path);
if (!file) throw new HTTPError("not found", 404); if (!file) throw new HTTPError("not found", 404);

View File

@ -1,6 +1,7 @@
import { Storage } from "./Storage"; import { Storage } from "./Storage";
import fs from "fs"; import fs from "fs";
import { join, relative } from "path"; import fse from "fs-extra";
import { join, relative, dirname } from "path";
import "missing-native-js-functions"; import "missing-native-js-functions";
function getPath(path: string) { function getPath(path: string) {
@ -23,8 +24,7 @@ export class FileStorage implements Storage {
async set(path: string, value: any) { async set(path: string, value: any) {
path = getPath(path); path = getPath(path);
const dir = path.split("/").slice(0, -1).join("/"); fse.ensureDirSync(dirname(path));
fs.mkdirSync(dir, { recursive: true });
return fs.writeFileSync(path, value, { encoding: "binary" }); return fs.writeFileSync(path, value, { encoding: "binary" });
} }