mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-14 18:25:16 +00:00
Fix attachments 2
This commit is contained in:
+23
-1
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user