mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 07:23:10 -06:00
mediaapi/thumbnailer: Rename metrics to fitness
Metrics is used in the context of monitoring with Prometheus so renaming to avoid confusion.
This commit is contained in:
parent
7af3bdf04a
commit
2cd886656f
|
|
@ -28,7 +28,7 @@ import (
|
||||||
"gopkg.in/h2non/bimg.v1"
|
"gopkg.in/h2non/bimg.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type thumbnailMetrics struct {
|
type thumbnailFitness struct {
|
||||||
isSmaller int
|
isSmaller int
|
||||||
aspect float64
|
aspect float64
|
||||||
size float64
|
size float64
|
||||||
|
|
@ -97,15 +97,15 @@ func GetThumbnailPath(src types.Path, config types.ThumbnailSize) types.Path {
|
||||||
func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailMetadata, thumbnailSizes []types.ThumbnailSize) (*types.ThumbnailMetadata, *types.ThumbnailSize) {
|
func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailMetadata, thumbnailSizes []types.ThumbnailSize) (*types.ThumbnailMetadata, *types.ThumbnailSize) {
|
||||||
var chosenThumbnail *types.ThumbnailMetadata
|
var chosenThumbnail *types.ThumbnailMetadata
|
||||||
var chosenThumbnailSize *types.ThumbnailSize
|
var chosenThumbnailSize *types.ThumbnailSize
|
||||||
chosenMetrics := newThumbnailMetrics()
|
bestFit := newThumbnailFitness()
|
||||||
|
|
||||||
for _, thumbnail := range thumbnails {
|
for _, thumbnail := range thumbnails {
|
||||||
if desired.ResizeMethod == "scale" && thumbnail.ThumbnailSize.ResizeMethod != "scale" {
|
if desired.ResizeMethod == "scale" && thumbnail.ThumbnailSize.ResizeMethod != "scale" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
metrics := calcThumbnailMetrics(thumbnail.ThumbnailSize, thumbnail.MediaMetadata, desired)
|
fitness := calcThumbnailFitness(thumbnail.ThumbnailSize, thumbnail.MediaMetadata, desired)
|
||||||
if isBetter := metrics.betterThan(chosenMetrics, desired.ResizeMethod == "crop"); isBetter {
|
if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == "crop"); isBetter {
|
||||||
chosenMetrics = metrics
|
bestFit = fitness
|
||||||
chosenThumbnail = thumbnail
|
chosenThumbnail = thumbnail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -114,9 +114,9 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM
|
||||||
if desired.ResizeMethod == "scale" && thumbnailSize.ResizeMethod != "scale" {
|
if desired.ResizeMethod == "scale" && thumbnailSize.ResizeMethod != "scale" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
metrics := calcThumbnailMetrics(thumbnailSize, nil, desired)
|
fitness := calcThumbnailFitness(thumbnailSize, nil, desired)
|
||||||
if isBetter := metrics.betterThan(chosenMetrics, desired.ResizeMethod == "crop"); isBetter {
|
if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == "crop"); isBetter {
|
||||||
chosenMetrics = metrics
|
bestFit = fitness
|
||||||
chosenThumbnailSize = &types.ThumbnailSize{
|
chosenThumbnailSize = &types.ThumbnailSize{
|
||||||
Width: thumbnailSize.Width,
|
Width: thumbnailSize.Width,
|
||||||
Height: thumbnailSize.Height,
|
Height: thumbnailSize.Height,
|
||||||
|
|
@ -307,8 +307,8 @@ func resize(dst types.Path, buffer []byte, w, h int, crop bool, logger *log.Entr
|
||||||
}
|
}
|
||||||
|
|
||||||
// init with worst values
|
// init with worst values
|
||||||
func newThumbnailMetrics() thumbnailMetrics {
|
func newThumbnailFitness() thumbnailFitness {
|
||||||
return thumbnailMetrics{
|
return thumbnailFitness{
|
||||||
isSmaller: 1,
|
isSmaller: 1,
|
||||||
aspect: float64(16384 * 16384),
|
aspect: float64(16384 * 16384),
|
||||||
size: float64(16384 * 16384),
|
size: float64(16384 * 16384),
|
||||||
|
|
@ -317,28 +317,28 @@ func newThumbnailMetrics() thumbnailMetrics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcThumbnailMetrics(size types.ThumbnailSize, metadata *types.MediaMetadata, desired types.ThumbnailSize) thumbnailMetrics {
|
func calcThumbnailFitness(size types.ThumbnailSize, metadata *types.MediaMetadata, desired types.ThumbnailSize) thumbnailFitness {
|
||||||
dW := desired.Width
|
dW := desired.Width
|
||||||
dH := desired.Height
|
dH := desired.Height
|
||||||
tW := size.Width
|
tW := size.Width
|
||||||
tH := size.Height
|
tH := size.Height
|
||||||
|
|
||||||
metrics := newThumbnailMetrics()
|
fitness := newThumbnailFitness()
|
||||||
// In all cases, a larger metric value is a worse fit.
|
// In all cases, a larger metric value is a worse fit.
|
||||||
// compare size: thumbnail smaller is true and gives 1, larger is false and gives 0
|
// compare size: thumbnail smaller is true and gives 1, larger is false and gives 0
|
||||||
metrics.isSmaller = boolToInt(tW < dW || tH < dH)
|
fitness.isSmaller = boolToInt(tW < dW || tH < dH)
|
||||||
// comparison of aspect ratios only makes sense for a request for desired cropped
|
// comparison of aspect ratios only makes sense for a request for desired cropped
|
||||||
metrics.aspect = math.Abs(float64(dW*tH - dH*tW))
|
fitness.aspect = math.Abs(float64(dW*tH - dH*tW))
|
||||||
// compare sizes
|
// compare sizes
|
||||||
metrics.size = math.Abs(float64((dW - tW) * (dH - tH)))
|
fitness.size = math.Abs(float64((dW - tW) * (dH - tH)))
|
||||||
// compare resize method
|
// compare resize method
|
||||||
metrics.methodMismatch = boolToInt(size.ResizeMethod != desired.ResizeMethod)
|
fitness.methodMismatch = boolToInt(size.ResizeMethod != desired.ResizeMethod)
|
||||||
if metadata != nil {
|
if metadata != nil {
|
||||||
// file size
|
// file size
|
||||||
metrics.fileSize = metadata.FileSizeBytes
|
fitness.fileSize = metadata.FileSizeBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
return metrics
|
return fitness
|
||||||
}
|
}
|
||||||
|
|
||||||
func boolToInt(b bool) int {
|
func boolToInt(b bool) int {
|
||||||
|
|
@ -348,7 +348,7 @@ func boolToInt(b bool) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a thumbnailMetrics) betterThan(b thumbnailMetrics, desiredCrop bool) bool {
|
func (a thumbnailFitness) betterThan(b thumbnailFitness, desiredCrop bool) bool {
|
||||||
// preference means returning -1
|
// preference means returning -1
|
||||||
|
|
||||||
// prefer images that are not smaller
|
// prefer images that are not smaller
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue