mirror of
https://github.com/miniflux/v2.git
synced 2025-08-16 18:01:37 +00:00
feat(icon): add resizing support for webp images
This commit is contained in:
parent
6c60d61f36
commit
261b72f149
2 changed files with 20 additions and 2 deletions
|
@ -27,6 +27,7 @@ import (
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
"golang.org/x/image/draw"
|
"golang.org/x/image/draw"
|
||||||
|
"golang.org/x/image/webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type iconFinder struct {
|
type iconFinder struct {
|
||||||
|
@ -195,8 +196,8 @@ func (f *iconFinder) downloadIcon(iconURL string) (*model.Icon, error) {
|
||||||
func resizeIcon(icon *model.Icon) *model.Icon {
|
func resizeIcon(icon *model.Icon) *model.Icon {
|
||||||
r := bytes.NewReader(icon.Content)
|
r := bytes.NewReader(icon.Content)
|
||||||
|
|
||||||
if !slices.Contains([]string{"image/jpeg", "image/png", "image/gif"}, icon.MimeType) {
|
if !slices.Contains([]string{"image/jpeg", "image/png", "image/gif", "image/webp"}, icon.MimeType) {
|
||||||
slog.Info("icon isn't a png/gif/jpeg, can't resize", slog.String("mimetype", icon.MimeType))
|
slog.Info("icon isn't a png/gif/jpeg/webp, can't resize", slog.String("mimetype", icon.MimeType))
|
||||||
return icon
|
return icon
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,6 +222,8 @@ func resizeIcon(icon *model.Icon) *model.Icon {
|
||||||
src, err = png.Decode(r)
|
src, err = png.Decode(r)
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
src, err = gif.Decode(r)
|
src, err = gif.Decode(r)
|
||||||
|
case "image/webp":
|
||||||
|
src, err = webp.Decode(r)
|
||||||
}
|
}
|
||||||
if err != nil || src == nil {
|
if err != nil || src == nil {
|
||||||
slog.Warn("unable to decode the icon", slog.Any("error", err))
|
slog.Warn("unable to decode the icon", slog.Any("error", err))
|
||||||
|
|
|
@ -170,6 +170,21 @@ func TestResizeIconPng(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResizeIconWebp(t *testing.T) {
|
||||||
|
data, err := base64.StdEncoding.DecodeString("UklGRkAAAABXRUJQVlA4IDQAAADwAQCdASoBAAEAAQAcJaACdLoB+AAETAAA/vW4f/6aR40jxpHxcP/ugT90CfugT/3NoAAA")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
icon := model.Icon{
|
||||||
|
Content: data,
|
||||||
|
MimeType: "image/webp",
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(icon.Content, resizeIcon(&icon).Content) {
|
||||||
|
t.Fatalf("Converted webp smaller than 16x16")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResizeInvalidImage(t *testing.T) {
|
func TestResizeInvalidImage(t *testing.T) {
|
||||||
icon := model.Icon{
|
icon := model.Icon{
|
||||||
Content: []byte("invalid data"),
|
Content: []byte("invalid data"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue