-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (136 loc) · 3.66 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
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/bibauporto/photosOrganizer/helpers"
"github.com/bibauporto/photosOrganizer/processors"
"github.com/schollz/progressbar/v3"
)
// Process all folders and files recursively with progress
func processFiles(folderPath string) error {
files, err := os.ReadDir(folderPath)
if err != nil {
return fmt.Errorf("error reading directory %s: %w", folderPath, err)
}
// Calculate total files to process for the progress bar
var totalFiles int
filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
totalFiles++
}
return nil
})
// Initialize progress bar
bar := progressbar.New(totalFiles)
for _, file := range files {
filePath := filepath.Join(folderPath, file.Name())
if file.IsDir() {
// Recursively process subfolders
if err := processFiles(filePath); err != nil {
return fmt.Errorf("error processing folder %s: %w", filePath, err)
}
} else {
// Process files based on extension
ext := strings.ToLower(filepath.Ext(file.Name()))
switch {
case helpers.Contains(helpers.IMAGE_EXTENSIONS, ext):
if err := processors.ProcessImage(file.Name(), folderPath); err != nil {
return fmt.Errorf("error processing image %s: %w", file.Name(), err)
}
case helpers.Contains(helpers.VIDEO_EXTENSIONS, ext):
if err := processors.ProcessVideo(file.Name(), folderPath); err != nil {
return fmt.Errorf("error processing video %s: %w", file.Name(), err)
}
default:
// fmt.Printf("Skipping unsupported file type: %s\n", file.Name())
}
// Update progress bar
bar.Add(1)
}
}
return nil
}
// Delete duplicate files based on MD5 hash with progress
func deleteDuplicates(folderPath string) error {
hashes := make(map[string]string)
var totalFiles int
// Count total files to process for the progress bar
filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() {
totalFiles++
}
return nil
})
// Initialize progress bar
bar := progressbar.New(totalFiles)
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// Open the file within a limited scope
func() error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return err
}
hashStr := fmt.Sprintf("%x", hash.Sum(nil))
if _, found := hashes[hashStr]; found {
// fmt.Printf("Duplicate found: %s and %s\n", existingPath, path)
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to delete file %s: %w", path, err)
}
} else {
hashes[hashStr] = path
}
return nil
}()
// Update progress bar
bar.Add(1)
return nil
})
return err
}
func main() {
for {
var choice int
fmt.Println("Select an option:")
fmt.Println("1. Parse and rename photos and videos")
fmt.Println("2. Delete duplicates")
fmt.Println("3. Exit")
fmt.Scan(&choice)
folderPath, _ := os.Getwd()
switch choice {
case 1:
fmt.Println("Starting processing...")
if err := processFiles(folderPath); err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Println("Processing complete.")
case 2:
fmt.Println("Deleting duplicates...")
if err := deleteDuplicates(folderPath); err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Println("Duplicate deletion complete.")
case 3:
fmt.Println("Exiting program.")
os.Exit(0)
default:
fmt.Println("Invalid option. Exiting program.")
os.Exit(1)
}
fmt.Println("---------------------------------")
}
}