2020-02-14 15:12:33 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2020-07-21 16:48:21 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-14 15:12:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
CREATE TABLE IF NOT EXISTS mediaapi_media_repository (
|
|
|
|
-- 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 INTEGER NOT NULL,
|
|
|
|
-- When the content was uploaded in UNIX epoch ms.
|
|
|
|
creation_ts INTEGER NOT NULL,
|
|
|
|
-- The file name with which the media was uploaded.
|
|
|
|
upload_name TEXT NOT NULL,
|
|
|
|
-- Alternate RFC 4648 unpadded base64 encoding string representation of a SHA-256 hash sum of the file data.
|
|
|
|
base64hash TEXT NOT NULL,
|
|
|
|
-- The user who uploaded the file. Should be a Matrix user ID.
|
|
|
|
user_id TEXT NOT NULL
|
|
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS mediaapi_media_repository_index ON mediaapi_media_repository (media_id, media_origin);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertMediaSQL = `
|
|
|
|
INSERT INTO mediaapi_media_repository (media_id, media_origin, content_type, file_size_bytes, creation_ts, upload_name, base64hash, user_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
|
|
`
|
|
|
|
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
2020-08-25 16:08:37 +02: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
|
|
|
|
`
|
|
|
|
|
2020-02-14 15:12:33 +01:00
|
|
|
type mediaStatements struct {
|
2020-08-25 16:08:37 +02:00
|
|
|
db *sql.DB
|
|
|
|
writer sqlutil.Writer
|
|
|
|
insertMediaStmt *sql.Stmt
|
|
|
|
selectMediaStmt *sql.Stmt
|
|
|
|
selectMediaByHashStmt *sql.Stmt
|
2020-02-14 15:12:33 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:42:08 +02:00
|
|
|
func (s *mediaStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
|
2020-07-21 16:48:21 +02:00
|
|
|
s.db = db
|
2020-08-21 11:42:08 +02:00
|
|
|
s.writer = writer
|
2020-07-21 16:48:21 +02:00
|
|
|
|
2020-02-14 15:12:33 +01:00
|
|
|
_, err = db.Exec(mediaSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return statementList{
|
|
|
|
{&s.insertMediaStmt, insertMediaSQL},
|
|
|
|
{&s.selectMediaStmt, selectMediaSQL},
|
2020-08-25 16:08:37 +02:00
|
|
|
{&s.selectMediaByHashStmt, selectMediaByHashSQL},
|
2020-02-14 15:12:33 +01:00
|
|
|
}.prepare(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mediaStatements) insertMedia(
|
|
|
|
ctx context.Context, mediaMetadata *types.MediaMetadata,
|
|
|
|
) error {
|
|
|
|
mediaMetadata.CreationTimestamp = types.UnixMs(time.Now().UnixNano() / 1000000)
|
2020-07-21 16:48:21 +02:00
|
|
|
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
|
|
|
|
})
|
2020-02-14 15:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mediaStatements) selectMedia(
|
|
|
|
ctx context.Context, mediaID types.MediaID, mediaOrigin gomatrixserverlib.ServerName,
|
|
|
|
) (*types.MediaMetadata, error) {
|
|
|
|
mediaMetadata := types.MediaMetadata{
|
|
|
|
MediaID: mediaID,
|
|
|
|
Origin: mediaOrigin,
|
|
|
|
}
|
|
|
|
err := s.selectMediaStmt.QueryRowContext(
|
|
|
|
ctx, mediaMetadata.MediaID, mediaMetadata.Origin,
|
|
|
|
).Scan(
|
|
|
|
&mediaMetadata.ContentType,
|
|
|
|
&mediaMetadata.FileSizeBytes,
|
|
|
|
&mediaMetadata.CreationTimestamp,
|
|
|
|
&mediaMetadata.UploadName,
|
|
|
|
&mediaMetadata.Base64Hash,
|
|
|
|
&mediaMetadata.UserID,
|
|
|
|
)
|
|
|
|
return &mediaMetadata, err
|
|
|
|
}
|
2020-08-25 16:08:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|