aboutsummaryrefslogtreecommitdiff
path: root/mediaapi
diff options
context:
space:
mode:
Diffstat (limited to 'mediaapi')
-rw-r--r--mediaapi/storage/sqlite3/media_repository_table.go33
1 files changed, 21 insertions, 12 deletions
diff --git a/mediaapi/storage/sqlite3/media_repository_table.go b/mediaapi/storage/sqlite3/media_repository_table.go
index 8e2e6236..f53f164d 100644
--- a/mediaapi/storage/sqlite3/media_repository_table.go
+++ b/mediaapi/storage/sqlite3/media_repository_table.go
@@ -20,6 +20,7 @@ import (
"database/sql"
"time"
+ "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@@ -60,11 +61,16 @@ SELECT content_type, file_size_bytes, creation_ts, upload_name, base64hash, user
`
type mediaStatements struct {
+ db *sql.DB
+ writer *sqlutil.TransactionWriter
insertMediaStmt *sql.Stmt
selectMediaStmt *sql.Stmt
}
func (s *mediaStatements) prepare(db *sql.DB) (err error) {
+ s.db = db
+ s.writer = sqlutil.NewTransactionWriter()
+
_, err = db.Exec(mediaSchema)
if err != nil {
return
@@ -80,18 +86,21 @@ func (s *mediaStatements) insertMedia(
ctx context.Context, mediaMetadata *types.MediaMetadata,
) error {
mediaMetadata.CreationTimestamp = types.UnixMs(time.Now().UnixNano() / 1000000)
- _, err := s.insertMediaStmt.ExecContext(
- ctx,
- mediaMetadata.MediaID,
- mediaMetadata.Origin,
- mediaMetadata.ContentType,
- mediaMetadata.FileSizeBytes,
- mediaMetadata.CreationTimestamp,
- mediaMetadata.UploadName,
- mediaMetadata.Base64Hash,
- mediaMetadata.UserID,
- )
- return err
+ return s.writer.Do(s.db, nil, func(txn *sql.Tx) error {
+ stmt := sqlutil.TxStmt(txn, s.insertMediaStmt)
+ _, err := stmt.ExecContext(
+ ctx,
+ mediaMetadata.MediaID,
+ mediaMetadata.Origin,
+ mediaMetadata.ContentType,
+ mediaMetadata.FileSizeBytes,
+ mediaMetadata.CreationTimestamp,
+ mediaMetadata.UploadName,
+ mediaMetadata.Base64Hash,
+ mediaMetadata.UserID,
+ )
+ return err
+ })
}
func (s *mediaStatements) selectMedia(