store: add more info to telegram_file table
This commit is contained in:
@@ -218,8 +218,8 @@ FROM telethon_entities_old
|
|||||||
WHERE phone<>''
|
WHERE phone<>''
|
||||||
ON CONFLICT DO NOTHING;
|
ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
INSERT INTO telegram_file (id, mxc, mime_type, size)
|
INSERT INTO telegram_file (id, mxc, mime_type, size, width, height, timestamp)
|
||||||
SELECT id, mxc, mime_type, size
|
SELECT id, mxc, mime_type, size, width, height, timestamp
|
||||||
FROM telegram_file_old;
|
FROM telegram_file_old;
|
||||||
|
|
||||||
INSERT INTO disappearing_message (bridge_id, mx_room, mxid, type, timer, disappear_at)
|
INSERT INTO disappearing_message (bridge_id, mx_room, mxid, type, timer, disappear_at)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
@@ -142,18 +143,24 @@ func (t *Transferer) WithStickerConfig(cfg AnimatedStickerConfig) *Transferer {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transferer) adjustStickerSize() {
|
func adjustStickerSize(info *event.FileInfo) {
|
||||||
if (t.fileInfo.Width < 256 && t.fileInfo.Height < 256) || t.animatedStickerConfig == nil {
|
if info.Width <= 256 && info.Height <= 256 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if t.fileInfo.Width == t.fileInfo.Height {
|
if info.Width == info.Height {
|
||||||
t.fileInfo.Width, t.fileInfo.Height = 256, 256
|
info.Width, info.Height = 256, 256
|
||||||
} else if t.fileInfo.Width > t.fileInfo.Height {
|
} else if info.Width > info.Height {
|
||||||
t.fileInfo.Height = t.fileInfo.Height * 256 / t.fileInfo.Width
|
info.Height = info.Height * 256 / info.Width
|
||||||
t.fileInfo.Width = 256
|
info.Width = 256
|
||||||
} else {
|
} else {
|
||||||
t.fileInfo.Width = t.fileInfo.Width * 256 / t.fileInfo.Height
|
info.Width = info.Width * 256 / info.Height
|
||||||
t.fileInfo.Height = 256
|
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 {
|
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)
|
return "", nil, nil, fmt.Errorf("failed to search for Telegram file by location ID: %w", err)
|
||||||
} else if file != nil {
|
} 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
|
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 := store.TelegramFile.New()
|
||||||
file.LocationID = locationID
|
file.LocationID = locationID
|
||||||
file.MXC = mxc
|
file.MXC = mxc
|
||||||
file.Size = t.inner.fileInfo.Size
|
|
||||||
file.MIMEType = t.inner.fileInfo.MimeType
|
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 {
|
if err = file.Insert(ctx); err != nil {
|
||||||
log.Err(err).Msg("failed to insert Telegram file into database")
|
log.Err(err).Msg("failed to insert Telegram file into database")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,16 +18,18 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.mau.fi/util/dbutil"
|
"go.mau.fi/util/dbutil"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
insertTelegramFileQuery = "INSERT INTO telegram_file (id, mxc, mime_type, size) VALUES ($1, $2, $3, $4)"
|
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 FROM telegram_file "
|
getTelegramFileSelect = "SELECT id, mxc, mime_type, size, width, height, timestamp FROM telegram_file"
|
||||||
getTelegramFileByLocationIDQuery = getTelegramFileSelect + "WHERE id=$1"
|
getTelegramFileByLocationIDQuery = getTelegramFileSelect + " WHERE id=$1"
|
||||||
getTelegramFileByMXCQuery = getTelegramFileSelect + "WHERE mxc=$1"
|
getTelegramFileByMXCQuery = getTelegramFileSelect + " WHERE mxc=$1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TelegramFileQuery struct {
|
type TelegramFileQuery struct {
|
||||||
@@ -43,6 +45,9 @@ type TelegramFile struct {
|
|||||||
MXC id.ContentURIString
|
MXC id.ContentURIString
|
||||||
MIMEType string
|
MIMEType string
|
||||||
Size int
|
Size int
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
Timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ dbutil.DataStruct[*TelegramFile] = (*TelegramFile)(nil)
|
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 {
|
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 {
|
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) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,10 @@ CREATE TABLE telegram_file (
|
|||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
mxc TEXT NOT NULL,
|
mxc TEXT NOT NULL,
|
||||||
mime_type TEXT,
|
mime_type TEXT,
|
||||||
size BIGINT
|
size BIGINT,
|
||||||
|
width INTEGER,
|
||||||
|
height INTEGER,
|
||||||
|
timestamp BIGINT
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX telegram_file_mxc_idx ON telegram_file (mxc);
|
CREATE INDEX telegram_file_mxc_idx ON telegram_file (mxc);
|
||||||
|
|||||||
@@ -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;
|
||||||
Reference in New Issue
Block a user