edge/pkg/bundle/zim/favicon.go

67 lines
1.4 KiB
Go
Raw Normal View History

2023-07-11 02:42:05 +02:00
package zim
import "github.com/pkg/errors"
2023-10-11 11:18:32 +02:00
func (r *Reader) Favicon() (*ContentEntry, error) {
illustration, err := r.getMetadataIllustration()
2023-07-11 02:42:05 +02:00
if err != nil && !errors.Is(err, ErrNotFound) {
return nil, errors.WithStack(err)
}
if illustration != nil {
return illustration, nil
}
2023-10-11 11:18:32 +02:00
namespaces := []Namespace{V5NamespaceLayout, V5NamespaceImageFile}
urls := []string{"favicon", "favicon.png"}
2023-07-11 02:42:05 +02:00
for _, ns := range namespaces {
2023-10-11 11:18:32 +02:00
for _, url := range urls {
entry, err := r.EntryWithURL(ns, url)
2023-07-11 02:42:05 +02:00
if err != nil && !errors.Is(err, ErrNotFound) {
return nil, errors.WithStack(err)
}
2023-10-11 11:18:32 +02:00
if errors.Is(err, ErrNotFound) {
continue
2023-07-11 02:42:05 +02:00
}
2023-10-11 11:18:32 +02:00
content, err := entry.Redirect()
if err != nil {
return nil, errors.WithStack(err)
}
return content, nil
2023-07-11 02:42:05 +02:00
}
}
return nil, errors.WithStack(ErrNotFound)
}
2023-10-11 11:18:32 +02:00
func (r *Reader) getMetadataIllustration() (*ContentEntry, error) {
keys := []MetadataKey{MetadataIllustration96x96at2, MetadataIllustration48x48at1}
metadata, err := r.Metadata(keys...)
2023-07-11 02:42:05 +02:00
if err != nil {
return nil, errors.WithStack(err)
}
2023-10-11 11:18:32 +02:00
for _, k := range keys {
if _, exists := metadata[k]; exists {
entry, err := r.EntryWithURL(V5NamespaceMetadata, string(k))
if err != nil {
return nil, errors.WithStack(err)
}
content, err := entry.Redirect()
if err != nil {
return nil, errors.WithStack(err)
}
2023-07-11 02:42:05 +02:00
2023-10-11 11:18:32 +02:00
return content, nil
}
2023-07-11 02:42:05 +02:00
}
return nil, errors.WithStack(ErrNotFound)
}