mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Added custom log level with -v.
Fix bug caused by hard code of exhaust path. Bump version to 0.1.1.
This commit is contained in:
parent
bc7ac93123
commit
7bdd0812f6
17
encoder.go
17
encoder.go
@ -3,17 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/chai2010/webp"
|
|
||||||
"golang.org/x/image/bmp"
|
|
||||||
"image"
|
"image"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/chai2010/webp"
|
||||||
|
"golang.org/x/image/bmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
|
func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err error) {
|
||||||
@ -41,25 +42,25 @@ func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro
|
|||||||
|
|
||||||
if img == nil {
|
if img == nil {
|
||||||
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
msg := "image file " + path.Base(p1) + " is corrupted or not supported"
|
||||||
log.Println(msg)
|
log.Info(msg)
|
||||||
err = errors.New(msg)
|
err = errors.New(msg)
|
||||||
ChanErr(c)
|
ChanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
|
if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil {
|
||||||
log.Println(err)
|
log.Info(err)
|
||||||
ChanErr(c)
|
ChanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil {
|
if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil {
|
||||||
log.Println(err)
|
log.Warn(err)
|
||||||
ChanErr(c)
|
ChanErr(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if Log {
|
if Log {
|
||||||
fmt.Printf("Save to %s ok\n", p2)
|
log.Warn("Save to %s ok\n", p2)
|
||||||
}
|
}
|
||||||
|
|
||||||
ChanErr(c)
|
ChanErr(c)
|
||||||
|
1
go.mod
1
go.mod
@ -5,5 +5,6 @@ go 1.13
|
|||||||
require (
|
require (
|
||||||
github.com/chai2010/webp v1.1.0
|
github.com/chai2010/webp v1.1.0
|
||||||
github.com/gofiber/fiber v1.4.0
|
github.com/gofiber/fiber v1.4.0
|
||||||
|
github.com/sirupsen/logrus v1.4.2
|
||||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
|
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
|
||||||
)
|
)
|
||||||
|
@ -2,12 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) {
|
func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY string) func(c *fiber.Ctx) {
|
||||||
@ -51,7 +52,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cwd, WebpAbsPath := GenWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI)
|
_, WebpAbsPath := GenWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI)
|
||||||
|
|
||||||
if ImageExists(WebpAbsPath) {
|
if ImageExists(WebpAbsPath) {
|
||||||
finalFile = WebpAbsPath
|
finalFile = WebpAbsPath
|
||||||
@ -59,7 +60,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
// we don't have abc.jpg.png1582558990.webp
|
// we don't have abc.jpg.png1582558990.webp
|
||||||
// delete the old pic and convert a new one.
|
// delete the old pic and convert a new one.
|
||||||
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
|
// /home/webp_server/exhaust/path/to/tsuki.jpg.1582558990.webp
|
||||||
destHalfFile := path.Clean(path.Join(cwd, "exhaust", path.Dir(reqURI), ImgFilename))
|
destHalfFile := path.Clean(path.Join(WebpAbsPath, path.Dir(reqURI), ImgFilename))
|
||||||
matches, err := filepath.Glob(destHalfFile + "*")
|
matches, err := filepath.Glob(destHalfFile + "*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
@ -76,7 +77,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY
|
|||||||
//for webp, we need to create dir first
|
//for webp, we need to create dir first
|
||||||
_ = os.MkdirAll(path.Dir(WebpAbsPath), 0755)
|
_ = os.MkdirAll(path.Dir(WebpAbsPath), 0755)
|
||||||
q, _ := strconv.ParseFloat(QUALITY, 32)
|
q, _ := strconv.ParseFloat(QUALITY, 32)
|
||||||
err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), true, nil)
|
err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), verboseMode, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
@ -4,11 +4,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/gofiber/fiber"
|
"github.com/gofiber/fiber"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,13 +22,14 @@ type Config struct {
|
|||||||
ExhaustPath string `json:"EXHAUST_PATH"`
|
ExhaustPath string `json:"EXHAUST_PATH"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const version = "0.1.0"
|
const version = "0.1.1"
|
||||||
|
|
||||||
var configPath string
|
var configPath string
|
||||||
var prefetch bool
|
var prefetch bool
|
||||||
var jobs int
|
var jobs int
|
||||||
var dumpConfig bool
|
var dumpConfig bool
|
||||||
var dumpSystemd bool
|
var dumpSystemd bool
|
||||||
|
var verboseMode bool
|
||||||
|
|
||||||
const sampleConfig = `
|
const sampleConfig = `
|
||||||
{
|
{
|
||||||
@ -76,10 +78,20 @@ func init() {
|
|||||||
flag.IntVar(&jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
|
flag.IntVar(&jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
|
||||||
flag.BoolVar(&dumpConfig, "dump-config", false, "Print sample config.json")
|
flag.BoolVar(&dumpConfig, "dump-config", false, "Print sample config.json")
|
||||||
flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
|
flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
|
||||||
|
flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// Logrus
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
if verboseMode {
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
} else {
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
}
|
||||||
|
|
||||||
// process cli params
|
// process cli params
|
||||||
if dumpConfig {
|
if dumpConfig {
|
||||||
fmt.Println(sampleConfig)
|
fmt.Println(sampleConfig)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user