Skip to content

Commit f65f117

Browse files
Rip out CDN migrations
1 parent d126319 commit f65f117

12 files changed

Lines changed: 68 additions & 237 deletions

src/cdn/routes/app-assets.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/app-icons.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/banners.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/channel-icons.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/discover-splashes.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/discovery-splashes.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

src/cdn/routes/emojis.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727
import { FileStorage } from "@spacebar/cdn";
2828
import fs from "fs/promises";
2929

@@ -69,7 +69,8 @@ router.get("/:emoji_id", cache, async (req: Request, res: Response) => {
6969
emoji_id = emoji_id.split(".")[0]; // remove .file extension
7070
const path = `${pathPrefix}/${emoji_id}`;
7171

72-
const file = await getOrMoveFile(path, `avatars/${emoji_id}`);
72+
const file = await storage.get(path);
73+
if (!file) return cacheNotFound(req, res);
7374
const type = await fileTypeFromBuffer(file);
7475

7576
res.set("Content-Type", type?.mime);
@@ -87,35 +88,4 @@ router.delete("/:emoji_id", async (req: Request, res: Response) => {
8788
return res.send({ success: true });
8889
});
8990

90-
async function getOrMoveFile(newPath: string, oldPath: string) {
91-
let file = await storage.get(newPath);
92-
if (file) {
93-
if (!(await storage.isFile(newPath))) {
94-
console.log(`[CDN] Migrating emoji from subdirectory+fallback to direct path for ${newPath}`);
95-
// noinspection SuspiciousTypeOfGuard -- not sure whats up with this
96-
if (storage instanceof FileStorage) {
97-
const files = await fs.readdir(storage.getFsPath(newPath));
98-
if (files.length === 1) {
99-
const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`);
100-
const newFilePath = storage.getFsPath(newPath);
101-
await fs.rename(oldFilePath, newFilePath + ".tmp");
102-
await fs.rmdir(storage.getFsPath(newPath));
103-
await fs.rename(newFilePath + ".tmp", newFilePath);
104-
file = await storage.get(newPath);
105-
} else console.log(`[CDN] Warning: not migrating emojis ${newPath}, as there are multiple files in the old directory`);
106-
} else {
107-
console.log("[CDN] Warning: no migration for s3 storage emojis, as it is not a filesystem");
108-
}
109-
}
110-
} else {
111-
if (await storage.exists(oldPath)) {
112-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
113-
await storage.move(oldPath, newPath);
114-
file = await storage.get(newPath);
115-
}
116-
}
117-
if (!file) throw new HTTPError("not found", 404);
118-
return file;
119-
}
120-
12191
export default router;

src/cdn/routes/icons.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323
import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
26-
import { cache } from "../util/cache";
26+
import { cache, cacheNotFound } from "../util/cache";
2727

2828
// TODO: check premium and animated pfp are allowed in the config
2929
// TODO: generate different sizes of icon
@@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => {
6767
guild_id = guild_id.split(".")[0]; // remove .file extension
6868
const path = `${pathPrefix}/${guild_id}`;
6969

70-
const file = await getOrMoveFile(path, `avatars/${guild_id}`);
70+
const file = await storage.get(path);
71+
if (!file) return cacheNotFound(req, res);
7172
const type = await fileTypeFromBuffer(file);
7273

7374
res.set("Content-Type", type?.mime);
@@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => {
8182
hash = hash.split(".")[0]; // remove .file extension
8283
const path = `${pathPrefix}/${guild_id}/${hash}`;
8384

84-
const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`);
85+
const file = await storage.get(path);
86+
if (!file) return cacheNotFound(req, res);
8587
const type = await fileTypeFromBuffer(file);
8688

8789
res.set("Content-Type", type?.mime);
@@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
101103
return res.send({ success: true });
102104
});
103105

104-
async function getOrMoveFile(newPath: string, oldPath: string) {
105-
let file = await storage.get(newPath);
106-
if (!file) {
107-
if (await storage.exists(oldPath)) {
108-
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109-
await storage.move(oldPath, newPath);
110-
file = await storage.get(newPath);
111-
}
112-
}
113-
if (!file) throw new HTTPError("not found", 404);
114-
return file;
115-
}
116-
117106
export default router;

0 commit comments

Comments
 (0)