mirror of
https://github.com/miniflux/v2.git
synced 2025-09-15 18:57:04 +00:00
Add support for data URL favicons
This commit is contained in:
parent
f546552a1d
commit
f6a5d7d6ed
2 changed files with 96 additions and 0 deletions
|
@ -5,9 +5,11 @@
|
|||
package icon
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/miniflux/miniflux/helper"
|
||||
"github.com/miniflux/miniflux/http"
|
||||
|
@ -36,6 +38,10 @@ func FindIcon(websiteURL string) (*model.Icon, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if strings.HasPrefix(iconURL, "data:") {
|
||||
return parseImageDataURL(iconURL)
|
||||
}
|
||||
|
||||
logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
|
||||
icon, err := downloadIcon(iconURL)
|
||||
if err != nil {
|
||||
|
@ -108,3 +114,42 @@ func downloadIcon(iconURL string) (*model.Icon, error) {
|
|||
|
||||
return icon, nil
|
||||
}
|
||||
|
||||
func parseImageDataURL(value string) (*model.Icon, error) {
|
||||
colon := strings.Index(value, ":")
|
||||
semicolon := strings.Index(value, ";")
|
||||
comma := strings.Index(value, ",")
|
||||
|
||||
if colon <= 0 || semicolon <= 0 || comma <= 0 {
|
||||
return nil, fmt.Errorf(`icon: invalid data url "%s"`, value)
|
||||
}
|
||||
|
||||
mimeType := value[colon+1 : semicolon]
|
||||
encoding := value[semicolon+1 : comma]
|
||||
data := value[comma+1:]
|
||||
|
||||
if encoding != "base64" {
|
||||
return nil, fmt.Errorf(`icon: unsupported data url encoding "%s"`, value)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(mimeType, "image/") {
|
||||
return nil, fmt.Errorf(`icon: invalid mime type "%s"`, mimeType)
|
||||
}
|
||||
|
||||
blob, err := base64.StdEncoding.DecodeString(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`icon: invalid data "%s" (%v)`, value, err)
|
||||
}
|
||||
|
||||
if len(blob) == 0 {
|
||||
return nil, fmt.Errorf(`icon: empty data "%s"`, value)
|
||||
}
|
||||
|
||||
icon := &model.Icon{
|
||||
Hash: helper.HashFromBytes(blob),
|
||||
Content: blob,
|
||||
MimeType: mimeType,
|
||||
}
|
||||
|
||||
return icon, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue