-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_parser.go
389 lines (343 loc) · 11.5 KB
/
path_parser.go
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/mmcloughlin/geohash"
"github.com/rwcarlsen/goexif/exif"
"github.com/wailsapp/wails/v2/pkg/runtime"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
type ImageMetadata struct {
FileSize int64 `json:"file_size"`
FilePath string `json:"filepath"`
Width int `json:"width"`
Height int `json:"height"`
Year string `json:"year"`
YearTaken string `json:"year_taken"`
YearCreated string `json:"year_created"`
Month string `json:"month"`
MonthTaken string `json:"month_taken"`
MonthCreated string `json:"month_created"`
Date string `json:"date"`
DateTaken string `json:"date_taken"`
DateCreated string `json:"date_created"`
Parent string `json:"parent"`
ParentIfNotDate string `json:"parent_if_not_date"`
Location GeoLocationMetadata `json:"location"`
}
type GeoLocationMetadata struct {
Geohash string `json:"hash"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Country string `json:"country"`
Division string `json:"division"`
City string `json:"city"`
Place string `json:"place"`
Json string `json:"json"`
}
type PlaceholderMap map[string]string
func getExifData(filepath string) (*exif.Exif, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
return nil, err
}
return x, nil
}
func getExifDateTime(x *exif.Exif) (time.Time, error) {
tm, err := x.DateTime()
if err != nil {
return time.Time{}, err
}
return tm, nil
}
func getExifLocation(x *exif.Exif) (float64, float64, string, error) {
lat, long, err := x.LatLong()
if err != nil {
return 0, 0, "", err
}
return lat, long, geohash.EncodeWithPrecision(lat, long, 8), nil
}
func getImageDimensions(filePath string) (int, int, error) {
file, err := os.Open(filePath)
if err != nil {
return 0, 0, err
}
defer file.Close()
config, _, err := image.DecodeConfig(file)
if err != nil {
return 0, 0, err
}
width := config.Width
height := config.Height
return width, height, nil
}
func isDateFolder(folderPath string) bool {
folderPath = NormalizePath(folderPath)
r, _ := regexp.Compile(`^(\d{4}([-/]\d{2}([-/]\d{2})?)?)$`)
if r.MatchString(filepath.Base(folderPath)) {
return true
}
r, _ = regexp.Compile(`(\d{4}(/\d{2}(/\d{2})?)?)$`)
if r.MatchString(folderPath) {
return true
}
return false
}
func GetImageMetadata(filePath string, getLocation bool, cache *CacheDatabase) (*ImageMetadata, error) {
metadata := &ImageMetadata{}
fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, err
}
// Set the created date from the file info
metadata.FileSize = fileInfo.Size()
metadata.FilePath = NormalizePath(filePath)
metadata.YearCreated = fmt.Sprintf("%04d", fileInfo.ModTime().Year())
metadata.MonthCreated = fmt.Sprintf("%02d", fileInfo.ModTime().Month())
metadata.DateCreated = fmt.Sprintf("%02d", fileInfo.ModTime().Day())
// Get image sizes
width, height, err := getImageDimensions(metadata.FilePath)
if err == nil {
metadata.Width = width
metadata.Height = height
}
// Set datetime and location from exif data, if available
exifData, err := getExifData(metadata.FilePath)
if err == nil {
dateTaken, err := getExifDateTime(exifData)
if err == nil {
metadata.YearTaken = fmt.Sprintf("%04d", dateTaken.Year())
metadata.MonthTaken = fmt.Sprintf("%02d", dateTaken.Month())
metadata.DateTaken = fmt.Sprintf("%02d", dateTaken.Day())
}
// Only get location stuff if needs be because it can dd a 1 sec delay per image
if getLocation {
lat, long, hash, err := getExifLocation(exifData)
if err == nil {
metadata.Location = *DoLocationLookup(lat, long, hash, cache)
}
}
}
// Set the generic date details
if metadata.YearTaken != "" {
metadata.Year = metadata.YearTaken
} else {
metadata.Year = metadata.YearCreated
}
if metadata.MonthTaken != "" {
metadata.Month = metadata.MonthTaken
} else {
metadata.Month = metadata.MonthCreated
}
if metadata.DateTaken != "" {
metadata.Date = metadata.DateTaken
} else {
metadata.Date = metadata.DateCreated
}
// Set the parent folder data
parentFolder := filepath.Base(filepath.Dir(metadata.FilePath))
metadata.Parent = parentFolder
if !isDateFolder(filepath.Dir(metadata.FilePath)) {
metadata.ParentIfNotDate = parentFolder
}
return metadata, nil
}
func ProcessPathSubstitution(placeholderText string, metadata *ImageMetadata) string {
placeholderMap := PlaceholderMap{
"{year}": metadata.Year,
"{year_taken}": metadata.YearTaken,
"{year_created}": metadata.YearCreated,
"{month}": metadata.Month,
"{month_taken}": metadata.MonthTaken,
"{month_created}": metadata.MonthCreated,
"{date}": metadata.Date,
"{date_taken}": metadata.DateTaken,
"{date_created}": metadata.DateCreated,
"{parent}": metadata.Parent,
"{parent_if_not_date}": metadata.ParentIfNotDate,
"{location_hash}": metadata.Location.Geohash,
"{location_country}": metadata.Location.Country,
"{location_division}": metadata.Location.Division,
"{location_city}": metadata.Location.City,
"{location_place}": metadata.Location.Place,
}
for placeholder, value := range placeholderMap {
r := CaseInsensitiveReplacer(placeholder, value)
placeholderText = r.Replace(placeholderText)
}
placeholderText += "/" + filepath.Base(metadata.FilePath)
placeholderText = strings.Replace(NormalizePath(placeholderText), "/, /", "/", -1)
placeholderText = strings.Replace(placeholderText, ", /", "/", -1)
return placeholderText
}
func RelocateFiles(a *App) {
runtime.EventsEmit(a.ctx, "relocating-start")
totalFiles := len(a.substitutions)
totalRelocated := 0
for i := range a.substitutions {
destinationPath := ToPathWithSuffix(a.substitutions[i].To, a.substitutions[i].Suffix)
destinationFolder := filepath.Dir(destinationPath)
if _, err := os.Stat(destinationFolder); os.IsNotExist(err) {
fmt.Println("Creating folder", destinationFolder)
_ = os.MkdirAll(destinationFolder, os.ModePerm)
}
uniqueDestinationPath := UniqueFileName(destinationPath)
if uniqueDestinationPath != destinationPath {
fmt.Printf("The destination file '%s' already exists", destinationPath)
if a.skipOrRename == "skip" {
fmt.Println(" - skipping")
continue
}
fmt.Printf(" - renaming to '%s'\n", uniqueDestinationPath)
destinationPath = uniqueDestinationPath
}
fmt.Println("Copying", a.substitutions[i].From, "to", destinationPath)
sourceFile, err := os.Open(a.substitutions[i].From)
if err != nil {
color.Red("Source file error: %s", err)
continue
}
destinationFile, err := os.Create(destinationPath)
if err != nil {
color.Red("Destination file error: %s", err)
sourceFile.Close()
continue
}
defer destinationFile.Close()
if _, err := io.Copy(destinationFile, sourceFile); err != nil {
color.Red("Failed to copy: %s", err)
sourceFile.Close()
continue
}
sourceFile.Close()
if a.moveOrCopy == "move" {
color.Yellow("Removing %s", a.substitutions[i].From)
err := os.Remove(a.substitutions[i].From)
if err != nil {
color.Red("Remove error: %s", err)
continue
}
}
a.substitutions[i].Relocated = true
totalRelocated++
runtime.EventsEmit(a.ctx, "relocating-files", RelocationStatus{a.substitutions, totalFiles, totalRelocated})
}
runtime.EventsEmit(a.ctx, "relocating-complete")
}
func DoLocationLookup(latitude float64, longitude float64, hash string, cache *CacheDatabase) *GeoLocationMetadata {
type LocationResponseStruct struct {
PlaceId int `json:"place_id"`
License string `json:"licence"`
OsmType string `json:"osm_type"`
OsmId int `json:"osm_id"`
Latitude string `json:"lat"`
Longitude string `json:"lon"`
Name string `json:"name"`
Category string `json:"category"`
Type string `json:"type"`
AddressType string `json:"addresstype"`
DisplayName string `json:"display_name"`
PlaceRank int `json:"place_rank"`
Importance float64
Address struct {
Amenity string `json:"amenity"`
Leisure string `json:"leisure"`
Road string `json:"road"`
Residential string `json:"residential"`
Village string `json:"village"`
Suburb string `json:"suburb"`
Town string `json:"town"`
City string `json:"city"`
CityDistrict string `json:"city_district"`
State string `json:"state"`
StateDistrict string `json:"state_district"`
County string `json:"county"`
Postcode string `json:"postcode"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
} `json:"address"`
BoundingBox []string `json:"boundingbox"`
}
// Check the cache first
if cache != nil {
geoLocationMetadata, _ := cache.GetLocation(hash)
if geoLocationMetadata != nil {
return geoLocationMetadata
}
}
// Look-up like: https://nominatim.openstreetmap.org/reverse?lat=50.842605555556&lon=0.16754722222222&format=jsonv2
// But needs to be 1 look-up per second max, with a proper user agent
time.Sleep(1 * time.Second)
// Get the data
url := fmt.Sprintf(`https://nominatim.openstreetmap.org/reverse?lat=%f&lon=%f&format=jsonv2&accept-language=en-GB&zoom=18`, latitude, longitude)
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "amnuts-photo-organizer/1.0")
resp, _ := client.Do(req)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
// Construct GeoLocationMetadata from LocationResponse
locationResponse := &LocationResponseStruct{}
_ = json.NewDecoder(resp.Body).Decode(locationResponse)
jsonMarshaled, _ := json.Marshal(locationResponse)
data := GeoLocationMetadata{
Latitude: latitude,
Longitude: longitude,
Country: locationResponse.Address.Country,
Place: locationResponse.Name,
Geohash: hash,
Json: string(jsonMarshaled),
}
// Sort out some (hopefully) sensible mappings
if locationResponse.Address.CountryCode == "gb" {
// fmt.Printf("%+v\n", locationResponse)
data.Country = locationResponse.Address.State
data.Division = locationResponse.Address.County
} else {
data.Country = locationResponse.Address.Country
data.Division = locationResponse.Address.State
data.City = locationResponse.Address.County
}
if locationResponse.Address.Village != "" {
data.City = locationResponse.Address.Village
} else if locationResponse.Address.Suburb != "" {
data.City = locationResponse.Address.Suburb
} else if locationResponse.Address.Town != "" {
data.City = locationResponse.Address.Town
} else if data.City == "" {
data.City = locationResponse.Address.City
}
if data.Division == "" && locationResponse.Address.StateDistrict != "" {
data.Division = locationResponse.Address.StateDistrict
}
if data.Division == "" && locationResponse.Address.City != "" {
data.Division = locationResponse.Address.City
}
if locationResponse.Name == "" && locationResponse.Address.Road != "" {
data.Place = locationResponse.Address.Road
}
// Cache the data
if cache != nil {
_ = cache.InsertLocation(url, data)
}
return &data
}