-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovephoto.go
658 lines (566 loc) · 18.7 KB
/
movephoto.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package main
import (
"bufio"
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
exiftool "github.com/barasher/go-exiftool"
"gopkg.in/yaml.v2"
)
// WatchDir represents a directory to watch along with the action to perform and optional prefixes
type WatchDir struct {
Path string `yaml:"path"`
Action string `yaml:"action"` // "move" or "copy"
IncludePrefix []string `yaml:"includePrefix"` // List of prefixes to include (optional)
}
// Config holds the configuration data
type Config struct {
WatchDirs []WatchDir `yaml:"watchDirs"`
DefaultDestinationDir string `yaml:"defaultDestinationDir"`
ImageExtensions []string `yaml:"imageExtensions"`
VideoExtensions []string `yaml:"videoExtensions"`
BannedExtensions []string `yaml:"bannedExtensions"`
LockFilePath string `yaml:"lockFilePath"`
}
// Global variable to keep track of processed files
var processedFiles map[string]struct{}
// Path to the processed files list
var processedFilesPath string
func loadConfig() Config {
if _, err := os.Stat(*configFilePath); os.IsNotExist(err) {
fmt.Printf("%s does not exist. Please create it and run the program again.\n", *configFilePath)
os.Exit(0)
}
config := Config{}
data, err := os.ReadFile(*configFilePath)
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
log.Fatalf("error: %v", err)
}
return config
}
var (
pollingInterval = flag.Int("polling-interval", 30, "Polling interval in seconds for checking new files in the watch directories")
debug = flag.Bool("debug", false, "Enable debug output")
watch = flag.Bool("watch", false, "Enable regular scanning of the source directories")
configFilePath = flag.String("config", "/etc/movephoto_config.yml", "Path to the configuration file")
minFileSize = int64(102400) // Minimum file size in bytes (100KB)
)
func main() {
flag.Parse() // Parse the command-line flags
if *debug {
log.Printf("[%s] Debug mode enabled\n", currentTime())
}
config := loadConfig()
// Add .heic to the list of image extensions
config.ImageExtensions = append(config.ImageExtensions, ".heic")
// Set the path for processed_files.txt under the destination directory
processedFilesPath = filepath.Join(config.DefaultDestinationDir, "processed_files.txt")
// Load processed files
processedFiles = loadProcessedFiles(processedFilesPath)
if !*watch {
if *debug {
log.Printf("[%s] Performing a single scan...\n", currentTime())
}
processFiles(config)
} else {
if *debug {
log.Printf("[%s] Starting regular scanning of source directories...\n", currentTime())
}
ticker := time.NewTicker(time.Duration(*pollingInterval) * time.Second)
defer ticker.Stop()
for range ticker.C {
if *debug {
log.Printf("[%s] Polling for new files...\n", currentTime())
}
processFiles(config)
}
}
}
func processFiles(config Config) {
for _, watchDir := range config.WatchDirs {
purge_unwanted(watchDir.Path, config.BannedExtensions)
switch watchDir.Action {
case "move":
move_photos(watchDir.Path, config.DefaultDestinationDir, config.ImageExtensions)
move_videos(watchDir.Path, config.DefaultDestinationDir, config.VideoExtensions)
case "copy":
copy_photos(watchDir.Path, config.DefaultDestinationDir, config.ImageExtensions, watchDir.IncludePrefix)
copy_videos(watchDir.Path, config.DefaultDestinationDir, config.VideoExtensions, watchDir.IncludePrefix)
default:
log.Printf("Unknown action %s for watch directory %s", watchDir.Action, watchDir.Path)
}
}
// No need to save processed files here since it's being updated during processing
}
func purge_unwanted(watch_dir string, banned_extensions []string) error {
files, err := os.ReadDir(watch_dir)
if err != nil {
return err
}
for _, file := range files {
for _, ext := range banned_extensions {
if strings.ToLower(filepath.Ext(file.Name())) == ext {
os.Remove(filepath.Join(watch_dir, file.Name()))
}
}
}
return nil
}
func move_files(watch_dir string, destination_dir string, extensions []string, get_destination_dir func(filePath string, file os.FileInfo) (string, bool)) error {
files, err := os.ReadDir(watch_dir)
if err != nil {
return err
}
for _, entry := range files {
info, err := entry.Info()
if err != nil {
continue // Skip if we can't get file info
}
if !info.Mode().IsRegular() {
continue // Skip non-regular files
}
if !hasExtension(info.Name(), extensions) {
continue
}
// Skip files smaller than the minimum size
if info.Size() < minFileSize {
if *debug {
log.Printf("[%s] Skipping file (too small): %s\n", currentTime(), info.Name())
}
continue
}
sourcePath := filepath.Join(watch_dir, info.Name())
full_destination_dir, shouldProcess := get_destination_dir(sourcePath, info)
if !shouldProcess {
log.Printf("[%s] Skipping file: %s (no valid date found)\n", currentTime(), sourcePath)
continue
}
full_destination := filepath.Join(full_destination_dir, info.Name())
if _, err := os.Stat(full_destination_dir); os.IsNotExist(err) {
os.MkdirAll(full_destination_dir, os.ModePerm)
}
if _, err := os.Stat(full_destination); os.IsNotExist(err) {
err := copyAndVerify(sourcePath, full_destination)
if err != nil {
log.Printf("[%s] Failed to move file: %s\n", currentTime(), err)
} else {
// Delete the source file after successful copy and verification
err = os.Remove(sourcePath)
if err != nil {
log.Printf("[%s] Failed to delete source file: %s\n", currentTime(), err)
} else {
log.Printf("[%s] Moved file: %s to %s\n", currentTime(), sourcePath, full_destination)
}
}
}
}
return nil
}
func copy_files(watch_dir string, destination_dir string, extensions []string, includePrefix []string, get_destination_dir func(filePath string, file os.FileInfo) (string, bool)) error {
files, err := os.ReadDir(watch_dir)
if err != nil {
return err
}
for _, entry := range files {
info, err := entry.Info()
if err != nil {
continue // Skip if we can't get file info
}
if !info.Mode().IsRegular() {
continue // Skip non-regular files
}
if !hasExtension(info.Name(), extensions) {
continue
}
// Skip files smaller than the minimum size
if info.Size() < minFileSize {
if *debug {
log.Printf("[%s] Skipping file (too small): %s\n", currentTime(), info.Name())
}
continue
}
// Apply includePrefix filtering if includePrefix is not empty
if len(includePrefix) > 0 && !hasPrefix(info.Name(), includePrefix) {
continue // Skip the file
}
filePath := filepath.Join(watch_dir, info.Name())
if _, ok := processedFiles[filePath]; ok {
// File has already been processed
continue
}
full_destination_dir, shouldProcess := get_destination_dir(filePath, info)
if !shouldProcess {
log.Printf("[%s] Skipping file: %s (no valid date found)\n", currentTime(), filePath)
continue
}
full_destination := filepath.Join(full_destination_dir, info.Name())
if _, err := os.Stat(full_destination_dir); os.IsNotExist(err) {
os.MkdirAll(full_destination_dir, os.ModePerm)
}
if _, err := os.Stat(full_destination); os.IsNotExist(err) {
err := copyAndVerify(filePath, full_destination)
if err != nil {
log.Printf("[%s] Failed to copy file: %s\n", currentTime(), err)
} else {
log.Printf("[%s] Copied file: %s to %s\n", currentTime(), filePath, full_destination)
// Add to processed files and update the file immediately
processedFiles[filePath] = struct{}{}
appendToProcessedFiles(processedFilesPath, filePath)
}
} else {
// Destination file already exists
processedFiles[filePath] = struct{}{}
// Ensure it's in the processed files list
appendToProcessedFiles(processedFilesPath, filePath)
}
}
return nil
}
func move_photos(watch_dir string, destination_dir string, image_extensions []string) error {
return move_files(watch_dir, destination_dir, image_extensions, func(filePath string, file os.FileInfo) (string, bool) {
date_taken, err := getPhotoTimestamp(filePath)
if err != nil {
// Attempt to parse date from filename
date_taken, err = parseDateFromFilename(file.Name())
if err != nil {
log.Printf("[%s] Skipping photo %s: no valid date found\n", currentTime(), filePath)
return "", false
}
}
year_taken, month_taken, day_taken := date_taken.Date()
month_name := month_taken.String()
return filepath.Join(
destination_dir,
fmt.Sprintf("%d", year_taken),
fmt.Sprintf("%02d - %s", month_taken, month_name),
fmt.Sprintf("%04d-%02d-%02d", year_taken, int(month_taken), day_taken),
), true
})
}
func move_videos(watch_dir string, destination_dir string, video_extensions []string) error {
return move_files(watch_dir, destination_dir, video_extensions, func(filePath string, file os.FileInfo) (string, bool) {
date_taken, err := getVideoTimestamp(filePath)
if err != nil {
// Log a notification and skip the video
log.Printf("[%s] Skipping video %s: %v\n", currentTime(), filePath, err)
return "", false
}
year_taken, month_taken, day_taken := date_taken.Date()
month_name := month_taken.String()
return filepath.Join(
destination_dir,
fmt.Sprintf("%d", year_taken),
fmt.Sprintf("%02d - %s", month_taken, month_name),
fmt.Sprintf("%04d-%02d-%02d", year_taken, int(month_taken), day_taken),
), true
})
}
func copy_photos(watch_dir string, destination_dir string, image_extensions []string, includePrefix []string) error {
return copy_files(watch_dir, destination_dir, image_extensions, includePrefix, func(filePath string, file os.FileInfo) (string, bool) {
date_taken, err := getPhotoTimestamp(filePath)
if err != nil {
// Attempt to parse date from filename
date_taken, err = parseDateFromFilename(file.Name())
if err != nil {
log.Printf("[%s] Skipping photo %s: no valid date found\n", currentTime(), filePath)
return "", false
}
}
year_taken, month_taken, day_taken := date_taken.Date()
month_name := month_taken.String()
return filepath.Join(
destination_dir,
fmt.Sprintf("%d", year_taken),
fmt.Sprintf("%02d - %s", month_taken, month_name),
fmt.Sprintf("%04d-%02d-%02d", year_taken, int(month_taken), day_taken),
), true
})
}
func copy_videos(watch_dir string, destination_dir string, video_extensions []string, includePrefix []string) error {
return copy_files(watch_dir, destination_dir, video_extensions, includePrefix, func(filePath string, file os.FileInfo) (string, bool) {
date_taken, err := getVideoTimestamp(filePath)
if err != nil {
// Log a notification and skip the video
log.Printf("[%s] Skipping video %s: %v\n", currentTime(), filePath, err)
return "", false
}
year_taken, month_taken, day_taken := date_taken.Date()
month_name := month_taken.String()
return filepath.Join(
destination_dir,
fmt.Sprintf("%d", year_taken),
fmt.Sprintf("%02d - %s", month_taken, month_name),
fmt.Sprintf("%04d-%02d-%02d", year_taken, int(month_taken), day_taken),
), true
})
}
// getPhotoTimestamp extracts the DateTimeOriginal from the photo's EXIF data using ExifTool
func getPhotoTimestamp(filePath string) (time.Time, error) {
et, err := exiftool.NewExiftool()
if err != nil {
return time.Time{}, fmt.Errorf("Error when creating Exiftool: %v", err)
}
defer et.Close()
fileInfos := et.ExtractMetadata(filePath)
if len(fileInfos) == 0 {
return time.Time{}, fmt.Errorf("No metadata extracted for file: %s", filePath)
}
fi := fileInfos[0]
if fi.Err != nil {
return time.Time{}, fi.Err
}
// Try to get DateTimeOriginal, CreateDate, ModifyDate, or DateTimeDigitized
var dateStr string
var ok bool
dateTags := []string{"DateTimeOriginal", "CreateDate", "ModifyDate", "DateTimeDigitized"}
for _, tag := range dateTags {
dateStr, ok = fi.Fields[tag].(string)
if ok && dateStr != "" {
break
}
}
if !ok || dateStr == "" {
return time.Time{}, fmt.Errorf("No valid date tag found in EXIF data")
}
dateFormats := []string{
"2006:01:02 15:04:05",
"2006:01:02 15:04:05-07:00",
"2006:01:02 15:04:05Z07:00",
}
var parsedTime time.Time
var parseErr error
for _, format := range dateFormats {
parsedTime, parseErr = time.Parse(format, dateStr)
if parseErr == nil {
break
}
}
if parseErr != nil {
return time.Time{}, fmt.Errorf("Error parsing date: %v", parseErr)
}
return parsedTime, nil
}
// getVideoTimestamp extracts the MediaCreateDate or CreateDate from the video's metadata using ExifTool
func getVideoTimestamp(filePath string) (time.Time, error) {
et, err := exiftool.NewExiftool()
if err != nil {
return time.Time{}, fmt.Errorf("Error when creating Exiftool: %v", err)
}
defer et.Close()
fileInfos := et.ExtractMetadata(filePath)
if len(fileInfos) == 0 {
return time.Time{}, fmt.Errorf("No metadata extracted for file: %s", filePath)
}
fi := fileInfos[0]
if fi.Err != nil {
return time.Time{}, fi.Err
}
// Try to get MediaCreateDate, CreateDate, or ModifyDate
var dateStr string
var ok bool
dateTags := []string{"MediaCreateDate", "CreateDate", "ModifyDate"}
for _, tag := range dateTags {
dateStr, ok = fi.Fields[tag].(string)
if ok {
break
}
}
if !ok {
return time.Time{}, fmt.Errorf("No valid date tag found in metadata")
}
// Remove timezone information in format "+00:00" if present
dateStr = strings.Replace(dateStr, "+00:00", "", 1)
dateFormats := []string{
"2006:01:02 15:04:05",
"2006:01:02 15:04:05-07:00",
"2006:01:02 15:04:05Z07:00",
}
var parsedTime time.Time
var parseErr error
for _, format := range dateFormats {
parsedTime, parseErr = time.Parse(format, dateStr)
if parseErr == nil {
break
}
}
if parseErr != nil {
return time.Time{}, fmt.Errorf("Error parsing date: %v", parseErr)
}
return parsedTime, nil
}
// hasExtension checks if the filename has one of the specified extensions
func hasExtension(filename string, extensions []string) bool {
fileExt := strings.ToLower(filepath.Ext(filename))
for _, ext := range extensions {
if fileExt == ext {
return true
}
}
return false
}
// hasPrefix checks if the filename starts with any of the specified prefixes
func hasPrefix(filename string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(filename, prefix) {
return true
}
}
return false
}
// currentTime returns the current time as a formatted string with timezone.
func currentTime() string {
return time.Now().Format("2006-01-02 15:04:05 MST")
}
// copyAndVerify copies a file from src to dst and verifies the integrity
func copyAndVerify(src, dst string) error {
// Copy the file
err := copyFile(src, dst)
if err != nil {
return err
}
// Check the file size of the destination
destInfo, err := os.Stat(dst)
if err != nil {
// Delete the incomplete destination file
os.Remove(dst)
return fmt.Errorf("error stating destination file: %v", err)
}
if destInfo.Size() == 0 {
// Delete the empty destination file
os.Remove(dst)
return fmt.Errorf("destination file %s has zero size after copy", dst)
}
// Compute checksums of source and destination
srcChecksum, err := computeFileChecksum(src)
if err != nil {
// Delete the destination file if checksum fails
os.Remove(dst)
return fmt.Errorf("error computing checksum of source file: %v", err)
}
destChecksum, err := computeFileChecksum(dst)
if err != nil {
// Delete the destination file if checksum fails
os.Remove(dst)
return fmt.Errorf("error computing checksum of destination file: %v", err)
}
if srcChecksum != destChecksum {
// Delete the destination file if checksums don't match
os.Remove(dst)
return fmt.Errorf("checksum mismatch between source and destination files for %s", src)
}
return nil
}
// Function to parse date from filename
func parseDateFromFilename(filename string) (time.Time, error) {
// Define regex patterns for iPhone and Pixel filenames
iphonePattern := regexp.MustCompile(`(\d{8})_\d{9}_iOS`)
pixelPattern := regexp.MustCompile(`PXL_(\d{8})_\d{9}`)
// Check for iPhone pattern
if matches := iphonePattern.FindStringSubmatch(filename); matches != nil {
return time.Parse("20060102", matches[1])
}
// Check for Pixel pattern
if matches := pixelPattern.FindStringSubmatch(filename); matches != nil {
return time.Parse("20060102", matches[1])
}
return time.Time{}, fmt.Errorf("no date found in filename")
}
// copyFile copies a file from src to dst
func copyFile(src, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}
if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
// Check if destination file exists
if _, err := os.Stat(dst); err == nil {
return fmt.Errorf("destination file %s already exists", dst)
}
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
destination, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, sourceFileStat.Mode())
if err != nil {
return err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
if err != nil {
// Delete the incomplete destination file
os.Remove(dst)
return err
}
if nBytes == 0 {
// Delete the empty destination file
os.Remove(dst)
return fmt.Errorf("copied zero bytes from %s to %s", src, dst)
}
return nil
}
// computeFileChecksum computes the MD5 checksum of a file
func computeFileChecksum(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
checksum := hex.EncodeToString(hash.Sum(nil))
return checksum, nil
}
// loadProcessedFiles loads the list of processed files from a file
func loadProcessedFiles(filePath string) map[string]struct{} {
processedFiles := make(map[string]struct{})
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
return processedFiles
}
log.Fatalf("Error opening processed files list: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
processedFiles[line] = struct{}{}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading processed files list: %v", err)
}
return processedFiles
}
// appendToProcessedFiles appends a file path to the processed files list
func appendToProcessedFiles(filePath, processedFile string) {
// Ensure the directory exists
processedDir := filepath.Dir(filePath)
if _, err := os.Stat(processedDir); os.IsNotExist(err) {
os.MkdirAll(processedDir, os.ModePerm)
}
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error opening processed files list for appending: %v", err)
}
defer file.Close()
if _, err := file.WriteString(processedFile + "\n"); err != nil {
log.Fatalf("Error writing to processed files list: %v", err)
}
}