-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
236 lines (204 loc) · 5.77 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
package main
import (
"flag"
"fmt"
"image/png"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"github.com/adrium/goheif"
"github.com/danbrakeley/frog"
)
// (go tool nm) -ldflags '-X "main.Version=${{version}}"'
var Version string = "unknown"
// (go tool nm) -ldflags '-X "main.BuildTimestamp=${{date -u +"%Y-%m-%dT%H:%M:%SZ"}}"'
var BuildTimestamp string = "unknown"
func main() {
// this one line main() ensures os.Exit is only called after all defers have run
os.Exit(main_())
}
func main_() int {
var findAll bool
var singleFile string
var numWorkers int
var deleteOnSuccess bool
var forceOverwrite bool
var version bool
flag.BoolVar(&findAll, "all", false, "")
flag.BoolVar(&findAll, "a", false, "")
flag.BoolVar(&deleteOnSuccess, "delete", false, "")
flag.StringVar(&singleFile, "file", "", "")
flag.StringVar(&singleFile, "f", "", "")
flag.BoolVar(&forceOverwrite, "overwrite", false, "")
flag.IntVar(&numWorkers, "procs", runtime.NumCPU(), "")
flag.IntVar(&numWorkers, "p", runtime.NumCPU(), "")
flag.BoolVar(&version, "version", false, "")
flag.BoolVar(&version, "v", false, "")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage of heic2png:
-a, --all · · · · · find all *.heic files and convert them to *.png
--delete · · · · · delete original heic file after successfull conversion
-f, --file <filename> specify a single heic file to convert to .png
--overwrite · · · · if target png file already exists, overwrite it
-p, --procs <num_procs> max number of files to process in parallel (default %d)
-v, --version · · · · · print version information and exit
-h, --help · · · · · print this message and exit
`, runtime.NumCPU())
}
flag.Parse()
// print version info and exit
if version {
fmt.Printf("Version: %s\nBuilt On: %s\n", Version, BuildTimestamp)
return 0
}
if !findAll && len(singleFile) == 0 {
fmt.Fprintf(os.Stderr, "no file(s) specified\n")
flag.Usage()
return -1
}
log := frog.New(frog.Auto, frog.POFieldIndent(30))
defer log.Close()
// build list of files
var filelist []string
switch {
case len(singleFile) > 0:
filelist = append(filelist, singleFile)
case findAll:
// get local directory
cwd, err := os.Getwd()
if err != nil {
log.Error("os.Getwd() had error", frog.Err(err))
return -1
}
files, err := os.ReadDir(cwd)
if err != nil {
log.Error("os.ReadDir(cwd) had error", frog.String("cwd", cwd), frog.Err(err))
return -1
}
filelist = make([]string, 0, len(files))
for _, v := range files {
if v.IsDir() {
continue
}
name := v.Name()
if !strings.HasSuffix(strings.ToLower(filepath.Ext(name)), ".heic") {
continue
}
filelist = append(filelist, v.Name())
}
}
if len(filelist) == 0 {
log.Warning("no files found, nothing to do")
return 0
}
// only spin up as many workers as we'll actually need
if numWorkers > len(filelist) {
numWorkers = len(filelist)
}
var wg sync.WaitGroup
chTasks := make(chan Task)
var numErrs int32
// spin up workers
log.Info("starting", frog.Int("num_workers", numWorkers), frog.String("version", Version), frog.String("built_on", BuildTimestamp))
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go func(thread int) {
defer wg.Done()
l := frog.AddAnchor(log)
defer frog.RemoveAnchor(l)
// loop until channel is closed
for t := range chTasks {
fid := frog.Int("worker_id", thread)
fheic := frog.String("heic_path", t.HeicPath)
fpng := frog.String("png_path", t.PngPath)
err := convertHeicToPng(t.HeicPath, t.PngPath, forceOverwrite, func(step, max int) {
l.Transient("converting: "+progressBar(step, max, '·', '☆'), fheic, fpng, fid)
})
if err != nil {
l.Error("conversion failed", fheic, fpng, frog.Err(err), fid)
atomic.AddInt32(&numErrs, 1)
continue
}
l.Info("image converted", fheic, fpng, fid)
if deleteOnSuccess {
l.Transient("deleting", fheic, fid)
if err = os.Remove(t.HeicPath); err != nil {
l.Error("delete failed", fheic, frog.Err(err), fid)
atomic.AddInt32(&numErrs, 1)
continue
}
l.Info("original image deleted", fheic, fid)
}
}
}(i)
}
// send work to workers
for _, v := range filelist {
chTasks <- Task{
HeicPath: v,
PngPath: removeExt(v) + ".png",
}
}
close(chTasks)
wg.Wait()
log.Info("done", frog.Int32("num_errors", numErrs))
// use the worker error count as the exit status
return int(numErrs)
}
type Task struct {
HeicPath string
PngPath string
}
func convertHeicToPng(filenameIn, filenameOut string, forceOverwrite bool, fnProgress func(step, max int)) error {
if fnProgress == nil {
fnProgress = func(_, _ int) {}
}
fnProgress(0, 4)
fIn, err := os.Open(filenameIn)
if err != nil {
return fmt.Errorf("unable to open %s: %w", filenameIn, err)
}
defer fIn.Close()
fnProgress(1, 4)
img, err := goheif.Decode(fIn)
if err != nil {
return fmt.Errorf("unable to decode %s: %w", filenameIn, err)
}
fnProgress(2, 4)
var fOut *os.File
if forceOverwrite {
fOut, err = os.Create(filenameOut)
} else {
fOut, err = os.OpenFile(filenameOut, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666)
}
if err != nil {
return fmt.Errorf("unable to create %s: %v", filenameOut, err)
}
defer fOut.Close()
fnProgress(3, 4)
pngenc := png.Encoder{CompressionLevel: png.BestSpeed}
err = pngenc.Encode(fOut, img)
if err != nil {
return fmt.Errorf("unable to encode %s: %w", filenameOut, err)
}
fnProgress(4, 4)
return nil
}
func progressBar(cur, max int, empty, full rune) string {
var sb strings.Builder
sb.Grow(max * 4)
for i := 0; i < max; i++ {
if i < cur {
sb.WriteRune(full)
} else {
sb.WriteRune(empty)
}
}
return sb.String()
}
func removeExt(p string) string {
return strings.TrimSuffix(p, filepath.Ext(p))
}