2017-05-26 07:57:09 +00: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 writers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-26 12:42:51 +00:00
|
|
|
"fmt"
|
2017-05-26 07:57:09 +00:00
|
|
|
"net/http"
|
2017-05-26 12:42:51 +00:00
|
|
|
"regexp"
|
2017-05-26 07:57:09 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-05-26 12:42:51 +00:00
|
|
|
const mediaIDCharacters = "A-Za-z0-9_=-"
|
|
|
|
|
|
|
|
// Note: unfortunately regex.MustCompile() cannot be assigned to a const
|
|
|
|
var mediaIDRegex = regexp.MustCompile("[" + mediaIDCharacters + "]+")
|
|
|
|
|
2017-05-26 07:57:09 +00:00
|
|
|
// 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 {
|
2017-05-26 12:42:51 +00:00
|
|
|
if mediaIDRegex.MatchString(string(r.MediaMetadata.MediaID)) == false {
|
2017-05-26 07:57:09 +00:00
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: 404,
|
2017-05-26 12:42:51 +00:00
|
|
|
JSON: jsonerror.NotFound(fmt.Sprintf("mediaId must be a non-empty string using only characters in %v", mediaIDCharacters)),
|
2017-05-26 07:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.MediaMetadata.Origin == "" {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: 404,
|
|
|
|
JSON: jsonerror.NotFound("serverName must be a non-empty string"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|