-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeduplicate_and_rename.go
362 lines (305 loc) · 8.7 KB
/
deduplicate_and_rename.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
package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
exif "github.com/rwcarlsen/goexif/exif"
)
var (
targetDir = flag.String("dir", "", "Path to the destination directory where duplicates need to be removed")
dryRun = flag.Bool("dry-run", false, "Perform a dry run without deleting or renaming any files")
verbose = flag.Bool("verbose", false, "Enable verbose output")
minFileSize = flag.Int64("min-size", 1024, "Minimum file size (in bytes) to process")
trashDir = flag.String("trash-dir", "", "Directory to move duplicates instead of deleting")
)
func main() {
flag.Parse()
if *targetDir == "" {
log.Fatal("Please provide the path to the destination directory using the -dir flag.")
}
err := processDirectory(*targetDir)
if err != nil {
log.Fatalf("Error processing directories: %v", err)
}
}
func processDirectory(dirPath string) error {
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return err
}
if *verbose {
fmt.Printf("Processing %d files in directory %s\n", len(files), dirPath)
}
// Map to store unique identifiers and associated file paths
uniqueMap := make(map[string][]string)
// Regular expression to match image files
regex := regexp.MustCompile(`(?i)^(IMG.*)\.(jpg|jpeg|png|gif|bmp)$`)
for _, file := range files {
if file.IsDir() {
continue
}
// Skip files smaller than the minimum size
if file.Size() < *minFileSize {
if *verbose {
fmt.Printf("Skipping file (too small): %s\n", file.Name())
}
continue
}
fileName := file.Name()
if *verbose {
fmt.Printf("Checking file: %s\n", fileName)
}
if !regex.MatchString(fileName) {
if *verbose {
fmt.Printf("Skipping file (no match): %s\n", fileName)
}
continue
}
if *verbose {
fmt.Printf("Processing file: %s\n", fileName)
}
filePath := filepath.Join(dirPath, fileName)
uniqueID, err := computeUniqueID(filePath)
if err != nil {
log.Printf("Error computing unique ID for %s: %v", filePath, err)
continue
}
if *verbose {
fmt.Printf("File: %s, Unique ID: %s\n", filePath, uniqueID)
}
uniqueMap[uniqueID] = append(uniqueMap[uniqueID], filePath)
}
// Remove duplicates
for uniqueID, filePaths := range uniqueMap {
if len(filePaths) > 1 {
// Sort file paths to determine which one to keep
sortFilesByMetadata(filePaths)
filesToDelete := filePaths[1:]
if *verbose {
fmt.Printf("Unique ID %s has %d duplicates\n", uniqueID, len(filesToDelete))
}
for _, filePath := range filesToDelete {
if *dryRun {
fmt.Printf("Would delete duplicate file: %s\n", filePath)
} else {
if *trashDir != "" {
// Move file to trash directory
err := moveToTrash(filePath, *trashDir)
if err != nil {
log.Printf("Error moving file %s to trash: %v", filePath, err)
} else {
fmt.Printf("Moved duplicate file to trash: %s\n", filePath)
}
} else {
err := os.Remove(filePath)
if err != nil {
log.Printf("Error deleting file %s: %v", filePath, err)
} else {
fmt.Printf("Deleted duplicate file: %s\n", filePath)
}
}
}
}
// Keep only the first file
uniqueMap[uniqueID] = filePaths[:1]
}
}
// Map to keep track of intended new filenames to avoid conflicts
intendedNames := make(map[string]string) // Map from current file path to intended new filename
existingNames := make(map[string]struct{}) // Set of intended new filenames
// Build intended new filenames for all files
for _, filePaths := range uniqueMap {
for _, filePath := range filePaths {
newFileName, err := generateUniqueFileName(filePath, existingNames)
if err != nil {
log.Printf("Error generating new filename for %s: %v", filePath, err)
continue
}
intendedNames[filePath] = newFileName
existingNames[newFileName] = struct{}{}
}
}
// Now perform the renaming
for filePath, newFileName := range intendedNames {
err := performRename(filePath, newFileName)
if err != nil {
log.Printf("Error renaming file %s: %v", filePath, err)
}
}
return nil
}
func computeUniqueID(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
// Attempt to read EXIF data
x, err := exif.Decode(file)
if err != nil {
// If EXIF data can't be read, fall back to computing checksum
file.Seek(0, io.SeekStart)
data, err := io.ReadAll(file)
if err != nil {
return "", err
}
return computeChecksum(data), nil
}
// Extract fields that uniquely identify the photo
make, _ := x.Get("Make")
model, _ := x.Get("Model")
dateTimeOriginal, _ := x.Get("DateTimeOriginal")
lensModel, _ := x.Get("LensModel")
imageUniqueID, _ := x.Get("ImageUniqueID")
serialNumber, _ := x.Get("BodySerialNumber")
// Get string values, handling possible nil pointers
makeStr := ""
if make != nil {
makeStr, _ = make.StringVal()
}
modelStr := ""
if model != nil {
modelStr, _ = model.StringVal()
}
dateTimeOriginalStr := ""
if dateTimeOriginal != nil {
dateTimeOriginalStr, _ = dateTimeOriginal.StringVal()
}
lensModelStr := ""
if lensModel != nil {
lensModelStr, _ = lensModel.StringVal()
}
imageUniqueIDStr := ""
if imageUniqueID != nil {
imageUniqueIDStr, _ = imageUniqueID.StringVal()
}
serialNumberStr := ""
if serialNumber != nil {
serialNumberStr, _ = serialNumber.StringVal()
}
// Concatenate metadata fields
uniqueString := fmt.Sprintf("%v|%v|%v|%v|%v|%v", makeStr, modelStr, dateTimeOriginalStr, lensModelStr, imageUniqueIDStr, serialNumberStr)
if *verbose {
fmt.Printf("Metadata for %s: %s\n", filePath, uniqueString)
}
// Generate MD5 hash of the unique string
hash := md5.Sum([]byte(uniqueString))
uniqueID := hex.EncodeToString(hash[:])
return uniqueID, nil
}
func computeChecksum(data []byte) string {
hash := md5.Sum(data)
return hex.EncodeToString(hash[:])
}
func generateUniqueFileName(filePath string, existingNames map[string]struct{}) (string, error) {
ext := strings.ToLower(filepath.Ext(filePath))
if ext == "" {
return "", fmt.Errorf("file %s has no extension", filePath)
}
timestamp, err := getPhotoTimestamp(filePath)
if err != nil {
return "", err
}
// Start with base filename
baseName := fmt.Sprintf("IMG_%s", timestamp.Format("20060102_150405"))
// Counter for duplicate filenames
counter := 0
var newFileName string
for {
if counter == 0 {
newFileName = fmt.Sprintf("%s%s", baseName, ext)
} else {
newFileName = fmt.Sprintf("%s_%d%s", baseName, counter, ext)
}
// Check if the filename already exists in existingNames map
if _, exists := existingNames[newFileName]; !exists {
// Filename is unique
break
}
counter++
}
// Replace any invalid characters (e.g., ':' on Windows)
newFileName = strings.ReplaceAll(newFileName, ":", "")
return newFileName, nil
}
func performRename(filePath, newFileName string) error {
dir := filepath.Dir(filePath)
newFilePath := filepath.Join(dir, newFileName)
currentFileName := filepath.Base(filePath)
if currentFileName == newFileName {
// File already has the correct name
if *verbose {
fmt.Printf("File already has the correct name: %s\n", filePath)
}
return nil
}
if *dryRun {
fmt.Printf("Would rename file: %s -> %s\n", filePath, newFilePath)
return nil
}
err := os.Rename(filePath, newFilePath)
if err != nil {
return err
}
fmt.Printf("Renamed file: %s -> %s\n", filePath, newFilePath)
return nil
}
func moveToTrash(filePath, trashDir string) error {
// Ensure trash directory exists
if _, err := os.Stat(trashDir); os.IsNotExist(err) {
err = os.MkdirAll(trashDir, os.ModePerm)
if err != nil {
return err
}
}
fileName := filepath.Base(filePath)
destination := filepath.Join(trashDir, fileName)
// Avoid overwriting files in the trash directory
for i := 1; ; i++ {
if _, err := os.Stat(destination); os.IsNotExist(err) {
break
}
destination = filepath.Join(trashDir, fmt.Sprintf("%s_%d", fileName, i))
}
return os.Rename(filePath, destination)
}
func sortFilesByMetadata(filePaths []string) {
sort.Slice(filePaths, func(i, j int) bool {
t1, err1 := getPhotoTimestamp(filePaths[i])
t2, err2 := getPhotoTimestamp(filePaths[j])
if err1 != nil || err2 != nil {
return filePaths[i] < filePaths[j]
}
return t1.Before(t2)
})
}
func getPhotoTimestamp(filePath string) (time.Time, error) {
file, err := os.Open(filePath)
if err != nil {
return time.Time{}, err
}
defer file.Close()
x, err := exif.Decode(file)
if err == nil {
dateTimeOriginal, err := x.DateTime()
if err == nil {
return dateTimeOriginal, nil
}
}
// Fallback to file modification time
info, err := os.Stat(filePath)
if err != nil {
return time.Time{}, err
}
return info.ModTime(), nil
}