-
Notifications
You must be signed in to change notification settings - Fork 4
/
photosort.go
191 lines (163 loc) · 4.29 KB
/
photosort.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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/rwcarlsen/goexif/exif"
)
func main() {
var sourceFolder, destinationFolder string
flag.StringVar(&sourceFolder, "source-folder", "", "Source folder with photos")
flag.StringVar(&destinationFolder, "destination-folder", "", "Destination folder with archived sorted photos")
flag.Parse()
if sourceFolder == "" {
log.Fatal("source must be specified")
}
if destinationFolder == "" {
log.Fatal("archive must be specified")
}
var totalSize int64 = 0
err := filepath.Walk(sourceFolder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
copiedBytes, err := processFile(path, destinationFolder)
if err != nil {
// we won't return nil just to not quit the walk function
log.Println(err)
} else {
totalSize += copiedBytes
log.Println(ByteCountSI(totalSize))
}
return nil
})
if err != nil {
log.Println(err)
}
}
// Converts a size in bytes to a human-readable string in SI (decimal)
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
func processFile(filePath string, archiveFolder string) (int64, error) {
filename := filepath.Base(filePath)
date, err := getDate(filePath)
if err != nil {
if err := createDir(filepath.Join(archiveFolder, "others")); err != nil {
return 0, err
}
extension := filepath.Ext(filePath)
if isImageOrVideo(extension) {
finalPath := filepath.Join(archiveFolder, "others", filename)
return copyFile(filePath, finalPath)
}
return 0, &ErrNotMediaFile{extension, filePath}
}
finalPath, err := newPath(archiveFolder, filename, date)
if err != nil {
return 0, err
}
return copyFile(filePath, finalPath)
}
func isImageOrVideo(extension string) bool {
// Convert extension to lowercase to ensure case-insensitive comparison
ext := strings.ToLower(extension)
switch ext {
case ".tiff", ".tif", ".gif", ".jpeg", ".jpg", ".png", ".raw",
".webm", ".mkv", ".avi", ".mov", ".wmv", ".mp4", ".m4v",
".mpg", ".mp2", ".mpeg":
return true
default:
return false
}
}
func getDate(filepath string) (time.Time, error) {
file, err := os.Open(filepath)
if err != nil {
return time.Time{}, err
}
defer file.Close()
data, err := exif.Decode(file)
if err != nil {
// Fallback to file modification date if Exif data is not available
fileInfo, statErr := os.Stat(filepath)
if statErr != nil {
return time.Time{}, statErr
}
return fileInfo.ModTime(), nil
}
return data.DateTime()
}
// Returns true if a dir/file already exists
func Exists(filepath string) (bool, error) {
if _, err := os.Stat(filepath); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
// Generates the entire new path based on all the data, checks for collisions (and rename if needed)
func newPath(archive string, oldName string, date time.Time) (string, error) {
dir := filepath.Join(archive, strconv.Itoa(date.Year()), strconv.Itoa(int(date.Month())))
if err := createDir(dir); err != nil {
return "", err
}
return filepath.Join(archive, strconv.Itoa(date.Year()), strconv.Itoa(int(date.Month())), oldName), nil
}
// Creates a directory if it doesn't exist yet
func createDir(dir string) error {
if exists, err := Exists(dir); err != nil {
return err
} else if !exists {
err = os.MkdirAll(dir, 0755) // This is the default permission for a folder
if err != nil {
return err
}
}
return nil
}
func copyFile(src, dst string) (int64, error) {
if exists, err := Exists(dst); err != nil {
return 0, err
} else if exists {
return 0, &ErrFileExists{filePath: dst}
}
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, &ErrFileNotRegular{filePath: src}
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer func() { _ = source.Close() }()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer func() { _ = destination.Close() }()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}