routing.go 3.0 KB
Newer Older
R
Robert Swain 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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 routing

import (
	"net/http"

20 21
	"github.com/matrix-org/dendrite/clientapi/auth/authtypes"

R
Robert Swain 已提交
22
	"github.com/gorilla/mux"
23
	"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
24
	"github.com/matrix-org/dendrite/common"
25
	"github.com/matrix-org/dendrite/common/config"
26
	"github.com/matrix-org/dendrite/mediaapi/storage"
R
Robert Swain 已提交
27 28 29 30 31 32 33 34
	"github.com/matrix-org/dendrite/mediaapi/types"
	"github.com/matrix-org/gomatrixserverlib"
	"github.com/matrix-org/util"
	"github.com/prometheus/client_golang/prometheus"
)

const pathPrefixR0 = "/_matrix/media/v1"

35
// Setup registers the media API HTTP handlers
36 37 38 39
func Setup(
	apiMux *mux.Router,
	cfg *config.Dendrite,
	db *storage.Database,
40
	deviceDB *devices.Database,
41 42
	client *gomatrixserverlib.Client,
) {
R
Robert Swain 已提交
43
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
44 45 46 47 48

	activeThumbnailGeneration := &types.ActiveThumbnailGeneration{
		PathToResult: map[string]*types.ThumbnailGenerationResult{},
	}

49 50 51 52
	r0mux.Handle("/upload", common.MakeAuthAPI(
		"upload",
		deviceDB,
		func(req *http.Request, _ *authtypes.Device) util.JSONResponse {
53
			return Upload(req, cfg, db, activeThumbnailGeneration)
54 55
		},
	)).Methods("POST", "OPTIONS")
R
Robert Swain 已提交
56 57

	activeRemoteRequests := &types.ActiveRemoteRequests{
58
		MXCToResult: map[string]*types.RemoteRequestResult{},
R
Robert Swain 已提交
59 60
	}
	r0mux.Handle("/download/{serverName}/{mediaId}",
61
		makeDownloadAPI("download", cfg, db, client, activeRemoteRequests, activeThumbnailGeneration),
62
	).Methods("GET")
63
	r0mux.Handle("/thumbnail/{serverName}/{mediaId}",
64
		makeDownloadAPI("thumbnail", cfg, db, client, activeRemoteRequests, activeThumbnailGeneration),
65
	).Methods("GET")
R
Robert Swain 已提交
66
}
67

68 69 70 71 72 73 74 75
func makeDownloadAPI(
	name string,
	cfg *config.Dendrite,
	db *storage.Database,
	client *gomatrixserverlib.Client,
	activeRemoteRequests *types.ActiveRemoteRequests,
	activeThumbnailGeneration *types.ActiveThumbnailGeneration,
) http.HandlerFunc {
76 77 78 79 80 81 82 83 84
	return prometheus.InstrumentHandler(name, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req = util.RequestWithLogging(req)

		// Set common headers returned regardless of the outcome of the request
		util.SetCORSHeaders(w)
		// Content-Type will be overridden in case of returning file data, else we respond with JSON-formatted errors
		w.Header().Set("Content-Type", "application/json")

		vars := mux.Vars(req)
85
		Download(
86 87 88 89 90 91 92 93 94 95 96
			w,
			req,
			gomatrixserverlib.ServerName(vars["serverName"]),
			types.MediaID(vars["mediaId"]),
			cfg,
			db,
			client,
			activeRemoteRequests,
			activeThumbnailGeneration,
			name == "thumbnail",
		)
97 98
	}))
}