2017-05-26 15:49:54 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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 storage
|
|
|
|
|
|
|
|
import (
|
2017-09-21 15:44:00 +01:00
|
|
|
"context"
|
2017-05-26 15:49:54 +01:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2017-05-26 16:44:43 +01:00
|
|
|
// Database is used to store metadata about a repository of media files.
|
2017-05-26 15:49:54 +01:00
|
|
|
type Database struct {
|
|
|
|
statements statements
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:44:43 +01:00
|
|
|
// Open opens a postgres database.
|
2017-05-26 15:49:54 +01:00
|
|
|
func Open(dataSourceName string) (*Database, error) {
|
|
|
|
var d Database
|
|
|
|
var err error
|
|
|
|
if d.db, err = sql.Open("postgres", dataSourceName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = d.statements.prepare(d.db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-09-21 15:44:00 +01:00
|
|
|
func (d *Database) StoreMediaMetadata(
|
|
|
|
ctx context.Context, mediaMetadata *types.MediaMetadata,
|
|
|
|
) error {
|
|
|
|
return d.statements.media.insertMedia(ctx, mediaMetadata)
|
2017-05-26 15:49:54 +01:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:44:43 +01: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.
|
2017-05-31 13:29:28 +01:00
|
|
|
// Returns nil metadata if there is no metadata associated with this media.
|
2017-09-21 15:44:00 +01:00
|
|
|
func (d *Database) GetMediaMetadata(
|
|
|
|
ctx context.Context, mediaID types.MediaID, mediaOrigin gomatrixserverlib.ServerName,
|
|
|
|
) (*types.MediaMetadata, error) {
|
|
|
|
mediaMetadata, err := d.statements.media.selectMedia(ctx, mediaID, mediaOrigin)
|
2017-05-31 13:29:28 +01:00
|
|
|
if err != nil && err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return mediaMetadata, err
|
2017-05-26 15:49:54 +01:00
|
|
|
}
|
2017-06-07 00:12:49 +01: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.
|
2017-09-21 15:44:00 +01:00
|
|
|
func (d *Database) StoreThumbnail(
|
|
|
|
ctx context.Context, thumbnailMetadata *types.ThumbnailMetadata,
|
|
|
|
) error {
|
|
|
|
return d.statements.thumbnail.insertThumbnail(ctx, thumbnailMetadata)
|
2017-06-07 00:12:49 +01: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.
|
2017-09-21 15:44:00 +01:00
|
|
|
func (d *Database) GetThumbnail(
|
|
|
|
ctx context.Context,
|
|
|
|
mediaID types.MediaID,
|
|
|
|
mediaOrigin gomatrixserverlib.ServerName,
|
|
|
|
width, height int,
|
|
|
|
resizeMethod string,
|
|
|
|
) (*types.ThumbnailMetadata, error) {
|
|
|
|
thumbnailMetadata, err := d.statements.thumbnail.selectThumbnail(
|
|
|
|
ctx, mediaID, mediaOrigin, width, height, resizeMethod,
|
|
|
|
)
|
2017-06-07 00:12:49 +01:00
|
|
|
if err != nil && err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return thumbnailMetadata, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-09-21 15:44:00 +01:00
|
|
|
func (d *Database) GetThumbnails(
|
|
|
|
ctx context.Context, mediaID types.MediaID, mediaOrigin gomatrixserverlib.ServerName,
|
|
|
|
) ([]*types.ThumbnailMetadata, error) {
|
|
|
|
thumbnails, err := d.statements.thumbnail.selectThumbnails(ctx, mediaID, mediaOrigin)
|
2017-06-07 00:12:49 +01:00
|
|
|
if err != nil && err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return thumbnails, err
|
|
|
|
}
|