Skip to content
Snippets Groups Projects
Commit 911a5ffb authored by Travis Ralston's avatar Travis Ralston
Browse files

Incremental cleanup of legacy code

parent ea173167
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/turt2live/matrix-media-repo/util"
"github.com/turt2live/matrix-media-repo/util/stream_util" "github.com/turt2live/matrix-media-repo/util/stream_util"
"github.com/djherbis/stream" "github.com/djherbis/stream"
...@@ -140,7 +139,7 @@ func downloadResourceWorkFn(request *resource_handler.WorkRequest) (resp *worker ...@@ -140,7 +139,7 @@ func downloadResourceWorkFn(request *resource_handler.WorkRequest) (resp *worker
resp.filename = "" resp.filename = ""
resp.contentType = "" resp.contentType = ""
resp.media = nil resp.media = nil
resp.err = util.PanicToError(err) resp.err = nil
} }
}() }()
......
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
}
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")
}
}
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