stability enhancement

1. large dimension photos
2. GET /1.webp
3. dependabot
This commit is contained in:
BennyThink 2021-12-04 17:15:54 +08:00
parent e9543522ae
commit 7c65d5e213
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
9 changed files with 31 additions and 14 deletions

View File

@ -1,6 +1,6 @@
version: 2
updates:
- package-ecosystem: ""
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"

View File

@ -53,3 +53,8 @@ RestartSec=3s
[Install]
WantedBy=multi-user.target`
)
const (
webpMax = 16383
avifMax = 65536
)

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
@ -25,6 +26,7 @@ func convertFilter(raw, avifPath, webpPath string, c chan int) {
if !imageExists(avifPath) {
convertImage(raw, avifPath, "avif")
}
if !imageExists(webpPath) {
convertImage(raw, webpPath, "webp")
}
@ -65,7 +67,7 @@ func convertImage(raw, optimized, itype string) {
}
func readRawImage(imgPath string) (img image.Image, err error) {
func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
data, err := ioutil.ReadFile(imgPath)
if err != nil {
log.Errorln(err)
@ -73,19 +75,26 @@ func readRawImage(imgPath string) (img image.Image, err error) {
contentType := getFileContentType(data[:512])
if strings.Contains(contentType, "jpeg") {
img, _ = jpeg.Decode(bytes.NewReader(data))
img, err = jpeg.Decode(bytes.NewReader(data))
} else if strings.Contains(contentType, "png") {
img, _ = png.Decode(bytes.NewReader(data))
img, err = png.Decode(bytes.NewReader(data))
} else if strings.Contains(contentType, "bmp") {
img, _ = bmp.Decode(bytes.NewReader(data))
img, err = bmp.Decode(bytes.NewReader(data))
}
if img == nil {
errinfo := "image file " + path.Base(imgPath) + " is corrupted or not supported"
if err != nil {
errinfo := fmt.Sprintf("image file %s is corrupted: %v", imgPath, err)
log.Errorln(errinfo)
return nil, errors.New(errinfo)
}
x, y := img.Bounds().Max.X, img.Bounds().Max.Y
if x > maxPixel || y > maxPixel {
errinfo := fmt.Sprintf("WebP: %s(%dx%d) is too large", imgPath, x, y)
log.Warnf(errinfo)
return nil, errors.New(errinfo)
}
return img, nil
}
@ -95,7 +104,8 @@ func avifEncoder(p1, p2 string, quality float32) {
if err != nil {
log.Fatalf("Can't create destination file: %v", err)
}
img, err = readRawImage(p1)
// AVIF has a maximum resolution of 65536 x 65536 pixels.
img, err = readRawImage(p1, avifMax)
if err != nil {
return
}
@ -113,8 +123,8 @@ func webpEncoder(p1, p2 string, quality float32) {
// if convert fails, return error; success nil
var buf bytes.Buffer
var img image.Image
img, err := readRawImage(p1)
// The maximum pixel dimensions of a WebP image is 16383 x 16383.
img, err := readRawImage(p1, webpMax)
if err != nil {
return
}

View File

@ -211,7 +211,11 @@ func findSmallestFiles(files []string) string {
var small int64
var final string
for _, f := range files {
stat, _ := os.Stat(f)
stat, err := os.Stat(f)
if err != nil {
log.Warnf("%s not found on filesystem", f)
continue
}
if stat.Size() < small || small == 0 {
small = stat.Size()
final = f

BIN
pics/big.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

BIN
pics/img_over_16383px.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

BIN
pics/jpg_without_eoi.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

View File

@ -23,7 +23,6 @@ func prefetchImages(confImgPath string, ExhaustPath string) {
//prefetch, recursive through the dir
all := fileCount(confImgPath)
var bar = progressbar.Default(all, "Prefetching...")
//count := 0
err := filepath.Walk(confImgPath,
func(picAbsPath string, info os.FileInfo, err error) error {
if err != nil {

View File

@ -91,8 +91,7 @@ func convert(c *fiber.Ctx) error {
}
}
var finalFile string
finalFile = findSmallestFiles(availableFiles)
var finalFile = findSmallestFiles(availableFiles)
etag := genEtag(finalFile)
c.Set("ETag", etag)
c.Set("X-Compression-Rate", getCompressionRate(rawImageAbs, finalFile))