diff --git a/attachments.go b/attachments.go index 7c34768..4cafcc6 100644 --- a/attachments.go +++ b/attachments.go @@ -76,8 +76,9 @@ func (br *DiscordBridge) uploadMatrixAttachment(intent *appservice.IntentAPI, da dbFile.URL = url dbFile.ID = attachmentID dbFile.Size = len(data) + dbFile.MimeType = mimetype.Detect(data).String() if mime == "" { - mime = mimetype.Detect(data).String() + mime = dbFile.MimeType } if strings.HasPrefix(mime, "image/") { cfg, _, _ := image.DecodeConfig(bytes.NewReader(data)) diff --git a/database/file.go b/database/file.go index 8f7c403..5691c5e 100644 --- a/database/file.go +++ b/database/file.go @@ -20,10 +20,10 @@ type FileQuery struct { // language=postgresql const ( - fileSelect = "SELECT url, encrypted, id, mxc, size, width, height, decryption_info, timestamp FROM discord_file" + fileSelect = "SELECT url, encrypted, id, mxc, size, width, height, mime_type, decryption_info, timestamp FROM discord_file" fileInsert = ` - INSERT INTO discord_file (url, encrypted, id, mxc, size, width, height, decryption_info, timestamp) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + INSERT INTO discord_file (url, encrypted, id, mxc, size, width, height, mime_type, decryption_info, timestamp) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) ` ) @@ -49,9 +49,10 @@ type File struct { ID string MXC id.ContentURI - Size int - Width int - Height int + Size int + Width int + Height int + MimeType string DecryptionInfo *attachment.EncryptedFile @@ -64,7 +65,7 @@ func (f *File) Scan(row dbutil.Scannable) *File { var width, height sql.NullInt32 var timestamp int64 var mxc string - err := row.Scan(&f.URL, &f.Encrypted, &fileID, &mxc, &f.Size, &width, &height, &decryptionInfo, ×tamp) + err := row.Scan(&f.URL, &f.Encrypted, &fileID, &mxc, &f.Size, &width, &height, &f.MimeType, &decryptionInfo, ×tamp) if err != nil { if !errors.Is(err, sql.ErrNoRows) { f.log.Errorln("Database scan failed:", err) @@ -114,7 +115,7 @@ func (f *File) Insert(txn dbutil.Execable) { } _, err = txn.Exec(fileInsert, f.URL, f.Encrypted, strPtr(f.ID), f.MXC.String(), f.Size, - positiveIntToNullInt32(f.Width), positiveIntToNullInt32(f.Height), + positiveIntToNullInt32(f.Width), positiveIntToNullInt32(f.Height), f.MimeType, decryptionInfo, f.Timestamp.UnixMilli(), ) if err != nil { diff --git a/database/upgrades/00-latest-revision.sql b/database/upgrades/00-latest-revision.sql index 8425584..a04643a 100644 --- a/database/upgrades/00-latest-revision.sql +++ b/database/upgrades/00-latest-revision.sql @@ -1,4 +1,4 @@ --- v0 -> v11: Latest revision +-- v0 -> v12: Latest revision CREATE TABLE guild ( dcid TEXT PRIMARY KEY, @@ -158,9 +158,10 @@ CREATE TABLE discord_file ( id TEXT, mxc TEXT NOT NULL, - size BIGINT NOT NULL, - width INTEGER, - height INTEGER, + size BIGINT NOT NULL, + width INTEGER, + height INTEGER, + mime_type TEXT NOT NULL, decryption_info jsonb, diff --git a/database/upgrades/12-file-cache-mime-type.sql b/database/upgrades/12-file-cache-mime-type.sql new file mode 100644 index 0000000..1bdb960 --- /dev/null +++ b/database/upgrades/12-file-cache-mime-type.sql @@ -0,0 +1,4 @@ +-- v12: Cache mime type for reuploaded files +ALTER TABLE discord_file ADD COLUMN mime_type TEXT NOT NULL DEFAULT ''; +-- only: postgres +ALTER TABLE discord_file ALTER COLUMN mime_type DROP DEFAULT;