Skip to content

Commit

Permalink
extracted GPS data processing to function in external parser; optimiz…
Browse files Browse the repository at this point in the history
…ed IF in internal parser; removed unnecessary comments; set exact values for positive test
  • Loading branch information
Konstantin Koval committed May 22, 2024
1 parent 945cf6c commit 1c3854d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
47 changes: 26 additions & 21 deletions api/scanner/exif/exif_parser_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ func sanitizeEXIF(exif *models.MediaEXIF) {
}
}

func extractValidGpsData(fileInfo) (GPSLat float, GPSLong float, returnErr error) {
// GPS coordinates - longitude
longitudeRaw, err := fileInfo.GetFloat("GPSLongitude")
if err == nil {
found_exif = true
GPSLong = &longitudeRaw
}

// GPS coordinates - latitude
latitudeRaw, err := fileInfo.GetFloat("GPSLatitude")
if err == nil {
found_exif = true
GPSLat = &latitudeRaw
}

// GPS data validation
if found_exif && (math.Abs(latitudeRaw) > 90 || math.Abs(longitudeRaw) > 90) {
returnErr = errors.New(fmt.Sprintf("Incorrect GPS data in the %s Exif data: %f, %f, while expected values between '-90' and '90'", media_path, longitudeRaw, latitudeRaw))
GPSLat = nil
GPSLong = nil
}
return GPSLat, GPSLong, returnErr
}

func (p *externalExifParser) ParseExif(media_path string) (returnExif *models.MediaEXIF, returnErr error) {
// ExifTool - No print conversion mode
if p.et == nil {
Expand Down Expand Up @@ -184,27 +208,8 @@ func (p *externalExifParser) ParseExif(media_path string) (returnExif *models.Me
newExif.ExposureProgram = &expProgram
}

// GPS coordinates - longitude
longitudeRaw, err := fileInfo.GetFloat("GPSLongitude")
if err == nil {
found_exif = true
newExif.GPSLongitude = &longitudeRaw
}

// GPS coordinates - latitude
latitudeRaw, err := fileInfo.GetFloat("GPSLatitude")
if err == nil {
found_exif = true
newExif.GPSLatitude = &latitudeRaw
}

// **Added GPS data validation**
// https://github.com/photoview/photoview/issues/850
if found_exif && (latitudeRaw < -90 || latitudeRaw > 90 || longitudeRaw < -90 || longitudeRaw > 90) {
returnErr = errors.New(fmt.Sprintf("Incorrect GPS data in the %s Exif data: %f, %f, while expected values between '-90' and '90'", media_path, longitudeRaw, latitudeRaw))
newExif.GPSLatitude = nil
newExif.GPSLongitude = nil
}
// Get GPS data
newExif.GPSLatitude, newExif.GPSLongitude, returnErr := extractValidGpsData(fileInfo)

if !found_exif {
return nil, nil
Expand Down
6 changes: 1 addition & 5 deletions api/scanner/exif/exif_parser_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,8 @@ func (p internalExifParser) ParseExif(media_path string) (returnExif *models.Med

lat, long, err := exifTags.LatLong()
if err == nil {
// **Added GPS data validation**
// https://github.com/photoview/photoview/issues/850
if lat < -90 || lat > 90 || long < -90 || long > 90 {
if math.Abs(lat) > 90 || math.Abs(long) > 90 {
returnErr = errors.New(fmt.Sprintf("Incorrect GPS data in the %s Exif data: %f, %f, while expected values between '-90' and '90'", media_path, long, lat))
newExif.GPSLatitude = nil
newExif.GPSLongitude = nil
} else {
newExif.GPSLatitude = &lat
newExif.GPSLongitude = &long
Expand Down
4 changes: 2 additions & 2 deletions api/scanner/exif/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func TestExifParsers(t *testing.T) {
assert: func(t *testing.T, exif *models.MediaEXIF) {
assert.NotNil(t, exif.GPSLatitude)
assert.NotNil(t, exif.GPSLongitude)
assert.True(t, *exif.GPSLatitude >= -90 && *exif.GPSLatitude <= 90)
assert.True(t, *exif.GPSLongitude >= -90 && *exif.GPSLongitude <= 90)
assert.True(t, *exif.GPSLatitude == 44.4789972222222)
assert.True(t, *exif.GPSLongitude == 11.2979222222222)
},
},
}
Expand Down

0 comments on commit 1c3854d

Please sign in to comment.