Skip to content
Snippets Groups Projects
Unverified Commit 28625a48 authored by Travis Ralston's avatar Travis Ralston Committed by GitHub
Browse files

Merge pull request #327 from t3chguy/msc2448

Fix MSC2448 implementation
parents 47226a30 fe06d9ce
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,6 @@ import ( ...@@ -4,9 +4,6 @@ import (
"net/http" "net/http"
) )
const MSC2448UploadRoute = "/_matrix/media/unstable/xyz.amorgan/upload"
const MSC2448GetHashRoute = "/_matrix/media/unstable/xyz.amorgan/download/{server:[a-zA-Z0-9.:\\-_]+}/{mediaId:[^/]+}/blurhash"
const MSC2448AltRenderRoute = "/_matrix/media/unstable/io.t2bot.msc2448/blurhash/{blurhash:[^/]+}"
const IPFSDownloadRoute = "/_matrix/media/unstable/io.t2bot.ipfs/download/{server:[a-zA-Z0-9.:\\-_]+}/{ipfsContentId:[^/]+}" const IPFSDownloadRoute = "/_matrix/media/unstable/io.t2bot.ipfs/download/{server:[a-zA-Z0-9.:\\-_]+}/{ipfsContentId:[^/]+}"
const IPFSLiveDownloadRouteR0 = "/_matrix/media/r0/download/{server:[a-zA-Z0-9.:\\-_]+}/ipfs:{ipfsContentId:[^/]+}" const IPFSLiveDownloadRouteR0 = "/_matrix/media/r0/download/{server:[a-zA-Z0-9.:\\-_]+}/ipfs:{ipfsContentId:[^/]+}"
const IPFSLiveDownloadRouteV1 = "/_matrix/media/v1/download/{server:[a-zA-Z0-9.:\\-_]+}/ipfs:{ipfsContentId:[^/]+}" const IPFSLiveDownloadRouteV1 = "/_matrix/media/v1/download/{server:[a-zA-Z0-9.:\\-_]+}/ipfs:{ipfsContentId:[^/]+}"
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/api" "github.com/turt2live/matrix-media-repo/api"
"github.com/turt2live/matrix-media-repo/api/features"
"github.com/turt2live/matrix-media-repo/common" "github.com/turt2live/matrix-media-repo/common"
"github.com/turt2live/matrix-media-repo/common/rcontext" "github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/controllers/info_controller" "github.com/turt2live/matrix-media-repo/controllers/info_controller"
...@@ -20,7 +19,7 @@ import ( ...@@ -20,7 +19,7 @@ import (
type MediaUploadedResponse struct { type MediaUploadedResponse struct {
ContentUri string `json:"content_uri"` ContentUri string `json:"content_uri"`
Blurhash string `json:"blurhash,omitempty"` Blurhash string `json:"xyz.amorgan.blurhash,omitempty"`
} }
func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} { func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} {
...@@ -73,7 +72,7 @@ func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInf ...@@ -73,7 +72,7 @@ func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInf
return api.InternalServerError("Unexpected Error") return api.InternalServerError("Unexpected Error")
} }
if rctx.Config.Features.MSC2448Blurhash.Enabled && features.IsRoute(r, features.MSC2448UploadRoute) { if rctx.Config.Features.MSC2448Blurhash.Enabled {
hash, err := info_controller.GetOrCalculateBlurhash(media, rctx) hash, err := info_controller.GetOrCalculateBlurhash(media, rctx)
if err != nil { if err != nil {
rctx.Log.Warn("Failed to calculate blurhash: " + err.Error()) rctx.Log.Warn("Failed to calculate blurhash: " + err.Error())
......
package unstable
import (
"github.com/getsentry/sentry-go"
"net/http"
"github.com/gorilla/mux"
"github.com/turt2live/matrix-media-repo/api"
"github.com/turt2live/matrix-media-repo/common"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/controllers/download_controller"
"github.com/turt2live/matrix-media-repo/controllers/info_controller"
)
func GetBlurhash(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} {
params := mux.Vars(r)
server := params["server"]
mediaId := params["mediaId"]
m, err := download_controller.FindMediaRecord(server, mediaId, true, rctx)
if err != nil {
if err == common.ErrMediaNotFound {
return api.NotFoundError()
} else if err == common.ErrMediaTooLarge {
return api.RequestTooLarge()
} else if err == common.ErrMediaQuarantined {
return api.NotFoundError() // We lie for security
}
rctx.Log.Error("Unexpected error locating media: " + err.Error())
sentry.CaptureException(err)
return api.InternalServerError("Unexpected Error")
}
hash, err := info_controller.GetOrCalculateBlurhash(m, rctx)
if err != nil {
rctx.Log.Error(err)
sentry.CaptureException(err)
return api.InternalServerError("Unexpected Error")
}
return &map[string]string{"blurhash": hash}
}
package unstable
import (
"bytes"
"github.com/getsentry/sentry-go"
"image/png"
"net/http"
"strconv"
"github.com/buckket/go-blurhash"
"github.com/gorilla/mux"
"github.com/turt2live/matrix-media-repo/api"
"github.com/turt2live/matrix-media-repo/api/r0"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/util"
)
func RenderBlurhash(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} {
params := mux.Vars(r)
hash := params["blurhash"]
width := rctx.Config.Features.MSC2448Blurhash.MaxRenderWidth / 2
height := rctx.Config.Features.MSC2448Blurhash.MaxRenderHeight / 2
wstr := r.URL.Query().Get("width")
hstr := r.URL.Query().Get("height")
if wstr != "" {
i, err := strconv.Atoi(wstr)
if err != nil {
return api.BadRequest("width must be an integer")
}
width = i
}
if hstr != "" {
i, err := strconv.Atoi(hstr)
if err != nil {
return api.BadRequest("height must be an integer")
}
height = i
}
if width > rctx.Config.Features.MSC2448Blurhash.MaxRenderWidth {
width = rctx.Config.Features.MSC2448Blurhash.MaxRenderWidth
}
if height > rctx.Config.Features.MSC2448Blurhash.MaxRenderHeight {
height = rctx.Config.Features.MSC2448Blurhash.MaxRenderHeight
}
img, err := blurhash.Decode(hash, width, height, rctx.Config.Features.MSC2448Blurhash.Punch)
if err != nil {
rctx.Log.Error(err)
sentry.CaptureException(err)
return api.InternalServerError("Unexpected error rendering blurhash")
}
buf := &bytes.Buffer{}
err = png.Encode(buf, img)
if err != nil {
rctx.Log.Error(err)
sentry.CaptureException(err)
return api.InternalServerError("Unexpected error rendering blurhash")
}
return &r0.DownloadMediaResponse{
ContentType: "image/png",
Filename: "blurhash.png",
SizeBytes: int64(buf.Len()),
Data: util.BufferToStream(buf), // convert to stream to avoid console spam
TargetDisposition: "inline",
}
}
...@@ -79,8 +79,6 @@ func Init() *sync.WaitGroup { ...@@ -79,8 +79,6 @@ func Init() *sync.WaitGroup {
appendToImportHandler := handler{api.RepoAdminRoute(custom.AppendToImport), "append_to_import", counter, false} appendToImportHandler := handler{api.RepoAdminRoute(custom.AppendToImport), "append_to_import", counter, false}
stopImportHandler := handler{api.RepoAdminRoute(custom.StopImport), "stop_import", counter, false} stopImportHandler := handler{api.RepoAdminRoute(custom.StopImport), "stop_import", counter, false}
versionHandler := handler{api.AccessTokenOptionalRoute(custom.GetVersion), "get_version", counter, false} versionHandler := handler{api.AccessTokenOptionalRoute(custom.GetVersion), "get_version", counter, false}
blurhashRenderHandler := handler{api.AccessTokenRequiredRoute(unstable.RenderBlurhash), "render_blurhash", counter, false}
blurhashCalcHandler := handler{api.AccessTokenRequiredRoute(unstable.GetBlurhash), "calculate_blurhash", counter, false}
ipfsDownloadHandler := handler{api.AccessTokenOptionalRoute(unstable.IPFSDownload), "ipfs_download", counter, false} ipfsDownloadHandler := handler{api.AccessTokenOptionalRoute(unstable.IPFSDownload), "ipfs_download", counter, false}
logoutHandler := handler{api.AccessTokenRequiredRoute(r0.Logout), "logout", counter, false} logoutHandler := handler{api.AccessTokenRequiredRoute(r0.Logout), "logout", counter, false}
logoutAllHandler := handler{api.AccessTokenRequiredRoute(r0.LogoutAll), "logout_all", counter, false} logoutAllHandler := handler{api.AccessTokenRequiredRoute(r0.LogoutAll), "logout_all", counter, false}
...@@ -154,12 +152,6 @@ func Init() *sync.WaitGroup { ...@@ -154,12 +152,6 @@ func Init() *sync.WaitGroup {
} }
} }
if config.Get().Features.MSC2448Blurhash.Enabled {
routes[features.MSC2448UploadRoute] = route{"POST", uploadHandler}
routes[features.MSC2448GetHashRoute] = route{"GET", blurhashCalcHandler}
routes[features.MSC2448AltRenderRoute] = route{"GET", blurhashRenderHandler}
}
if config.Get().Features.IPFS.Enabled { if config.Get().Features.IPFS.Enabled {
routes[features.IPFSDownloadRoute] = route{"GET", ipfsDownloadHandler} routes[features.IPFSDownloadRoute] = route{"GET", ipfsDownloadHandler}
routes[features.IPFSLiveDownloadRouteR0] = route{"GET", ipfsDownloadHandler} routes[features.IPFSLiveDownloadRouteR0] = route{"GET", ipfsDownloadHandler}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment