From ac347540b8f3ec0947f77e4be4f68edf9ca05b02 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 21 Jan 2025 12:27:30 +0800 Subject: [PATCH] :bug: Analyzer now can handle EXIF Orientation --- pkg/internal/services/analyzer.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index 5bf3e9c..9513c6f 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -99,6 +99,13 @@ func ScanUnanalyzedFileFromDatabase() { }() } +func calculateAspectRatio(width, height int, orientation int) float64 { + if orientation == 6 || orientation == 8 { + width, height = height, width + } + return float64(width) / float64(height) +} + func AnalyzeAttachment(file models.Attachment) error { if file.Destination != models.AttachmentDstTemporary { return fmt.Errorf("attachment isn't in temporary storage, unable to analyze") @@ -133,7 +140,7 @@ func AnalyzeAttachment(file models.Attachment) error { "Model", "ShutterSpeed", "ISO", "Megapixels", "Aperture", "ColorSpace", "ColorTemperature", "ColorTone", "Contrast", "ExposureTime", "FNumber", "FocalLength", "Flash", "HDREffect", - "LensModel", + "LensModel", "Orientation", } switch strings.SplitN(file.MimeType, "/", 2)[0] { @@ -165,6 +172,9 @@ func AnalyzeAttachment(file models.Attachment) error { exif := et.ExtractMetadata(dst) for _, data := range exif { for k := range data.Fields { + if k == "Orientation" { + file.Metadata["ratio"] = calculateAspectRatio(width, height, data.Fields[k].(int)) + } if strings.HasPrefix(k, "GPS") { data.Clear(k) } else if lo.Contains(exifWhitelist, k) { @@ -206,6 +216,9 @@ func AnalyzeAttachment(file models.Attachment) error { exif := et.ExtractMetadata(dst) for _, data := range exif { for k := range data.Fields { + if k == "Orientation" { + file.Metadata["ratio"] = calculateAspectRatio(stream.Width, stream.Height, data.Fields[k].(int)) + } if strings.HasPrefix(k, "GPS") { data.Clear(k) } else if lo.Contains(exifWhitelist, k) {