-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
286 lines (233 loc) · 7.09 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
package main
import (
"fmt"
"image/jpeg"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/adrium/goheif"
)
const logFileName = "logs.txt"
func main() {
fmt.Println("Starting the program...")
currentDir, err := getCurrentDirectory()
if err != nil {
log.Fatalf("Failed to get current directory: %v", err)
}
jpegDir := ensureJPEGDirectoryExists(currentDir)
files, err := getFilesInDirectory(currentDir)
if err != nil {
log.Fatalf("Failed to read directory: %v", err)
}
logs := processFiles(currentDir, jpegDir, files)
saveLogsToFile(jpegDir, logs)
fmt.Println("Program completed!")
}
func getCurrentDirectory() (string, error) {
fmt.Println("Fetching the current directory...")
return os.Getwd()
}
func ensureJPEGDirectoryExists(dir string) string {
jpegDir := filepath.Join(dir, "jpegs")
if err := os.MkdirAll(jpegDir, 0755); err != nil {
log.Fatalf("Failed to create directory: %v", err)
}
return jpegDir
}
func getFilesInDirectory(dir string) ([]os.DirEntry, error) {
return os.ReadDir(dir)
}
func saveLogsToFile(jpegDir string, logs map[string][]string) {
logFilePath := filepath.Join(jpegDir, logFileName)
logFile, err := os.Create(logFilePath)
if err != nil {
log.Fatalf("Failed to create log file: %v", err)
}
defer logFile.Close()
fmt.Println("Saving logs to logs.txt...")
for key, logMessages := range logs {
if key == "general" {
continue
}
for _, logMessage := range logMessages {
fmt.Fprintln(logFile, logMessage)
}
}
// Now write the general logs at the end of the file.
if generalLogs, ok := logs["general"]; ok {
for _, logMessage := range generalLogs {
fmt.Fprintln(logFile, logMessage)
}
}
}
func processFiles(currentDir, jpegDir string, files []os.DirEntry) map[string][]string {
fmt.Println("Processing files...")
startTime := time.Now()
logs := make(map[string][]string)
fileChan, logChan := setupWorkers(currentDir, jpegDir, len(files))
for _, file := range files {
fileChan <- file
}
close(fileChan)
aggregateLogs(logChan, logs, currentDir, jpegDir, startTime)
return logs
}
func setupWorkers(currentDir, jpegDir string, filesCount int) (chan os.DirEntry, chan map[string]string) {
fileChan := make(chan os.DirEntry, filesCount)
logChan := make(chan map[string]string, filesCount)
var wg sync.WaitGroup
workerCount := runtime.NumCPU()
for i := 0; i < workerCount; i++ {
wg.Add(1)
go worker(fileChan, logChan, currentDir, jpegDir, &wg)
}
go func() {
wg.Wait()
close(logChan)
}()
return fileChan, logChan
}
func worker(fileChan chan os.DirEntry, logChan chan map[string]string, currentDir, jpegDir string, wg *sync.WaitGroup) {
defer wg.Done()
for file := range fileChan {
logChan <- processFile(file, currentDir, jpegDir)
}
}
func processFile(file os.DirEntry, currentDir, jpegDir string) map[string]string {
logEntry := make(map[string]string)
ext := strings.ToLower(filepath.Ext(file.Name()))
if ext == ".heic" {
fmt.Printf("Processing file: %s\n", file.Name())
err := convertFile(currentDir, file.Name(), jpegDir)
if err != nil {
logEntry[file.Name()] = fmt.Sprintf("error details: %s", err)
} else {
logEntry[file.Name()] = "converted successfully"
}
}
return logEntry
}
func aggregateLogs(logChan chan map[string]string, logs map[string][]string, currentDir, jpegDir string, startTime time.Time) {
var totalHEICSize, totalJPEGSize int64
generalLogs := []string{} // Storing general logs here
for logItem := range logChan {
for k := range logItem {
heicFilePath := filepath.Join(currentDir, k)
jpgFilePath := getJPEGFilePath(jpegDir, k)
heicSizeBytes := getFileSize(heicFilePath)
jpgSizeBytes := getFileSize(jpgFilePath)
totalHEICSize += heicSizeBytes
totalJPEGSize += jpgSizeBytes
heicSize := humanReadableFileSize(heicSizeBytes)
jpgSize := humanReadableFileSize(jpgSizeBytes)
logs[k] = append(logs[k], fmt.Sprintf("%s %s > Converted > jpegs/%s.jpg %s", k, heicSize, strings.TrimSuffix(k, filepath.Ext(k)), jpgSize))
}
}
// Add general logs to the generalLogs slice
totalDuration := time.Since(startTime)
totalLogLines := len(logs)
generalLogs = append(generalLogs, fmt.Sprintf("\n%v Files", totalLogLines))
generalLogs = append(generalLogs, fmt.Sprintf("Total Time Taken==%v", totalDuration))
generalLogs = append(generalLogs, fmt.Sprintf("Average Time Per File==%v", totalDuration/time.Duration(totalLogLines)))
generalLogs = append(generalLogs, fmt.Sprintf("Total HEIC File Size==%s", humanReadableFileSize(totalHEICSize)))
generalLogs = append(generalLogs, fmt.Sprintf("Total JPEG Folder Size==%s", humanReadableFileSize(totalJPEGSize)))
// Add the generalLogs slice to the main logs map
logs["general"] = generalLogs
}
func getJPEGFilePath(jpegDir, originalFileName string) string {
return filepath.Join(jpegDir, strings.TrimSuffix(originalFileName, filepath.Ext(originalFileName))+".jpg")
}
func getFileSize(path string) int64 {
fileInfo, err := os.Stat(path)
if err != nil {
return 0
}
return fileInfo.Size()
}
func convertFile(currentDir, inputFileName, jpegDir string) error {
inputFilePath := filepath.Join(currentDir, inputFileName)
outputFileName := strings.TrimSuffix(filepath.Base(inputFileName), filepath.Ext(inputFileName)) + ".jpg"
outputFilePath := filepath.Join(jpegDir, outputFileName)
return convertHeicToJpg(inputFilePath, outputFilePath)
}
func humanReadableFileSize(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%dB", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
func convertHeicToJpg(input, output string) error {
fileInput, err := os.Open(input)
if err != nil {
return err
}
defer fileInput.Close()
exif, err := goheif.ExtractExif(fileInput)
if err != nil {
return err
}
// Seek back to the beginning of the file for the next operation.
fileInput.Seek(0, 0)
img, err := goheif.Decode(fileInput)
if err != nil {
return err
}
fileOutput, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer fileOutput.Close()
w, err := newWriterExif(fileOutput, exif)
if err != nil {
return err
}
return jpeg.Encode(w, img, nil)
}
type writerSkipper struct {
w io.Writer
bytesToSkip int
}
func (w *writerSkipper) Write(data []byte) (int, error) {
if w.bytesToSkip <= 0 {
return w.w.Write(data)
}
dataLen := len(data)
if dataLen < w.bytesToSkip {
w.bytesToSkip -= dataLen
return dataLen, nil
}
n, err := w.w.Write(data[w.bytesToSkip:])
n += w.bytesToSkip
w.bytesToSkip = 0
return n, err
}
func newWriterExif(w io.Writer, exif []byte) (io.Writer, error) {
writer := &writerSkipper{w, 2}
soi := []byte{0xff, 0xd8}
if _, err := w.Write(soi); err != nil {
return nil, err
}
if exif != nil {
app1Marker := 0xe1
markerlen := 2 + len(exif)
marker := []byte{0xff, uint8(app1Marker), uint8(markerlen >> 8), uint8(markerlen & 0xff)}
if _, err := w.Write(marker); err != nil {
return nil, err
}
if _, err := w.Write(exif); err != nil {
return nil, err
}
}
return writer, nil
}