aboutsummaryrefslogtreecommitdiff
path: root/pkg/fileop/exif.go
blob: 49cd0ce96fef9742223fef099298ae62a13b9ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package fileop

import (
	"math"
	"time"

	"github.com/barasher/go-exiftool"

	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
)

func ReadExif(path string) (*repository.MediaEXIF, error) {
	et, err := exiftool.NewExiftool()
	if err != nil {
		return nil, err
	}
	defer et.Close()

	newExif := &repository.MediaEXIF{}
	fileInfo := et.ExtractMetadata(path)[0]

	// Get description
	description, err := fileInfo.GetString("ImageDescription")
	if err == nil {
		newExif.Description = &description
	}

	// Get camera model
	model, err := fileInfo.GetString("Model")
	if err == nil {
		newExif.Camera = &model
	}

	// Get Camera make
	make, err := fileInfo.GetString("Make")
	if err == nil {
		newExif.Maker = &make
	}

	// Get Image Size
	width, err := fileInfo.GetFloat("ImageWidth")
	if err == nil {
		newExif.Width = &width
	}

	height, err := fileInfo.GetFloat("ImageHeight")
	if err == nil {
		newExif.Height = &height
	}

	// Get lens
	lens, err := fileInfo.GetString("LensModel")
	if err == nil {
		newExif.Lens = &lens
	}

	//Get time of photo
	createDateKeys := []string{
		"CreationDate",
		"DateTimeOriginal",
		"CreateDate",
		"TrackCreateDate",
		"MediaCreateDate",
		"FileCreateDate",
		"ModifyDate",
		"TrackModifyDate",
		"MediaModifyDate",
		"FileModifyDate",
	}
	for _, createDateKey := range createDateKeys {
		date, err := fileInfo.GetString(createDateKey)
		if err == nil {
			layout := "2006:01:02 15:04:05"
			dateTime, err := time.Parse(layout, date)
			if err == nil {
				newExif.DateShot = &dateTime
			} else {
				layoutWithOffset := "2006:01:02 15:04:05+02:00"
				dateTime, err = time.Parse(layoutWithOffset, date)
				if err == nil {
					newExif.DateShot = &dateTime
				}
			}
			break
		}
	}

	// Get exposure time
	exposureTime, err := fileInfo.GetFloat("ExposureTime")
	if err == nil {
		newExif.Exposure = &exposureTime
	}

	// Get aperture
	aperture, err := fileInfo.GetFloat("Aperture")
	if err == nil {
		newExif.Aperture = &aperture
	}

	// Get ISO
	iso, err := fileInfo.GetInt("ISO")
	if err == nil {
		newExif.Iso = &iso
	}

	// Get focal length
	focalLen, err := fileInfo.GetFloat("FocalLength")
	if err == nil {
		newExif.FocalLength = &focalLen
	}

	// Get flash info
	flash, err := fileInfo.GetInt("Flash")
	if err == nil {
		newExif.Flash = &flash
	}

	// Get orientation
	orientation, err := fileInfo.GetInt("Orientation")
	if err == nil {
		newExif.Orientation = &orientation
	}

	// Get exposure program
	expProgram, err := fileInfo.GetInt("ExposureProgram")
	if err == nil {
		newExif.ExposureProgram = &expProgram
	}

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

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

	sanitizeEXIF(newExif)

	return newExif, nil
}

// isFloatReal returns true when the float value represents a real number
// (different than +Inf, -Inf or NaN)
func isFloatReal(v float64) bool {
	if math.IsInf(v, 1) {
		return false
	} else if math.IsInf(v, -1) {
		return false
	} else if math.IsNaN(v) {
		return false
	}
	return true
}

// sanitizeEXIF removes any EXIF float64 field that is not a real number (+Inf,
// -Inf or Nan)
func sanitizeEXIF(exif *repository.MediaEXIF) {
	if exif.Exposure != nil && !isFloatReal(*exif.Exposure) {
		exif.Exposure = nil
	}
	if exif.Aperture != nil && !isFloatReal(*exif.Aperture) {
		exif.Aperture = nil
	}
	if exif.FocalLength != nil && !isFloatReal(*exif.FocalLength) {
		exif.FocalLength = nil
	}
	if (exif.GPSLatitude != nil && !isFloatReal(*exif.GPSLatitude)) ||
		(exif.GPSLongitude != nil && !isFloatReal(*exif.GPSLongitude)) {
		exif.GPSLatitude = nil
		exif.GPSLongitude = nil
	}
}