Store attachment data in db

This commit is contained in:
Rory&
2026-08-23 17:05:10 +02:00
parent a491279b67
commit 95d3004b60
5 changed files with 61 additions and 5 deletions
+30 -3
View File
@@ -19,7 +19,7 @@
import { BeforeRemove, Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
import { Config, deleteFile } from "@spacebar/util/util";
import { getUrlSignature, NewUrlUserSignatureData, NewUrlSignatureData } from "@spacebar/util/Signing";
import type { PublicAttachment } from "@spacebar/schemas/api/messages/Attachments";
import { AttachmentFlags, PublicAttachment } from "@spacebar/schemas/api/messages/Attachments";
import { BaseClass } from "./BaseClass";
@Entity({
@@ -61,19 +61,46 @@ export class Attachment extends BaseClass {
})
channel: import("./Channel").Channel;
@Column({ nullable: true, type: "character varying" })
description?: string;
@Column({ nullable: true, type: "int2" })
flags?: AttachmentFlags;
@Column({ nullable: true, type: "int2" })
content_scan_version?: number;
@Column({ nullable: true, type: "int2" })
placeholder_version?: number;
@Column({ nullable: true, type: "character varying" })
placeholder?: string;
@Column({ nullable: true, type: "float8" })
duration_secs?: number;
@Column({ nullable: true, type: "character varying" })
waveform?: string;
@Column({ nullable: true, type: "character varying" })
title?: string;
@Column({ nullable: true, type: "timestamp with time zone" })
clip_created_at?: Date;
@BeforeRemove()
onDelete() {
return deleteFile(new URL(this.toJSON().url).pathname);
}
toJSON() {
toJSON(): PublicAttachment {
const channelId = this.channel_id ?? this.channel?.id ?? this.message?.channel_id;
const messageId = this.message_id ?? this.message?.id;
return {
...this,
url: `${Config.get().cdn.endpointPublic}/attachments/${channelId}/${messageId}/${this.filename}`,
proxy_url: `${Config.get().cdn.endpointPublic}/attachments/${channelId}/${messageId}/${this.filename}`,
};
} satisfies PublicAttachment;
}
signUrls(data: NewUrlUserSignatureData): PublicAttachment {
const att = Attachment.prototype.toJSON.apply(this);
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AttachmentMetadata1787497429170 implements MigrationInterface {
name = "AttachmentMetadata1787497429170";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "attachments" ADD "description" character varying`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "flags" smallint`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "content_scan_version" smallint`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "placeholder_version" smallint`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "placeholder" character varying`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "duration_secs" double precision`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "waveform" character varying`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "title" character varying`);
await queryRunner.query(`ALTER TABLE "attachments" ADD "clip_created_at" TIMESTAMP WITH TIME ZONE`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "clip_created_at"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "title"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "waveform"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "duration_secs"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "placeholder"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "placeholder_version"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "content_scan_version"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "flags"`);
await queryRunner.query(`ALTER TABLE "attachments" DROP COLUMN "description"`);
}
}