diff --git a/encoder.go b/encoder.go index e79ffdb..e166d78 100644 --- a/encoder.go +++ b/encoder.go @@ -3,17 +3,18 @@ package main import ( "bytes" "errors" - "fmt" - "github.com/chai2010/webp" - "golang.org/x/image/bmp" "image" "image/gif" "image/jpeg" "image/png" "io/ioutil" - "log" "path" "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) { @@ -41,25 +42,25 @@ func WebpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro if img == nil { msg := "image file " + path.Base(p1) + " is corrupted or not supported" - log.Println(msg) + log.Info(msg) err = errors.New(msg) ChanErr(c) return } if err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}); err != nil { - log.Println(err) + log.Info(err) ChanErr(c) return } if err = ioutil.WriteFile(p2, buf.Bytes(), 0755); err != nil { - log.Println(err) + log.Warn(err) ChanErr(c) return } if Log { - fmt.Printf("Save to %s ok\n", p2) + log.Warn("Save to %s ok\n", p2) } ChanErr(c) diff --git a/go.mod b/go.mod index dc9e825..c5844a6 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.13 require ( github.com/chai2010/webp v1.1.0 github.com/gofiber/fiber v1.4.0 + github.com/sirupsen/logrus v1.4.2 golang.org/x/image v0.0.0-20200119044424-58c23975cae1 ) diff --git a/router.go b/router.go index 1e2c5f8..3f5bec8 100644 --- a/router.go +++ b/router.go @@ -2,12 +2,13 @@ package main import ( "fmt" - "github.com/gofiber/fiber" "os" "path" "path/filepath" "strconv" "strings" + + "github.com/gofiber/fiber" ) 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 } - cwd, WebpAbsPath := GenWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI) + _, WebpAbsPath := GenWebpAbs(RawImageAbs, ExhaustPath, ImgFilename, reqURI) if ImageExists(WebpAbsPath) { finalFile = WebpAbsPath @@ -59,7 +60,7 @@ func Convert(ImgPath string, ExhaustPath string, AllowedTypes []string, QUALITY // we don't have abc.jpg.png1582558990.webp // delete the old pic and convert a new one. // /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 + "*") if err != nil { 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 _ = os.MkdirAll(path.Dir(WebpAbsPath), 0755) q, _ := strconv.ParseFloat(QUALITY, 32) - err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), true, nil) + err = WebpEncoder(RawImageAbs, WebpAbsPath, float32(q), verboseMode, nil) if err != nil { fmt.Println(err) diff --git a/webp-server.go b/webp-server.go index 73a9170..6b52f4c 100644 --- a/webp-server.go +++ b/webp-server.go @@ -4,11 +4,12 @@ import ( "encoding/json" "flag" "fmt" - "log" "os" "path" "runtime" + log "github.com/sirupsen/logrus" + "github.com/gofiber/fiber" ) @@ -21,13 +22,14 @@ type Config struct { ExhaustPath string `json:"EXHAUST_PATH"` } -const version = "0.1.0" +const version = "0.1.1" var configPath string var prefetch bool var jobs int var dumpConfig bool var dumpSystemd bool +var verboseMode bool const sampleConfig = ` { @@ -76,10 +78,20 @@ func init() { flag.IntVar(&jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.") flag.BoolVar(&dumpConfig, "dump-config", false, "Print sample config.json") flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.") + flag.BoolVar(&verboseMode, "v", false, "Verbose, print out debug info.") flag.Parse() } func main() { + + // Logrus + log.SetOutput(os.Stdout) + if verboseMode { + log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(log.WarnLevel) + } + // process cli params if dumpConfig { fmt.Println(sampleConfig)