2022-04-14 13:32:48 +01:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
2020-01-03 14:07:05 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-04-14 13:32:48 +01:00
|
|
|
package shared
|
2020-01-03 14:07:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-07-27 21:29:19 +01:00
|
|
|
"errors"
|
2020-01-03 14:07:05 +00:00
|
|
|
|
2020-04-16 10:06:55 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-04-14 13:32:48 +01:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/storage/tables"
|
2020-01-03 14:07:05 +00:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-01-03 14:07:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
2022-04-14 13:32:48 +01:00
|
|
|
DB *sql.DB
|
|
|
|
Writer sqlutil.Writer
|
|
|
|
MediaRepository tables.MediaRepository
|
|
|
|
Thumbnails tables.Thumbnails
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreMediaMetadata inserts the metadata about the uploaded media into the database.
|
|
|
|
// Returns an error if the combination of MediaID and Origin are not unique in the table.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) StoreMediaMetadata(ctx context.Context, mediaMetadata *types.MediaMetadata) error {
|
2022-04-14 13:32:48 +01:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.MediaRepository.InsertMedia(ctx, txn, mediaMetadata)
|
|
|
|
})
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMediaMetadata 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.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) GetMediaMetadata(ctx context.Context, mediaID types.MediaID, mediaOrigin spec.ServerName) (*types.MediaMetadata, error) {
|
2022-04-14 13:32:48 +01:00
|
|
|
mediaMetadata, err := d.MediaRepository.SelectMedia(ctx, nil, mediaID, mediaOrigin)
|
2024-07-27 21:29:19 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-01-03 14:07:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return mediaMetadata, err
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:08:37 +01:00
|
|
|
// 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.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) GetMediaMetadataByHash(ctx context.Context, mediaHash types.Base64Hash, mediaOrigin spec.ServerName) (*types.MediaMetadata, error) {
|
2022-04-14 13:32:48 +01:00
|
|
|
mediaMetadata, err := d.MediaRepository.SelectMediaByHash(ctx, nil, mediaHash, mediaOrigin)
|
2024-07-27 21:29:19 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-08-25 15:08:37 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return mediaMetadata, err
|
|
|
|
}
|
|
|
|
|
2020-01-03 14:07:05 +00:00
|
|
|
// 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.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) StoreThumbnail(ctx context.Context, thumbnailMetadata *types.ThumbnailMetadata) error {
|
2022-04-14 13:32:48 +01:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.Thumbnails.InsertThumbnail(ctx, txn, thumbnailMetadata)
|
|
|
|
})
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetThumbnail returns metadata about a specific thumbnail.
|
|
|
|
// 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 thumbnail.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) GetThumbnail(ctx context.Context, mediaID types.MediaID, mediaOrigin spec.ServerName, width, height int, resizeMethod string) (*types.ThumbnailMetadata, error) {
|
2022-04-14 13:32:48 +01:00
|
|
|
metadata, err := d.Thumbnails.SelectThumbnail(ctx, nil, mediaID, mediaOrigin, width, height, resizeMethod)
|
2024-07-27 21:29:19 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, nil
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
2022-04-14 13:32:48 +01:00
|
|
|
return metadata, err
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetThumbnails returns metadata about all thumbnails for a specific 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 are no thumbnails associated with this media.
|
2024-07-27 21:29:19 +01:00
|
|
|
func (d *Database) GetThumbnails(ctx context.Context, mediaID types.MediaID, mediaOrigin spec.ServerName) ([]*types.ThumbnailMetadata, error) {
|
2022-04-14 13:32:48 +01:00
|
|
|
metadatas, err := d.Thumbnails.SelectThumbnails(ctx, nil, mediaID, mediaOrigin)
|
2024-07-27 21:29:19 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, nil
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|
2022-04-14 13:32:48 +01:00
|
|
|
return metadatas, err
|
2020-01-03 14:07:05 +00:00
|
|
|
}
|