forked from webp-sh/webp_server_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefetch.go
More file actions
51 lines (45 loc) · 1.27 KB
/
Copy pathprefetch.go
File metadata and controls
51 lines (45 loc) · 1.27 KB
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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
)
func prefetchImages(confImgPath string, ExhaustPath string) {
// maximum ongoing prefetch is depending on your core of CPU
var sTime = time.Now()
log.Infof("Prefetching using %d cores", jobs)
var finishChan = make(chan int, jobs)
for i := 0; i < jobs; i++ {
finishChan <- 1
}
//prefetch, recursive through the dir
all := fileCount(confImgPath)
var bar = progressbar.Default(all, "Prefetching...")
err := filepath.Walk(confImgPath,
func(picAbsPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// RawImagePath string, ImgFilename string, reqURI string
proposedURI := strings.Replace(picAbsPath, confImgPath, "", 1)
avif, webp := genOptimizedAbsPath(picAbsPath, ExhaustPath, info.Name(), proposedURI)
_ = os.MkdirAll(path.Dir(avif), 0755)
log.Infof("Prefetching %s", picAbsPath)
go convertFilter(picAbsPath, avif, webp, finishChan)
_ = bar.Add(<-finishChan)
return nil
})
if err != nil {
log.Errorln(err)
}
elapsed := time.Since(sTime)
_, _ = fmt.Fprintf(os.Stdout, "Prefetch completeY(^_^)Y in %s\n\n", elapsed)
}