mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 07:23:10 -06:00
mediaapi: Use ServerName type from gomatrixserverlib
This commit is contained in:
parent
bd9db7557a
commit
c5cd5a93b9
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/mediaapi/routing"
|
"github.com/matrix-org/dendrite/mediaapi/routing"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/storage"
|
"github.com/matrix-org/dendrite/mediaapi/storage"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
@ -56,7 +57,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := &config.MediaAPI{
|
cfg := &config.MediaAPI{
|
||||||
ServerName: types.ServerName(serverName),
|
ServerName: gomatrixserverlib.ServerName(serverName),
|
||||||
BasePath: types.Path(basePath),
|
BasePath: types.Path(basePath),
|
||||||
MaxFileSizeBytes: types.ContentLength(maxFileSizeBytes),
|
MaxFileSizeBytes: types.ContentLength(maxFileSizeBytes),
|
||||||
DataSource: dataSource,
|
DataSource: dataSource,
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,15 @@
|
||||||
|
|
||||||
package config
|
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.
|
// MediaAPI contains the config information necessary to spin up a mediaapi process.
|
||||||
type MediaAPI struct {
|
type MediaAPI struct {
|
||||||
// The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'.
|
// 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.
|
// The base path to where media files will be stored.
|
||||||
BasePath types.Path `yaml:"base_path"`
|
BasePath types.Path `yaml:"base_path"`
|
||||||
// The maximum file size in bytes that is allowed to be stored on this server.
|
// The maximum file size in bytes that is allowed to be stored on this server.
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/mediaapi/storage"
|
"github.com/matrix-org/dendrite/mediaapi/storage"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/writers"
|
"github.com/matrix-org/dendrite/mediaapi/writers"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
vars := mux.Vars(req)
|
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)
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mediaSchema = `
|
const mediaSchema = `
|
||||||
|
|
@ -88,7 +89,7 @@ func (s *mediaStatements) insertMedia(mediaMetadata *types.MediaMetadata) error
|
||||||
return err
|
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{
|
mediaMetadata := types.MediaMetadata{
|
||||||
MediaID: mediaID,
|
MediaID: mediaID,
|
||||||
Origin: mediaOrigin,
|
Origin: mediaOrigin,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
// Import the postgres database driver.
|
// Import the postgres database driver.
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Database is used to store room events and stream offsets.
|
// 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.
|
// 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)
|
metadata, err := d.statements.selectMedia(mediaID, mediaOrigin)
|
||||||
mediaMetadata.ContentType = metadata.ContentType
|
mediaMetadata.ContentType = metadata.ContentType
|
||||||
mediaMetadata.ContentDisposition = metadata.ContentDisposition
|
mediaMetadata.ContentDisposition = metadata.ContentDisposition
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,11 @@
|
||||||
|
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
// ContentDisposition is an HTTP Content-Disposition header string
|
// ContentDisposition is an HTTP Content-Disposition header string
|
||||||
type ContentDisposition 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)
|
// MediaID is a string representing the unique identifier for a file (could be a hash but does not have to be)
|
||||||
type MediaID string
|
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
|
// RequestMethod is an HTTP request method i.e. GET, POST, etc
|
||||||
type RequestMethod string
|
type RequestMethod string
|
||||||
|
|
||||||
|
|
@ -49,7 +50,7 @@ type UnixMs int64
|
||||||
// MediaMetadata is metadata associated with a media file
|
// MediaMetadata is metadata associated with a media file
|
||||||
type MediaMetadata struct {
|
type MediaMetadata struct {
|
||||||
MediaID MediaID
|
MediaID MediaID
|
||||||
Origin ServerName
|
Origin gomatrixserverlib.ServerName
|
||||||
ContentType ContentType
|
ContentType ContentType
|
||||||
ContentDisposition ContentDisposition
|
ContentDisposition ContentDisposition
|
||||||
ContentLength ContentLength
|
ContentLength ContentLength
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/mediaapi/config"
|
"github.com/matrix-org/dendrite/mediaapi/config"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/storage"
|
"github.com/matrix-org/dendrite/mediaapi/storage"
|
||||||
"github.com/matrix-org/dendrite/mediaapi/types"
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"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 present in the cache, they are served directly.
|
||||||
// If they are not present in the cache, they are obtained from the remote server and
|
// 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.
|
// 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{
|
r := &downloadRequest{
|
||||||
MediaMetadata: &types.MediaMetadata{
|
MediaMetadata: &types.MediaMetadata{
|
||||||
MediaID: mediaID,
|
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
|
// Given a matrix server name, attempt to discover URLs to contact the server
|
||||||
// on.
|
// on.
|
||||||
func getMatrixUrls(serverName types.ServerName) []string {
|
func getMatrixUrls(serverName gomatrixserverlib.ServerName) []string {
|
||||||
_, srvs, err := net.LookupSRV("matrix", "tcp", string(serverName))
|
_, srvs, err := net.LookupSRV("matrix", "tcp", string(serverName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{"https://" + string(serverName) + ":8448"}
|
return []string{"https://" + string(serverName) + ":8448"}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue