diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-08-25 15:08:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 15:08:37 +0100 |
commit | 287700baeebd3762008db368122f34076dd76f3c (patch) | |
tree | 51e8fcbf2b31f5ff62376f38b58f126de422bb5f /mediaapi/storage/postgres | |
parent | a4db43e0969125db899dae465daf3ab1385c8ce9 (diff) |
Randomly generated media IDs (#1336)
* Filenames based on hashes but media IDs randomly generated and per-upload metadata
* Lint
* Comment generateMediaID
Diffstat (limited to 'mediaapi/storage/postgres')
-rw-r--r-- | mediaapi/storage/postgres/media_repository_table.go | 30 | ||||
-rw-r--r-- | mediaapi/storage/postgres/storage.go | 13 |
2 files changed, 41 insertions, 2 deletions
diff --git a/mediaapi/storage/postgres/media_repository_table.go b/mediaapi/storage/postgres/media_repository_table.go index e975530a..1d3264ca 100644 --- a/mediaapi/storage/postgres/media_repository_table.go +++ b/mediaapi/storage/postgres/media_repository_table.go @@ -59,9 +59,14 @@ const selectMediaSQL = ` SELECT content_type, file_size_bytes, creation_ts, upload_name, base64hash, user_id FROM mediaapi_media_repository WHERE media_id = $1 AND media_origin = $2 ` +const selectMediaByHashSQL = ` +SELECT content_type, file_size_bytes, creation_ts, upload_name, media_id, user_id FROM mediaapi_media_repository WHERE base64hash = $1 AND media_origin = $2 +` + type mediaStatements struct { - insertMediaStmt *sql.Stmt - selectMediaStmt *sql.Stmt + insertMediaStmt *sql.Stmt + selectMediaStmt *sql.Stmt + selectMediaByHashStmt *sql.Stmt } func (s *mediaStatements) prepare(db *sql.DB) (err error) { @@ -73,6 +78,7 @@ func (s *mediaStatements) prepare(db *sql.DB) (err error) { return statementList{ {&s.insertMediaStmt, insertMediaSQL}, {&s.selectMediaStmt, selectMediaSQL}, + {&s.selectMediaByHashStmt, selectMediaByHashSQL}, }.prepare(db) } @@ -113,3 +119,23 @@ func (s *mediaStatements) selectMedia( ) return &mediaMetadata, err } + +func (s *mediaStatements) selectMediaByHash( + ctx context.Context, mediaHash types.Base64Hash, mediaOrigin gomatrixserverlib.ServerName, +) (*types.MediaMetadata, error) { + mediaMetadata := types.MediaMetadata{ + Base64Hash: mediaHash, + Origin: mediaOrigin, + } + err := s.selectMediaStmt.QueryRowContext( + ctx, mediaMetadata.Base64Hash, mediaMetadata.Origin, + ).Scan( + &mediaMetadata.ContentType, + &mediaMetadata.FileSizeBytes, + &mediaMetadata.CreationTimestamp, + &mediaMetadata.UploadName, + &mediaMetadata.MediaID, + &mediaMetadata.UserID, + ) + return &mediaMetadata, err +} diff --git a/mediaapi/storage/postgres/storage.go b/mediaapi/storage/postgres/storage.go index 756913d3..f89501de 100644 --- a/mediaapi/storage/postgres/storage.go +++ b/mediaapi/storage/postgres/storage.go @@ -67,6 +67,19 @@ func (d *Database) GetMediaMetadata( return mediaMetadata, err } +// GetMediaMetadataByHash returns metadata about media stored on this server. +// The media could have been uploaded to this server or fetched from another server and cached here. +// Returns nil metadata if there is no metadata associated with this media. +func (d *Database) GetMediaMetadataByHash( + ctx context.Context, mediaHash types.Base64Hash, mediaOrigin gomatrixserverlib.ServerName, +) (*types.MediaMetadata, error) { + mediaMetadata, err := d.statements.media.selectMediaByHash(ctx, mediaHash, mediaOrigin) + if err != nil && err == sql.ErrNoRows { + return nil, nil + } + return mediaMetadata, err +} + // StoreThumbnail inserts the metadata about the thumbnail into the database. // Returns an error if the combination of MediaID and Origin are not unique in the table. func (d *Database) StoreThumbnail( |