From bb69cf8c45a58ec18cd16b7951a9de597bc90663 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 15 Apr 2026 22:10:14 +0200 Subject: [PATCH] Fix attachments 2 --- src/cdn/Server.ts | 24 +++++++++++++++++++++++- src/cdn/routes/attachments.ts | 14 +------------- src/webrtc/Server.ts | 3 ++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/cdn/Server.ts b/src/cdn/Server.ts index 423235567..198f6bdd8 100644 --- a/src/cdn/Server.ts +++ b/src/cdn/Server.ts @@ -17,11 +17,14 @@ */ import { Server, ServerOptions } from "lambert-server"; -import { Config, initDatabase, registerRoutes } from "@spacebar/util"; +import { Attachment, Config, initDatabase, registerRoutes } from "@spacebar/util"; import { CORS, BodyParser } from "@spacebar/api"; import path from "path"; import guildProfilesRoute from "./routes/guild-profiles"; import morgan from "morgan"; +import { loadWebRtcLibrary, mediaServer, WRTC_PORT_MAX, WRTC_PORT_MIN, WRTC_PUBLIC_IP } from "@spacebar/webrtc*"; +import { green, yellow } from "picocolors"; +import { storage } from "./util"; export type CDNServerOptions = ServerOptions; @@ -36,6 +39,11 @@ export class CDNServer extends Server { await initDatabase(); await Config.init(); + this.migrateAttachments().then( + (_) => console.log("[CDN] Successfully migrated attachments"), + (_) => console.log("[CDN] Attachment migration failed"), + ); + const logRequests = process.env["LOG_REQUESTS"] != undefined; if (logRequests) { this.app.use( @@ -68,6 +76,20 @@ export class CDNServer extends Server { return super.start(); } + async migrateAttachments() { + if (await storage.exists(".mig_complete.attachments1")) return; + for await (const attachment of await Attachment.createQueryBuilder("attachments").where("message_id is not null").select().stream()) { + const oldPath = `attachments/${attachment.attachments_channel_id}/${attachment.attachments_id}/${attachment.attachments_filename}`; + const newPath = `attachments/${attachment.attachments_channel_id}/${attachment.attachments_message_id}/${attachment.attachments_filename}`; + if (!(await storage.exists(oldPath))) { + console.log(`[CDN/Attachments] Attachment migration: could not find old path, skipping migration: ` + oldPath); + continue; + } + await storage.move(oldPath, newPath); + } + await storage.set(".mig_complete.attachments1", Buffer.from([1])); + } + async stop() { return super.stop(); } diff --git a/src/cdn/routes/attachments.ts b/src/cdn/routes/attachments.ts index d17ac43c2..ab5028a83 100644 --- a/src/cdn/routes/attachments.ts +++ b/src/cdn/routes/attachments.ts @@ -98,19 +98,7 @@ router.get("/:channel_id/:message_id/:filename", cache, async (req: Request, res if (!hasValidAuth) return res.status(404).send("This content is no longer available."); - let file = await storage.get(path); - // handle re-keying paths to be correct - if (!file) { - const att = await Attachment.findOne({ where: { id: message_id, channel_id: channel_id } }); - if (att) { - const oldPath = `attachments/${channel_id}/${att.id}/${filename}`; - if (await storage.exists(oldPath)) { - console.log(`[CDN/Attachments] Moving ${oldPath} -> ${path}!`); - await storage.move(oldPath, path); - file = await storage.get(path); - } - } - } + const file = await storage.get(path); if (!file) throw new HTTPError("File not found"); const type = await fileTypeFromBuffer(file); let content_type = type?.mime || "application/octet-stream"; diff --git a/src/webrtc/Server.ts b/src/webrtc/Server.ts index ff98fe57e..f3e9043b9 100644 --- a/src/webrtc/Server.ts +++ b/src/webrtc/Server.ts @@ -17,12 +17,13 @@ */ import dotenv from "dotenv"; dotenv.config({ quiet: true }); -import { closeDatabase, Config, initDatabase, initEvent } from "@spacebar/util"; +import { closeDatabase, Config, initDatabase, initEvent, Session, TimeSpan } from "@spacebar/util"; import http from "http"; import ws from "ws"; import { Connection } from "./events/Connection"; import { loadWebRtcLibrary, mediaServer, WRTC_PORT_MAX, WRTC_PORT_MIN, WRTC_PUBLIC_IP } from "./util"; import { green, yellow } from "picocolors"; +import { storage } from "@spacebar/cdn*"; export class Server { public ws: ws.Server;