diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index ef18bfc..9272721 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -99,8 +99,21 @@ func ScanUnanalyzedFileFromDatabase() { }() } -func calculateAspectRatio(width, height int, orientation string) float64 { - if orientation == "6" || orientation == "8" { +func parseExifOrientation(output string) (int, error) { + parts := strings.Fields(output) + if len(parts) < 2 { + return 0, fmt.Errorf("unexpected format: %s", output) + } + orientation, err := strconv.Atoi(parts[len(parts)-1]) + if err != nil { + return 0, fmt.Errorf("invalid orientation value: %s", parts[len(parts)-1]) + } + return orientation, nil +} + +func calculateAspectRatio(width, height int, orientation int) float64 { + switch orientation { + case 5, 6, 7, 8: width, height = height, width } return float64(width) / float64(height) @@ -173,7 +186,8 @@ func AnalyzeAttachment(file models.Attachment) error { for _, data := range exif { for k := range data.Fields { if k == "Orientation" { - file.Metadata["ratio"] = calculateAspectRatio(width, height, data.Fields[k].(string)) + ori, _ := parseExifOrientation(data.Fields[k].(string)) + file.Metadata["ratio"] = calculateAspectRatio(width, height, ori) } if strings.HasPrefix(k, "GPS") { data.Clear(k) @@ -217,7 +231,8 @@ func AnalyzeAttachment(file models.Attachment) error { for _, data := range exif { for k := range data.Fields { if k == "Orientation" { - file.Metadata["ratio"] = calculateAspectRatio(stream.Width, stream.Height, data.Fields[k].(string)) + ori, _ := parseExifOrientation(data.Fields[k].(string)) + file.Metadata["ratio"] = calculateAspectRatio(stream.Width, stream.Height, ori) } if strings.HasPrefix(k, "GPS") { data.Clear(k)