Browse Source

Store mime type for reuploaded files

Tulir Asokan 2 years ago
parent
commit
75fafae82f

+ 2 - 1
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))

+ 9 - 8
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, &timestamp)
+	err := row.Scan(&f.URL, &f.Encrypted, &fileID, &mxc, &f.Size, &width, &height, &f.MimeType, &decryptionInfo, &timestamp)
 	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 {

+ 5 - 4
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,
 

+ 4 - 0
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;