| 36 | import { logger } from "../logger.js"; |
| 37 | |
| 38 | export class Attachments implements ICollection { |
| 39 | name = "attachments"; |
| 40 | key?: SerializedKey; |
| 41 | readonly collection: SQLCollection<"attachments", Attachment>; |
| 42 | constructor(private readonly db: Database) { |
| 43 | this.collection = new SQLCollection( |
| 44 | db.sql, |
| 45 | db.transaction, |
| 46 | "attachments", |
| 47 | db.eventManager, |
| 48 | db.sanitizer |
| 49 | ); |
| 50 | |
| 51 | db.eventManager.subscribe( |
| 52 | EVENTS.fileDownloaded, |
| 53 | async ({ |
| 54 | success, |
| 55 | filename, |
| 56 | groupId, |
| 57 | eventData |
| 58 | }: { |
| 59 | success: boolean; |
| 60 | filename: string; |
| 61 | groupId: string; |
| 62 | eventData: Record<string, unknown>; |
| 63 | }) => { |
| 64 | if (!success || !eventData || !eventData.readOnDownload) return; |
| 65 | const attachment = await this.attachment(filename); |
| 66 | if (!attachment) return; |
| 67 | |
| 68 | const src = await this.read(filename, getOutputType(attachment)); |
| 69 | if (!src) return; |
| 70 | |
| 71 | this.db.eventManager.publish(EVENTS.mediaAttachmentDownloaded, { |
| 72 | groupId, |
| 73 | hash: attachment.hash, |
| 74 | attachmentType: getAttachmentType(attachment), |
| 75 | src |
| 76 | }); |
| 77 | } |
| 78 | ); |
| 79 | |
| 80 | db.eventManager.subscribe( |
| 81 | EVENTS.fileUploaded, |
| 82 | async ({ |
| 83 | success, |
| 84 | error, |
| 85 | filename |
| 86 | }: { |
| 87 | success: boolean; |
| 88 | filename: string; |
| 89 | error: string; |
| 90 | }) => { |
| 91 | const attachment = await this.attachment(filename); |
| 92 | if (!attachment) return; |
| 93 | if (success) await this.markAsUploaded(attachment.id); |
| 94 | else |
| 95 | await this.markAsFailed( |
nothing calls this directly
no outgoing calls
no test coverage detected