-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (65 loc) · 1.98 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
package main
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"sync"
"regexp"
"image-conversion/helpers/constants"
"image-conversion/helpers/fileOperations"
"image-conversion/helpers/helpers"
"image-conversion/helpers/timer"
)
// Only for converting .heic files into .jpg
func main() {
defer timer.FuncTimer("main")()
if _, existErr := os.Stat(constants.OUTPUT_PATH); os.IsNotExist(existErr) {
if mkdirErr := os.Mkdir(constants.OUTPUT_PATH, os.ModePerm); mkdirErr != nil {
log.Fatal(mkdirErr)
}
}
allEntries, err := os.ReadDir(constants.INPUT_PATH)
var imageEntries []fs.DirEntry
validImageExtensions := []string{".jpg", ".jpeg", ".png", ".heic", ".tiff", ".eps", ".bmp"}
for _, e := range allEntries {
fileExtension := strings.ToLower(filepath.Ext(e.Name()))
// Filter out files that aren't images
if helpers.StringInSlice(fileExtension, validImageExtensions) {
imageEntries = append(imageEntries, e)
}
}
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
for _, e := range imageEntries {
inputFileName := fmt.Sprintf("%s/%s", constants.INPUT_PATH, e.Name())
outputFileName := fmt.Sprintf("%s/%s", constants.OUTPUT_PATH, e.Name())
fileWithDesiredOutputType := strings.Contains(
strings.ToLower(inputFileName),
strings.ToLower(fmt.Sprintf(".%s", constants.OUTPUT_FILE_TYPE)),
)
if fileWithDesiredOutputType {
outputFileName = strings.ReplaceAll(inputFileName, constants.INPUT_PATH, constants.OUTPUT_PATH)
wg.Add(1)
go func() {
defer wg.Done()
fileOperations.MoveFile(inputFileName, outputFileName)
}()
} else {
re := regexp.MustCompile(`(?i)heic`)
outputFileName = re.ReplaceAllString(outputFileName, constants.OUTPUT_FILE_TYPE)
wg.Add(1)
go func() {
defer wg.Done()
fileOperations.ImageConvertToTarget(inputFileName, outputFileName)
}()
}
}
log.Println("Waiting for goroutines to complete...")
wg.Wait()
log.Printf("Completed image conversion for %v files", len(imageEntries))
}