mirror of
https://github.com/matrix-org/dendrite
synced 2024-12-14 15:22:50 +00:00
96 lines
3 KiB
Go
96 lines
3 KiB
Go
|
// 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 writers
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
log "github.com/Sirupsen/logrus"
|
||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||
|
"github.com/matrix-org/dendrite/mediaapi/config"
|
||
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
||
|
"github.com/matrix-org/gomatrixserverlib"
|
||
|
"github.com/matrix-org/util"
|
||
|
)
|
||
|
|
||
|
// downloadRequest metadata included in or derivable from an download request
|
||
|
// https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-media-r0-download-servername-mediaid
|
||
|
type downloadRequest struct {
|
||
|
MediaMetadata *types.MediaMetadata
|
||
|
Logger *log.Entry
|
||
|
}
|
||
|
|
||
|
// Download implements /download
|
||
|
func Download(w http.ResponseWriter, req *http.Request, origin gomatrixserverlib.ServerName, mediaID types.MediaID, cfg *config.MediaAPI, activeRemoteRequests *types.ActiveRemoteRequests) {
|
||
|
r := &downloadRequest{
|
||
|
MediaMetadata: &types.MediaMetadata{
|
||
|
MediaID: mediaID,
|
||
|
Origin: origin,
|
||
|
},
|
||
|
Logger: util.GetLogger(req.Context()),
|
||
|
}
|
||
|
|
||
|
// request validation
|
||
|
if req.Method != "GET" {
|
||
|
r.jsonErrorResponse(w, util.JSONResponse{
|
||
|
Code: 405,
|
||
|
JSON: jsonerror.Unknown("request method must be GET"),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if resErr := r.Validate(); resErr != nil {
|
||
|
r.jsonErrorResponse(w, *resErr)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// doDownload
|
||
|
}
|
||
|
|
||
|
func (r *downloadRequest) jsonErrorResponse(w http.ResponseWriter, res util.JSONResponse) {
|
||
|
// Marshal JSON response into raw bytes to send as the HTTP body
|
||
|
resBytes, err := json.Marshal(res.JSON)
|
||
|
if err != nil {
|
||
|
r.Logger.WithError(err).Error("Failed to marshal JSONResponse")
|
||
|
// this should never fail to be marshalled so drop err to the floor
|
||
|
res = util.MessageResponse(500, "Internal Server Error")
|
||
|
resBytes, _ = json.Marshal(res.JSON)
|
||
|
}
|
||
|
|
||
|
// Set status code and write the body
|
||
|
w.WriteHeader(res.Code)
|
||
|
r.Logger.WithField("code", res.Code).Infof("Responding (%d bytes)", len(resBytes))
|
||
|
w.Write(resBytes)
|
||
|
}
|
||
|
|
||
|
// Validate validates the downloadRequest fields
|
||
|
func (r *downloadRequest) Validate() *util.JSONResponse {
|
||
|
// maybe give the URL pattern in the routing, these are not even possible as the handler would not be hit...?
|
||
|
if r.MediaMetadata.MediaID == "" {
|
||
|
return &util.JSONResponse{
|
||
|
Code: 404,
|
||
|
JSON: jsonerror.NotFound("mediaId must be a non-empty string"),
|
||
|
}
|
||
|
}
|
||
|
if r.MediaMetadata.Origin == "" {
|
||
|
return &util.JSONResponse{
|
||
|
Code: 404,
|
||
|
JSON: jsonerror.NotFound("serverName must be a non-empty string"),
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|