2020-01-03 08:07:05 -06:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-05-26 09:49:54 -05: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.
|
|
|
|
|
2020-01-03 08:07:05 -06:00
|
|
|
package postgres
|
2017-05-26 09:49:54 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-21 09:44:00 -05:00
|
|
|
"context"
|
2017-05-26 09:49:54 -05:00
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/mediaapi/storage/tables"
|
2017-05-26 09:49:54 -05:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-05-26 09:49:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const mediaSchema = `
|
|
|
|
-- The media_repository table holds metadata for each media file stored and accessible to the local server,
|
|
|
|
-- the actual file is stored separately.
|
2017-08-07 05:51:46 -05:00
|
|
|
CREATE TABLE IF NOT EXISTS mediaapi_media_repository (
|
2017-05-26 09:49:54 -05:00
|
|
|
-- The id used to refer to the media.
|
|
|
|
-- For uploads to this server this is a base64-encoded sha256 hash of the file data
|
|
|
|
-- For media from remote servers, this can be any unique identifier string
|
|
|
|
media_id TEXT NOT NULL,
|
|
|
|
-- The origin of the media as requested by the client. Should be a homeserver domain.
|
|
|
|
media_origin TEXT NOT NULL,
|
|
|
|
-- The MIME-type of the media file as specified when uploading.
|
|
|
|
content_type TEXT NOT NULL,
|
|
|
|
-- Size of the media file in bytes.
|
|
|
|
file_size_bytes BIGINT NOT NULL,
|
|
|
|
-- When the content was uploaded in UNIX epoch ms.
|
|
|
|
creation_ts BIGINT NOT NULL,
|
|
|
|
-- The file name with which the media was uploaded.
|
|
|
|
upload_name TEXT NOT NULL,
|
2017-05-31 00:11:00 -05:00
|
|
|
-- Alternate RFC 4648 unpadded base64 encoding string representation of a SHA-256 hash sum of the file data.
|
2017-05-26 09:49:54 -05:00
|
|
|
base64hash TEXT NOT NULL,
|
|
|
|
-- The user who uploaded the file. Should be a Matrix user ID.
|
|
|
|
user_id TEXT NOT NULL
|
|
|
|
);
|
2017-08-07 05:51:46 -05:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS mediaapi_media_repository_index ON mediaapi_media_repository (media_id, media_origin);
|
2017-05-26 09:49:54 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertMediaSQL = `
|
2017-08-07 05:51:46 -05:00
|
|
|
INSERT INTO mediaapi_media_repository (media_id, media_origin, content_type, file_size_bytes, creation_ts, upload_name, base64hash, user_id)
|
2017-05-26 10:24:13 -05:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
2017-05-26 09:49:54 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectMediaSQL = `
|
2017-08-07 05:51:46 -05:00
|
|
|
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
|
2017-05-26 09:49:54 -05:00
|
|
|
`
|
|
|
|
|
2020-08-25 09:08:37 -05:00
|
|
|
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
|
|
|
|
`
|
|
|
|
|
2017-05-26 09:49:54 -05:00
|
|
|
type mediaStatements struct {
|
2020-08-25 09:08:37 -05:00
|
|
|
insertMediaStmt *sql.Stmt
|
|
|
|
selectMediaStmt *sql.Stmt
|
|
|
|
selectMediaByHashStmt *sql.Stmt
|
2017-05-26 09:49:54 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
func NewPostgresMediaRepositoryTable(db *sql.DB) (tables.MediaRepository, error) {
|
|
|
|
s := &mediaStatements{}
|
|
|
|
_, err := db.Exec(mediaSchema)
|
2017-05-26 09:49:54 -05:00
|
|
|
if err != nil {
|
2022-04-14 07:32:48 -05:00
|
|
|
return nil, err
|
2017-05-26 09:49:54 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
return s, sqlutil.StatementList{
|
2017-05-26 09:49:54 -05:00
|
|
|
{&s.insertMediaStmt, insertMediaSQL},
|
|
|
|
{&s.selectMediaStmt, selectMediaSQL},
|
2020-08-25 09:08:37 -05:00
|
|
|
{&s.selectMediaByHashStmt, selectMediaByHashSQL},
|
2022-04-14 07:32:48 -05:00
|
|
|
}.Prepare(db)
|
2017-05-26 09:49:54 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
func (s *mediaStatements) InsertMedia(
|
|
|
|
ctx context.Context, txn *sql.Tx, mediaMetadata *types.MediaMetadata,
|
2017-09-21 09:44:00 -05:00
|
|
|
) error {
|
2023-04-19 09:50:33 -05:00
|
|
|
mediaMetadata.CreationTimestamp = spec.AsTimestamp(time.Now())
|
2022-04-14 07:32:48 -05:00
|
|
|
_, err := sqlutil.TxStmtContext(ctx, txn, s.insertMediaStmt).ExecContext(
|
2017-09-21 09:44:00 -05:00
|
|
|
ctx,
|
2017-05-26 09:49:54 -05:00
|
|
|
mediaMetadata.MediaID,
|
|
|
|
mediaMetadata.Origin,
|
|
|
|
mediaMetadata.ContentType,
|
|
|
|
mediaMetadata.FileSizeBytes,
|
|
|
|
mediaMetadata.CreationTimestamp,
|
|
|
|
mediaMetadata.UploadName,
|
|
|
|
mediaMetadata.Base64Hash,
|
|
|
|
mediaMetadata.UserID,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
func (s *mediaStatements) SelectMedia(
|
2023-04-19 09:50:33 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, mediaID types.MediaID, mediaOrigin spec.ServerName,
|
2017-09-21 09:44:00 -05:00
|
|
|
) (*types.MediaMetadata, error) {
|
2017-05-26 09:49:54 -05:00
|
|
|
mediaMetadata := types.MediaMetadata{
|
|
|
|
MediaID: mediaID,
|
|
|
|
Origin: mediaOrigin,
|
|
|
|
}
|
2022-04-14 07:32:48 -05:00
|
|
|
err := sqlutil.TxStmtContext(ctx, txn, s.selectMediaStmt).QueryRowContext(
|
2017-09-21 09:44:00 -05:00
|
|
|
ctx, mediaMetadata.MediaID, mediaMetadata.Origin,
|
2017-05-26 09:49:54 -05:00
|
|
|
).Scan(
|
|
|
|
&mediaMetadata.ContentType,
|
|
|
|
&mediaMetadata.FileSizeBytes,
|
|
|
|
&mediaMetadata.CreationTimestamp,
|
|
|
|
&mediaMetadata.UploadName,
|
|
|
|
&mediaMetadata.Base64Hash,
|
|
|
|
&mediaMetadata.UserID,
|
|
|
|
)
|
|
|
|
return &mediaMetadata, err
|
|
|
|
}
|
2020-08-25 09:08:37 -05:00
|
|
|
|
2022-04-14 07:32:48 -05:00
|
|
|
func (s *mediaStatements) SelectMediaByHash(
|
2023-04-19 09:50:33 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, mediaHash types.Base64Hash, mediaOrigin spec.ServerName,
|
2020-08-25 09:08:37 -05:00
|
|
|
) (*types.MediaMetadata, error) {
|
|
|
|
mediaMetadata := types.MediaMetadata{
|
|
|
|
Base64Hash: mediaHash,
|
|
|
|
Origin: mediaOrigin,
|
|
|
|
}
|
2022-04-14 07:32:48 -05:00
|
|
|
err := sqlutil.TxStmtContext(ctx, txn, s.selectMediaByHashStmt).QueryRowContext(
|
2020-08-25 09:08:37 -05:00
|
|
|
ctx, mediaMetadata.Base64Hash, mediaMetadata.Origin,
|
|
|
|
).Scan(
|
|
|
|
&mediaMetadata.ContentType,
|
|
|
|
&mediaMetadata.FileSizeBytes,
|
|
|
|
&mediaMetadata.CreationTimestamp,
|
|
|
|
&mediaMetadata.UploadName,
|
|
|
|
&mediaMetadata.MediaID,
|
|
|
|
&mediaMetadata.UserID,
|
|
|
|
)
|
|
|
|
return &mediaMetadata, err
|
|
|
|
}
|