Skip to content

Commit d126319

Browse files
Add sticker migration
1 parent ab4a8a3 commit d126319

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

src/cdn/routes/stickers.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { HTTPError } from "lambert-server";
2424
import crypto from "crypto";
2525
import { multer } from "../util/multer";
2626
import { cache } from "../util/cache";
27+
import { FileStorage } from "@spacebar/cdn*";
28+
import fs from "fs/promises";
2729

2830
// TODO: check premium and animated pfp are allowed in the config
2931
// TODO: generate different sizes of icon
@@ -103,7 +105,25 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
103105

104106
async function getOrMoveFile(newPath: string, oldPath: string) {
105107
let file = await storage.get(newPath);
106-
if (!file) {
108+
if (file) {
109+
if (!(await storage.isFile(newPath))) {
110+
console.log(`[CDN] Migrating sticker from subdirectory+fallback to direct path for ${newPath}`);
111+
// noinspection SuspiciousTypeOfGuard -- not sure whats up with this
112+
if (storage instanceof FileStorage) {
113+
const files = await fs.readdir(storage.getFsPath(newPath));
114+
if (files.length === 1) {
115+
const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`);
116+
const newFilePath = storage.getFsPath(newPath);
117+
await fs.rename(oldFilePath, newFilePath + ".tmp");
118+
await fs.rmdir(storage.getFsPath(newPath));
119+
await fs.rename(newFilePath + ".tmp", newFilePath);
120+
file = await storage.get(newPath);
121+
} else console.log(`[CDN] Warning: not migrating sticker ${newPath}, as there are multiple files in the old directory`);
122+
} else {
123+
console.log("[CDN] Warning: no migration for s3 storage stickers, as it is not a filesystem");
124+
}
125+
}
126+
} else {
107127
if (await storage.exists(oldPath)) {
108128
console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`);
109129
await storage.move(oldPath, newPath);

0 commit comments

Comments
 (0)