From 911a5ffba938ff3b0964789707eb4e940bddc5aa Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 16 Jul 2023 23:04:43 -0600 Subject: [PATCH] Incremental cleanup of legacy code --- .../download_resource_handler.go | 3 +- .../info_controller/info_controller.go | 70 ------------------- types/MAYBE_LEGACY.txt | 0 util/LEGACY_errors.go | 14 ---- 4 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 controllers/info_controller/info_controller.go create mode 100644 types/MAYBE_LEGACY.txt delete mode 100644 util/LEGACY_errors.go diff --git a/controllers/download_controller/download_resource_handler.go b/controllers/download_controller/download_resource_handler.go index b985f19a..fb15be23 100644 --- a/controllers/download_controller/download_resource_handler.go +++ b/controllers/download_controller/download_resource_handler.go @@ -9,7 +9,6 @@ import ( "time" "github.com/getsentry/sentry-go" - "github.com/turt2live/matrix-media-repo/util" "github.com/turt2live/matrix-media-repo/util/stream_util" "github.com/djherbis/stream" @@ -140,7 +139,7 @@ func downloadResourceWorkFn(request *resource_handler.WorkRequest) (resp *worker resp.filename = "" resp.contentType = "" resp.media = nil - resp.err = util.PanicToError(err) + resp.err = nil } }() diff --git a/controllers/info_controller/info_controller.go b/controllers/info_controller/info_controller.go deleted file mode 100644 index 98eb0969..00000000 --- a/controllers/info_controller/info_controller.go +++ /dev/null @@ -1,70 +0,0 @@ -package info_controller - -import ( - "bytes" - "image/png" - - "github.com/buckket/go-blurhash" - "github.com/disintegration/imaging" - "github.com/turt2live/matrix-media-repo/common/rcontext" - "github.com/turt2live/matrix-media-repo/controllers/download_controller" - "github.com/turt2live/matrix-media-repo/storage" - "github.com/turt2live/matrix-media-repo/types" - "github.com/turt2live/matrix-media-repo/util/stream_util" -) - -func GetOrCalculateBlurhash(media *types.Media, rctx rcontext.RequestContext) (string, error) { - rctx.Log.Info("Attempting fetch of blurhash for sha256 of " + media.Sha256Hash) - db := storage.GetDatabase().GetMetadataStore(rctx) - cached, err := db.GetBlurhash(media.Sha256Hash) - if err != nil { - return "", err - } - - if cached != "" { - rctx.Log.Info("Returning cached blurhash: " + cached) - return cached, nil - } - - rctx.Log.Info("Getting minimal media record to calculate blurhash") - minMedia, err := download_controller.FindMinimalMediaRecord(media.Origin, media.MediaId, true, rctx) - if err != nil { - return "", err - } - defer stream_util.DumpAndCloseStream(minMedia.Stream) - - // No cached blurhash: calculate one - rctx.Log.Info("Decoding image for blurhash calculation") - imgSrc, err := imaging.Decode(minMedia.Stream) - if err != nil { - return "", err - } - - // Resize the image to make the blurhash a bit more reasonable to calculate - rctx.Log.Info("Resizing image for blurhash (faster calculation)") - smallImg := imaging.Fill(imgSrc, rctx.Config.Features.MSC2448Blurhash.GenerateWidth, rctx.Config.Features.MSC2448Blurhash.GenerateHeight, imaging.Center, imaging.Lanczos) - imgBuf := &bytes.Buffer{} - err = imaging.Encode(imgBuf, smallImg, imaging.PNG) - if err != nil { - return "", err - } - decoded, err := png.Decode(imgBuf) - if err != nil { - return "", err - } - - rctx.Log.Info("Calculating blurhash") - encoded, err := blurhash.Encode(rctx.Config.Features.MSC2448Blurhash.XComponents, rctx.Config.Features.MSC2448Blurhash.YComponents, decoded) - if err != nil { - return "", err - } - - // Save the blurhash for next time - rctx.Log.Infof("Saving blurhash %s and returning", encoded) - err = db.InsertBlurhash(media.Sha256Hash, encoded) - if err != nil { - return "", err - } - - return encoded, nil -} diff --git a/types/MAYBE_LEGACY.txt b/types/MAYBE_LEGACY.txt new file mode 100644 index 00000000..e69de29b diff --git a/util/LEGACY_errors.go b/util/LEGACY_errors.go deleted file mode 100644 index 8393f82b..00000000 --- a/util/LEGACY_errors.go +++ /dev/null @@ -1,14 +0,0 @@ -package util - -import "errors" - -func PanicToError(err interface{}) error { - switch x := err.(type) { - case string: - return errors.New(x) - case error: - return x - default: - return errors.New("unknown panic") - } -} -- GitLab