mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:59:03 +00:00
Validate incoming GPS data (#951)
* Validate incoming GPS data and throw an error if it is incorrect, storing Null values * Extracted GPS data processing to function in external parser; optimized IF in internal parser; removed unnecessary comments; set exact values for positive test * Add the migration for removing existing invalid GPS data and its test; added better errors to asserts in the GPS validation test * Install FFmpeg and ExifTool on the API unit-test environment * Fix 'stripped.jpg', 'IncorrectGPS.jpg', and 'CorrectGPS.jpg' tests for the external parser * Optimized data validation in the external parser, returned error by the internal parser for invalid data, updated test to expect errors and handle them * Switched from error to log entry in case of incorrect GPS data, as error handling is not so transparent in the internal parser --------- Co-authored-by: Konstantin Koval
This commit is contained in:
@@ -63,6 +63,31 @@ func sanitizeEXIF(exif *models.MediaEXIF) {
|
||||
}
|
||||
}
|
||||
|
||||
func extractValidGpsData(fileInfo *exiftool.FileMetadata, media_path string) (*float64, *float64) {
|
||||
var GPSLat, GPSLong *float64
|
||||
|
||||
// GPS coordinates - longitude
|
||||
longitudeRaw, err := fileInfo.GetFloat("GPSLongitude")
|
||||
if err == nil {
|
||||
GPSLong = &longitudeRaw
|
||||
}
|
||||
|
||||
// GPS coordinates - latitude
|
||||
latitudeRaw, err := fileInfo.GetFloat("GPSLatitude")
|
||||
if err == nil {
|
||||
GPSLat = &latitudeRaw
|
||||
}
|
||||
|
||||
// GPS data validation
|
||||
if (GPSLat != nil && math.Abs(*GPSLat) > 90) || (GPSLong != nil && math.Abs(*GPSLong) > 90) {
|
||||
log.Printf(
|
||||
"Incorrect GPS data in the %s Exif data: %f, %f, while expected values between '-90' and '90'. Ignoring GPS data.",
|
||||
media_path, *GPSLat, *GPSLong)
|
||||
return nil, nil
|
||||
}
|
||||
return GPSLat, GPSLong
|
||||
}
|
||||
|
||||
func (p *externalExifParser) ParseExif(media_path string) (returnExif *models.MediaEXIF, returnErr error) {
|
||||
// ExifTool - No print conversion mode
|
||||
if p.et == nil {
|
||||
@@ -182,18 +207,10 @@ func (p *externalExifParser) ParseExif(media_path string) (returnExif *models.Me
|
||||
newExif.ExposureProgram = &expProgram
|
||||
}
|
||||
|
||||
// GPS coordinates - longitude
|
||||
longitudeRaw, err := fileInfo.GetFloat("GPSLongitude")
|
||||
if err == nil {
|
||||
// Get GPS data
|
||||
newExif.GPSLatitude, newExif.GPSLongitude = extractValidGpsData(&fileInfo, media_path)
|
||||
if (newExif.GPSLatitude != nil) && (newExif.GPSLongitude != nil) {
|
||||
found_exif = true
|
||||
newExif.GPSLongitude = &longitudeRaw
|
||||
}
|
||||
|
||||
// GPS coordinates - latitude
|
||||
latitudeRaw, err := fileInfo.GetFloat("GPSLatitude")
|
||||
if err == nil {
|
||||
found_exif = true
|
||||
newExif.GPSLatitude = &latitudeRaw
|
||||
}
|
||||
|
||||
if !found_exif {
|
||||
|
||||
Reference in New Issue
Block a user