webp_server_go/encoder/prefetch.go
Nova Kwok 43c275e3ec
Adds JPEG XL support, max_height/max_width support (#321)
* WIP JXL

* Fix test

* Tries to fix autobuild

* Tries to fix autobuild

* Add setup go in codeql

* Bump actions version

* Do not print curl output in CI

* Do not print curl output in CI

* Remove Metadata on RAW image

* Update sample config

* better loop

* Prefetch should also respect AllowedType

* Better Export params and UA handle

* Only do conversion on supported formats

* CONVERT_TYPES default to webp only

* CONVERT_TYPES default to webp only

* Add GIF to AllowedTypes

* Update README
2024-03-22 15:12:09 +08:00

68 lines
1.7 KiB
Go

package encoder
import (
"fmt"
"os"
"path"
"path/filepath"
"time"
"webp_server_go/config"
"webp_server_go/helper"
"github.com/schollz/progressbar/v3"
log "github.com/sirupsen/logrus"
)
func PrefetchImages() {
// maximum ongoing prefetch is depending on your core of CPU
var sTime = time.Now()
log.Infof("Prefetching using %d cores", config.Jobs)
var finishChan = make(chan int, config.Jobs)
for range config.Jobs {
finishChan <- 1
}
//prefetch, recursive through the dir
all := helper.FileCount(config.Config.ImgPath)
var bar = progressbar.Default(all, "Prefetching...")
err := filepath.Walk(config.Config.ImgPath,
func(picAbsPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !helper.CheckAllowedType(picAbsPath) {
return nil
}
// RawImagePath string, ImgFilename string, reqURI string
metadata := helper.ReadMetadata(picAbsPath, "", config.LocalHostAlias)
avifAbsPath, webpAbsPath, jxlAbsPath := helper.GenOptimizedAbsPath(metadata, config.LocalHostAlias)
// Using avifAbsPath here is the same as using webpAbsPath/jxlAbsPath
_ = os.MkdirAll(path.Dir(avifAbsPath), 0755)
log.Infof("Prefetching %s", picAbsPath)
// Allow all supported formats
supported := map[string]bool{
"raw": true,
"webp": true,
"avif": true,
"jxl": true,
}
go ConvertFilter(picAbsPath, jxlAbsPath, avifAbsPath, webpAbsPath, config.ExtraParams{Width: 0, Height: 0}, supported, finishChan)
_ = bar.Add(<-finishChan)
return nil
})
if err != nil {
log.Errorln(err)
}
elapsed := time.Since(sTime)
_, _ = fmt.Fprintf(os.Stdout, "Prefetch complete in %s\n\n", elapsed)
}