mediaapi: Use ServerName type from gomatrixserverlib

This commit is contained in:
Robert Swain 2017-05-18 11:57:44 +02:00
parent bd9db7557a
commit c5cd5a93b9
7 changed files with 22 additions and 13 deletions

View file

@ -24,6 +24,7 @@ import (
"github.com/matrix-org/dendrite/mediaapi/routing"
"github.com/matrix-org/dendrite/mediaapi/storage"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
log "github.com/Sirupsen/logrus"
)
@ -56,7 +57,7 @@ func main() {
}
cfg := &config.MediaAPI{
ServerName: types.ServerName(serverName),
ServerName: gomatrixserverlib.ServerName(serverName),
BasePath: types.Path(basePath),
MaxFileSizeBytes: types.ContentLength(maxFileSizeBytes),
DataSource: dataSource,

View file

@ -14,12 +14,15 @@
package config
import "github.com/matrix-org/dendrite/mediaapi/types"
import (
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
// MediaAPI contains the config information necessary to spin up a mediaapi process.
type MediaAPI struct {
// The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'.
ServerName types.ServerName `yaml:"server_name"`
ServerName gomatrixserverlib.ServerName `yaml:"server_name"`
// The base path to where media files will be stored.
BasePath types.Path `yaml:"base_path"`
// The maximum file size in bytes that is allowed to be stored on this server.

View file

@ -23,6 +23,7 @@ import (
"github.com/matrix-org/dendrite/mediaapi/storage"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/dendrite/mediaapi/writers"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"github.com/prometheus/client_golang/prometheus"
)
@ -51,7 +52,7 @@ func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg *config.MediaAPI
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(req)
writers.Download(w, req, types.ServerName(vars["serverName"]), types.MediaID(vars["mediaId"]), cfg, db, activeRemoteRequests)
writers.Download(w, req, gomatrixserverlib.ServerName(vars["serverName"]), types.MediaID(vars["mediaId"]), cfg, db, activeRemoteRequests)
})),
)

View file

@ -19,6 +19,7 @@ import (
"time"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
const mediaSchema = `
@ -88,7 +89,7 @@ func (s *mediaStatements) insertMedia(mediaMetadata *types.MediaMetadata) error
return err
}
func (s *mediaStatements) selectMedia(mediaID types.MediaID, mediaOrigin types.ServerName) (*types.MediaMetadata, error) {
func (s *mediaStatements) selectMedia(mediaID types.MediaID, mediaOrigin gomatrixserverlib.ServerName) (*types.MediaMetadata, error) {
mediaMetadata := types.MediaMetadata{
MediaID: mediaID,
Origin: mediaOrigin,

View file

@ -20,6 +20,7 @@ import (
// Import the postgres database driver.
_ "github.com/lib/pq"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
// A Database is used to store room events and stream offsets.
@ -47,7 +48,7 @@ func (d *Database) StoreMediaMetadata(mediaMetadata *types.MediaMetadata) error
}
// GetMediaMetadata possibly selects the metadata about previously uploaded media from the database.
func (d *Database) GetMediaMetadata(mediaID types.MediaID, mediaOrigin types.ServerName, mediaMetadata *types.MediaMetadata) error {
func (d *Database) GetMediaMetadata(mediaID types.MediaID, mediaOrigin gomatrixserverlib.ServerName, mediaMetadata *types.MediaMetadata) error {
metadata, err := d.statements.selectMedia(mediaID, mediaOrigin)
mediaMetadata.ContentType = metadata.ContentType
mediaMetadata.ContentDisposition = metadata.ContentDisposition

View file

@ -14,7 +14,11 @@
package types
import "sync"
import (
"sync"
"github.com/matrix-org/gomatrixserverlib"
)
// ContentDisposition is an HTTP Content-Disposition header string
type ContentDisposition string
@ -34,9 +38,6 @@ type Path string
// MediaID is a string representing the unique identifier for a file (could be a hash but does not have to be)
type MediaID string
// ServerName is the host of a matrix homeserver, e.g. matrix.org
type ServerName string
// RequestMethod is an HTTP request method i.e. GET, POST, etc
type RequestMethod string
@ -49,7 +50,7 @@ type UnixMs int64
// MediaMetadata is metadata associated with a media file
type MediaMetadata struct {
MediaID MediaID
Origin ServerName
Origin gomatrixserverlib.ServerName
ContentType ContentType
ContentDisposition ContentDisposition
ContentLength ContentLength

View file

@ -32,6 +32,7 @@ import (
"github.com/matrix-org/dendrite/mediaapi/config"
"github.com/matrix-org/dendrite/mediaapi/storage"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)
@ -90,7 +91,7 @@ var nTries = 5
// If they are present in the cache, they are served directly.
// If they are not present in the cache, they are obtained from the remote server and
// simultaneously served back to the client and written into the cache.
func Download(w http.ResponseWriter, req *http.Request, origin types.ServerName, mediaID types.MediaID, cfg *config.MediaAPI, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests) {
func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib.ServerName, mediaID types.MediaID, cfg *config.MediaAPI, db *storage.Database, activeRemoteRequests *types.ActiveRemoteRequests) {
r := &downloadRequest{
MediaMetadata: &types.MediaMetadata{
MediaID: mediaID,
@ -529,7 +530,7 @@ func (r *downloadRequest) respondFromRemoteFile(w http.ResponseWriter, basePath
// Given a matrix server name, attempt to discover URLs to contact the server
// on.
func getMatrixUrls(serverName types.ServerName) []string {
func getMatrixUrls(serverName gomatrixserverlib.ServerName) []string {
_, srvs, err := net.LookupSRV("matrix", "tcp", string(serverName))
if err != nil {
return []string{"https://" + string(serverName) + ":8448"}