-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
295 lines (255 loc) · 7.53 KB
/
main.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
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/wneessen/go-fileperm"
"gopkg.in/yaml.v3"
)
type Config struct {
InputPaths []string `yaml:"input_paths"`
OutputPath string `yaml:"output_path"`
MoveFiles bool `yaml:"move_files"`
RemoveDuplicates bool `yaml:"remove_duplicates"`
}
var config Config = Config{
MoveFiles: true,
RemoveDuplicates: false,
}
func main() {
slog.Info("Starting Iris v0.1.2...")
slog.Info("Loading config ...")
if err := loadConfig(&config); err != nil {
slog.Error(err.Error())
return
}
// Check if output path exists
if !doesPathExist(config.OutputPath) {
slog.Error("Output folder does not exist")
return
}
// and if we have the permission to write to it
permissions, err := fileperm.New(config.OutputPath)
if err != nil {
slog.Error(err.Error())
return
}
if !permissions.UserWriteReadable() {
slog.Error("No write and/or read permission for output folder")
}
for _, inputFolderPath := range config.InputPaths {
slog.Info("Processing folder", "path", inputFolderPath)
// Check if output path exists
if !doesPathExist(inputFolderPath) {
slog.Error("Input folder does not exist")
return
}
// and if we have the permission to write to it
permissions, err := fileperm.New(inputFolderPath)
if err != nil {
slog.Error(err.Error())
return
}
if !permissions.UserWriteReadable() {
slog.Error("No write and/or read permission for input folder path")
}
if err := filepath.WalkDir(inputFolderPath, walk); err != nil {
slog.Error(err.Error())
}
}
slog.Info("Done!")
}
func loadConfig(configVar *Config) error {
yamlFile, err := os.ReadFile("config.yaml")
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, configVar)
if err != nil {
return err
}
return nil
}
func walk(srcFilePath string, srcFileInfo os.DirEntry, err error) error {
if err != nil {
slog.Error(err.Error())
return nil
}
if srcFileInfo.IsDir() {
// Skip hidden folders
if strings.HasPrefix(srcFileInfo.Name(), ".") {
return filepath.SkipDir
// Don't process folders
} else {
return nil
}
}
// Skip non regular files
if !srcFileInfo.Type().IsRegular() {
return nil
}
// Skip hidden files
if strings.HasPrefix(srcFileInfo.Name(), ".") {
return nil
}
// Open srcFile
srcFile, err := os.Open(srcFilePath)
if err != nil {
slog.Error(err.Error())
return nil
}
defer srcFile.Close()
// Get file content type, important to distinct images and videos
fileContentType, err := getFileContentType(srcFile)
if err != nil {
slog.Error("Could not get file content type", "srcPath", srcFilePath, "error", err.Error())
return nil
}
// Skip non image and video files
supportedFileContentTypes := []string{"image/jpeg", "video/mp4"}
if !slices.Contains(supportedFileContentTypes, fileContentType) {
slog.Warn("File is not a image or video", "srcPath", srcFilePath, "fileContentTrypes", fileContentType)
return nil
}
destFilePath := destFilePath{
rootPath: config.OutputPath,
fileExtension: filepath.Ext(srcFilePath),
}
destFilePath.creationTime, err = getCreationTimeFromMedia(srcFile, srcFilePath, fileContentType)
if err != nil {
slog.Error("Could not get media creation time", "srcPath", srcFilePath)
return nil
}
// Create the folder if it doesn't exist
folderPath := filepath.Dir(destFilePath.generate())
if !doesPathExist(folderPath) {
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
slog.Error("Could not create folder", "folderPath", folderPath, "error", err.Error())
// Stop completely since this likely also affects other files
return filepath.SkipAll
}
}
// File exists, check if they are the same
for doesPathExist(destFilePath.generate()) {
srcFileHash, err := getFileHash(srcFile)
if err != nil {
slog.Error("Could not get file hash", "path", srcFilePath, "error", err.Error())
return nil
}
// Get hash of the existing file
destFile, err := os.Open(destFilePath.generate())
if err != nil {
slog.Error("Could not open file", "destPath", destFilePath.generate(), "error", err.Error())
return nil
}
destFileHash, err := getFileHash(destFile)
if err != nil {
slog.Error("Could not get file hash", "destPath", destFilePath.generate(), "error", err.Error())
return nil
}
destFile.Close()
if srcFileHash == destFileHash {
// Skip if they are the same
slog.Warn("File already exists", "destPath", destFilePath.generate())
// Remove duplicated file if configured
if config.RemoveDuplicates {
err := os.Remove(srcFilePath)
if err != nil {
slog.Error("Could not remove file", "srcPath", srcFilePath, "error", err.Error())
}
}
return nil
} else {
// Try another name if they are different
slog.Warn("Different file with same path found", "destPath", destFilePath.generate())
destFilePath.number++
}
}
// Copy or move the file
err = copyFile(srcFile, destFilePath.generate())
if err != nil {
slog.Error("Could not copy file", "srcPath", srcFilePath, "error", err.Error())
return nil
}
if config.MoveFiles {
err = os.Remove(srcFilePath)
if err != nil {
slog.Error("Could not remove file", "srcPath", srcFilePath, "error", err.Error())
return nil
}
}
return nil
}
func getCreationTimeFromMedia(file *os.File, filePath string, fileContentType string) (time.Time, error) {
// Get creation time, important to distinct images and videos since they have different metadata
if strings.HasPrefix(fileContentType, "image") {
if creationTime, err := getImageCreationTime(file); err == nil {
return creationTime, nil
}
}
if strings.HasPrefix(fileContentType, "video") {
if creationTime, err := getVideoCreationTime(file); err == nil {
return creationTime, nil
}
}
// Try to get date from the filename if the above don't work
srcFileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
// 2006: year, 01: month, 02: day, 15: hour, 04: minute, 05: second
possibleTimeFormats := []string{
"2006-01-02_15-04-05",
"IMG_20060102_150405",
"PXL_20060102_150405",
"IMG-20060102",
"signal-2006-01-02-15-04-05",
"image_20060102150405",
"20060102_150405",
}
for _, format := range possibleTimeFormats {
// Try to remove some random stuff at the end of some image names
if len(srcFileName) < len(format) {
continue
}
cleanSrcFileName := srcFileName[:len(format)]
if creationTime, err := time.Parse(format, cleanSrcFileName); err == nil {
return creationTime, nil
}
}
return time.Time{}, errors.New("could not determine media creation time")
}
type destFilePath struct {
rootPath string
creationTime time.Time
number int
fileExtension string
}
func (d *destFilePath) generate() string {
// Determine folder name to put the file in
var yearQuarter string
if d.creationTime.Month() <= 2 {
yearQuarter = fmt.Sprintf("%d-4", d.creationTime.Year()-1)
} else if d.creationTime.Month() <= 5 {
yearQuarter = fmt.Sprintf("%d-1", d.creationTime.Year())
} else if d.creationTime.Month() <= 8 {
yearQuarter = fmt.Sprintf("%d-2", d.creationTime.Year())
} else if d.creationTime.Month() <= 11 {
yearQuarter = fmt.Sprintf("%d-3", d.creationTime.Year())
} else if d.creationTime.Month() == 12 {
yearQuarter = fmt.Sprintf("%d-4", d.creationTime.Year())
}
var numberSuffix string
if d.number != 0 {
numberSuffix = fmt.Sprintf("_%d", d.number)
}
destFilePath := filepath.Join(
d.rootPath,
yearQuarter,
d.creationTime.Format("2006-01-02_15-04-05")+numberSuffix+d.fileExtension,
)
return destFilePath
}