mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-15 07:05:42 +00:00
Rip out CDN migrations
This commit is contained in:
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
import { FileStorage } from "@spacebar/cdn";
|
||||
import fs from "fs/promises";
|
||||
|
||||
@@ -69,7 +69,8 @@ router.get("/:emoji_id", cache, async (req: Request, res: Response) => {
|
||||
emoji_id = emoji_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${emoji_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${emoji_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -87,35 +88,4 @@ router.delete("/:emoji_id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (file) {
|
||||
if (!(await storage.isFile(newPath))) {
|
||||
console.log(`[CDN] Migrating emoji from subdirectory+fallback to direct path for ${newPath}`);
|
||||
// noinspection SuspiciousTypeOfGuard -- not sure whats up with this
|
||||
if (storage instanceof FileStorage) {
|
||||
const files = await fs.readdir(storage.getFsPath(newPath));
|
||||
if (files.length === 1) {
|
||||
const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`);
|
||||
const newFilePath = storage.getFsPath(newPath);
|
||||
await fs.rename(oldFilePath, newFilePath + ".tmp");
|
||||
await fs.rmdir(storage.getFsPath(newPath));
|
||||
await fs.rename(newFilePath + ".tmp", newFilePath);
|
||||
file = await storage.get(newPath);
|
||||
} else console.log(`[CDN] Warning: not migrating emojis ${newPath}, as there are multiple files in the old directory`);
|
||||
} else {
|
||||
console.log("[CDN] Warning: no migration for s3 storage emojis, as it is not a filesystem");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
import { FileStorage } from "@spacebar/cdn*";
|
||||
import fs from "fs/promises";
|
||||
|
||||
@@ -39,11 +39,11 @@ const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES];
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
const pathPrefix = "stickers";
|
||||
router.post("/:guild_id", multer.single("file"), async (req: Request, res: Response) => {
|
||||
router.post("/:sticker_id", multer.single("file"), async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature");
|
||||
if (!req.file) throw new HTTPError("Missing file");
|
||||
const { buffer, size } = req.file;
|
||||
const { guild_id } = req.params as { [key: string]: string };
|
||||
const { sticker_id } = req.params as { [key: string]: string };
|
||||
|
||||
let hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex");
|
||||
|
||||
@@ -51,7 +51,7 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo
|
||||
if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type");
|
||||
if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
|
||||
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
const path = `${pathPrefix}/${sticker_id}`;
|
||||
const endpoint = Config.get().cdn.endpointPublic;
|
||||
|
||||
await storage.set(path, buffer);
|
||||
@@ -60,16 +60,17 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo
|
||||
id: hash,
|
||||
content_type: type.mime,
|
||||
size,
|
||||
url: `${endpoint}${req.baseUrl}/${guild_id}/${hash}`,
|
||||
url: `${endpoint}${req.baseUrl}/${sticker_id}`,
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
let { guild_id } = req.params as { [key: string]: string };
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
router.get("/:sticker_id", cache, async (req: Request, res: Response) => {
|
||||
let { sticker_id } = req.params as { [key: string]: string };
|
||||
sticker_id = sticker_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${sticker_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -77,61 +78,14 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
export const getAvatar = async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params as { [key: string]: string };
|
||||
let { hash } = req.params as { [key: string]: string };
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
|
||||
return res.send(file);
|
||||
};
|
||||
|
||||
router.get("/:guild_id/:hash", cache, getAvatar);
|
||||
|
||||
router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
router.delete("/:sticker_id/", async (req: Request, res: Response) => {
|
||||
if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature");
|
||||
const { guild_id, id } = req.params as { [key: string]: string };
|
||||
const path = `${pathPrefix}/${guild_id}/${id}`;
|
||||
const { sticker_id } = req.params as { [key: string]: string };
|
||||
const path = `${pathPrefix}/${sticker_id}`;
|
||||
|
||||
await storage.delete(path);
|
||||
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (file) {
|
||||
if (!(await storage.isFile(newPath))) {
|
||||
console.log(`[CDN] Migrating sticker from subdirectory+fallback to direct path for ${newPath}`);
|
||||
// noinspection SuspiciousTypeOfGuard -- not sure whats up with this
|
||||
if (storage instanceof FileStorage) {
|
||||
const files = await fs.readdir(storage.getFsPath(newPath));
|
||||
if (files.length === 1) {
|
||||
const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`);
|
||||
const newFilePath = storage.getFsPath(newPath);
|
||||
await fs.rename(oldFilePath, newFilePath + ".tmp");
|
||||
await fs.rmdir(storage.getFsPath(newPath));
|
||||
await fs.rename(newFilePath + ".tmp", newFilePath);
|
||||
file = await storage.get(newPath);
|
||||
} else console.log(`[CDN] Warning: not migrating sticker ${newPath}, as there are multiple files in the old directory`);
|
||||
} else {
|
||||
console.log("[CDN] Warning: no migration for s3 storage stickers, as it is not a filesystem");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import crypto from "crypto";
|
||||
import { multer } from "../util/multer";
|
||||
import { cache } from "../util/cache";
|
||||
import { cache, cacheNotFound } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
|
||||
guild_id = guild_id.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
const path = `${pathPrefix}/${guild_id}/${hash}`;
|
||||
|
||||
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
|
||||
const file = await storage.get(path);
|
||||
if (!file) return cacheNotFound(req, res);
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
|
||||
return res.send({ success: true });
|
||||
});
|
||||
|
||||
async function getOrMoveFile(newPath: string, oldPath: string) {
|
||||
let file = await storage.get(newPath);
|
||||
if (!file) {
|
||||
if (await storage.exists(oldPath)) {
|
||||
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
|
||||
await storage.move(oldPath, newPath);
|
||||
file = await storage.get(newPath);
|
||||
}
|
||||
}
|
||||
if (!file) throw new HTTPError("not found", 404);
|
||||
return file;
|
||||
}
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -23,3 +23,9 @@ export function cache(req: Request, res: Response, next: NextFunction) {
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`);
|
||||
next();
|
||||
}
|
||||
|
||||
export function cacheNotFound(req: Request, res: Response) {
|
||||
const cacheDuration = 60; // 1 minute
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`);
|
||||
res.status(404).send(req.path + " not found");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user