From fe06d9ceec10f9767504339a3ccf84fccd10ced8 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Sat, 22 May 2021 00:48:22 +0100
Subject: [PATCH] Fix MSC2448 implementation

---
 api/features/feature_routes.go  |  3 --
 api/r0/upload.go                |  5 +--
 api/unstable/blurhash_get.go    | 43 --------------------
 api/unstable/blurhash_render.go | 72 ---------------------------------
 api/webserver/webserver.go      |  8 ----
 5 files changed, 2 insertions(+), 129 deletions(-)
 delete mode 100644 api/unstable/blurhash_get.go
 delete mode 100644 api/unstable/blurhash_render.go

diff --git a/api/features/feature_routes.go b/api/features/feature_routes.go
index 100fbd46..442d251b 100644
--- a/api/features/feature_routes.go
+++ b/api/features/feature_routes.go
@@ -4,9 +4,6 @@ import (
 	"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 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:[^/]+}"
diff --git a/api/r0/upload.go b/api/r0/upload.go
index 14445cc0..4153bca4 100644
--- a/api/r0/upload.go
+++ b/api/r0/upload.go
@@ -9,7 +9,6 @@ import (
 
 	"github.com/sirupsen/logrus"
 	"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/rcontext"
 	"github.com/turt2live/matrix-media-repo/controllers/info_controller"
@@ -20,7 +19,7 @@ import (
 
 type MediaUploadedResponse struct {
 	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{} {
@@ -73,7 +72,7 @@ func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInf
 		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)
 		if err != nil {
 			rctx.Log.Warn("Failed to calculate blurhash: " + err.Error())
diff --git a/api/unstable/blurhash_get.go b/api/unstable/blurhash_get.go
deleted file mode 100644
index ad1ad901..00000000
--- a/api/unstable/blurhash_get.go
+++ /dev/null
@@ -1,43 +0,0 @@
-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}
-}
diff --git a/api/unstable/blurhash_render.go b/api/unstable/blurhash_render.go
deleted file mode 100644
index 5035b26b..00000000
--- a/api/unstable/blurhash_render.go
+++ /dev/null
@@ -1,72 +0,0 @@
-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",
-	}
-}
diff --git a/api/webserver/webserver.go b/api/webserver/webserver.go
index cdc7b0e7..ca4f85ef 100644
--- a/api/webserver/webserver.go
+++ b/api/webserver/webserver.go
@@ -79,8 +79,6 @@ func Init() *sync.WaitGroup {
 	appendToImportHandler := handler{api.RepoAdminRoute(custom.AppendToImport), "append_to_import", counter, false}
 	stopImportHandler := handler{api.RepoAdminRoute(custom.StopImport), "stop_import", 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}
 	logoutHandler := handler{api.AccessTokenRequiredRoute(r0.Logout), "logout", counter, false}
 	logoutAllHandler := handler{api.AccessTokenRequiredRoute(r0.LogoutAll), "logout_all", counter, false}
@@ -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 {
 		routes[features.IPFSDownloadRoute] = route{"GET", ipfsDownloadHandler}
 		routes[features.IPFSLiveDownloadRouteR0] = route{"GET", ipfsDownloadHandler}
-- 
GitLab