diff --git a/cmd/mautrix-telegram/legacymigrate.sql b/cmd/mautrix-telegram/legacymigrate.sql index 82d67691..b4a85ed2 100644 --- a/cmd/mautrix-telegram/legacymigrate.sql +++ b/cmd/mautrix-telegram/legacymigrate.sql @@ -218,8 +218,8 @@ FROM telethon_entities_old WHERE phone<>'' ON CONFLICT DO NOTHING; -INSERT INTO telegram_file (id, mxc, mime_type, size) -SELECT id, mxc, mime_type, size +INSERT INTO telegram_file (id, mxc, mime_type, size, width, height, timestamp) +SELECT id, mxc, mime_type, size, width, height, timestamp FROM telegram_file_old; INSERT INTO disappearing_message (bridge_id, mx_room, mxid, type, timer, disappear_at) diff --git a/pkg/connector/media/transfer.go b/pkg/connector/media/transfer.go index 58770c3b..be7c08de 100644 --- a/pkg/connector/media/transfer.go +++ b/pkg/connector/media/transfer.go @@ -23,6 +23,7 @@ import ( "io" "os" "strings" + "time" "github.com/rs/zerolog" "maunium.net/go/mautrix/bridgev2" @@ -142,18 +143,24 @@ func (t *Transferer) WithStickerConfig(cfg AnimatedStickerConfig) *Transferer { return t } -func (t *Transferer) adjustStickerSize() { - if (t.fileInfo.Width < 256 && t.fileInfo.Height < 256) || t.animatedStickerConfig == nil { +func adjustStickerSize(info *event.FileInfo) { + if info.Width <= 256 && info.Height <= 256 { return } - if t.fileInfo.Width == t.fileInfo.Height { - t.fileInfo.Width, t.fileInfo.Height = 256, 256 - } else if t.fileInfo.Width > t.fileInfo.Height { - t.fileInfo.Height = t.fileInfo.Height * 256 / t.fileInfo.Width - t.fileInfo.Width = 256 + if info.Width == info.Height { + info.Width, info.Height = 256, 256 + } else if info.Width > info.Height { + info.Height = info.Height * 256 / info.Width + info.Width = 256 } else { - t.fileInfo.Width = t.fileInfo.Width * 256 / t.fileInfo.Height - t.fileInfo.Height = 256 + info.Width = info.Width * 256 / info.Height + info.Height = 256 + } +} + +func (t *Transferer) adjustStickerSize() { + if t.animatedStickerConfig != nil { + adjustStickerSize(&t.fileInfo) } } @@ -284,7 +291,10 @@ func (t *ReadyTransferer) Transfer(ctx context.Context, store *store.Container, if file, err := store.TelegramFile.GetByLocationID(ctx, locationID); err != nil { return "", nil, nil, fmt.Errorf("failed to search for Telegram file by location ID: %w", err) } else if file != nil { - t.inner.fileInfo.Size, t.inner.fileInfo.MimeType = file.Size, file.MIMEType + t.inner.fileInfo.Size = file.Size + t.inner.fileInfo.Width = file.Width + t.inner.fileInfo.Height = file.Height + t.inner.fileInfo.MimeType = file.MIMEType return file.MXC, nil, &t.inner.fileInfo, nil } @@ -357,8 +367,11 @@ func (t *ReadyTransferer) Transfer(ctx context.Context, store *store.Container, file := store.TelegramFile.New() file.LocationID = locationID file.MXC = mxc - file.Size = t.inner.fileInfo.Size file.MIMEType = t.inner.fileInfo.MimeType + file.Size = t.inner.fileInfo.Size + file.Width = t.inner.fileInfo.Width + file.Height = t.inner.fileInfo.Height + file.Timestamp = time.Now() if err = file.Insert(ctx); err != nil { log.Err(err).Msg("failed to insert Telegram file into database") } diff --git a/pkg/connector/store/telegramfile.go b/pkg/connector/store/telegramfile.go index 7284f670..9154957a 100644 --- a/pkg/connector/store/telegramfile.go +++ b/pkg/connector/store/telegramfile.go @@ -18,16 +18,18 @@ package store import ( "context" + "database/sql" + "time" "go.mau.fi/util/dbutil" "maunium.net/go/mautrix/id" ) const ( - insertTelegramFileQuery = "INSERT INTO telegram_file (id, mxc, mime_type, size) VALUES ($1, $2, $3, $4)" - getTelegramFileSelect = "SELECT id, mxc, mime_type, size FROM telegram_file " - getTelegramFileByLocationIDQuery = getTelegramFileSelect + "WHERE id=$1" - getTelegramFileByMXCQuery = getTelegramFileSelect + "WHERE mxc=$1" + insertTelegramFileQuery = "INSERT INTO telegram_file (id, mxc, mime_type, size, width, height, timestamp) VALUES ($1, $2, $3, $4, $5, $6, $7)" + getTelegramFileSelect = "SELECT id, mxc, mime_type, size, width, height, timestamp FROM telegram_file" + getTelegramFileByLocationIDQuery = getTelegramFileSelect + " WHERE id=$1" + getTelegramFileByMXCQuery = getTelegramFileSelect + " WHERE mxc=$1" ) type TelegramFileQuery struct { @@ -43,6 +45,9 @@ type TelegramFile struct { MXC id.ContentURIString MIMEType string Size int + Width int + Height int + Timestamp time.Time } var _ dbutil.DataStruct[*TelegramFile] = (*TelegramFile)(nil) @@ -60,7 +65,15 @@ func (fq *TelegramFileQuery) GetByMXC(ctx context.Context, mxc string) (*Telegra } func (f *TelegramFile) sqlVariables() []any { - return []any{f.LocationID, f.MXC, f.MIMEType, f.Size} + return []any{ + f.LocationID, + f.MXC, + dbutil.StrPtr(f.MIMEType), + dbutil.NumPtr(f.Size), + dbutil.NumPtr(f.Width), + dbutil.NumPtr(f.Height), + dbutil.ConvertedPtr(f.Timestamp, time.Time.Unix), + } } func (f *TelegramFile) Insert(ctx context.Context) error { @@ -68,5 +81,18 @@ func (f *TelegramFile) Insert(ctx context.Context) error { } func (f *TelegramFile) Scan(row dbutil.Scannable) (*TelegramFile, error) { - return f, row.Scan(&f.LocationID, &f.MXC, &f.MIMEType, &f.Size) + var mime sql.NullString + var size, width, height, timestamp sql.NullInt64 + err := row.Scan(&f.LocationID, &f.MXC, &mime, &size, &width, &height, ×tamp) + if err != nil { + return nil, err + } + f.MIMEType = mime.String + f.Size = int(size.Int64) + f.Width = int(width.Int64) + f.Height = int(height.Int64) + if timestamp.Int64 > 0 { + f.Timestamp = time.Unix(timestamp.Int64, 0) + } + return f, nil } diff --git a/pkg/connector/store/upgrades/00-latest.sql b/pkg/connector/store/upgrades/00-latest.sql index f7e790a3..61b03ec5 100644 --- a/pkg/connector/store/upgrades/00-latest.sql +++ b/pkg/connector/store/upgrades/00-latest.sql @@ -49,7 +49,10 @@ CREATE TABLE telegram_file ( id TEXT PRIMARY KEY, mxc TEXT NOT NULL, mime_type TEXT, - size BIGINT + size BIGINT, + width INTEGER, + height INTEGER, + timestamp BIGINT ); CREATE INDEX telegram_file_mxc_idx ON telegram_file (mxc); diff --git a/pkg/connector/store/upgrades/08-telegram-file-more-info.sql b/pkg/connector/store/upgrades/08-telegram-file-more-info.sql new file mode 100644 index 00000000..01578671 --- /dev/null +++ b/pkg/connector/store/upgrades/08-telegram-file-more-info.sql @@ -0,0 +1,4 @@ +-- v8 (compatible with v2+): Add more info to telegram_file +ALTER TABLE telegram_file ADD COLUMN width INTEGER; +ALTER TABLE telegram_file ADD COLUMN height INTEGER; +ALTER TABLE telegram_file ADD COLUMN timestamp BIGINT;