mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
refactor(idlewatcher): streamline loading screen favicon handling
This commit is contained in:
@@ -47,9 +47,6 @@ func isFaviconPath(path string) bool {
|
|||||||
return path == "/favicon.ico"
|
return path == "/favicon.ico"
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:linkname GetFavIconFromAlias v1.GetFavIconFromAlias
|
|
||||||
func GetFavIconFromAlias(ctx context.Context, alias string) (homepage.FetchResult, error)
|
|
||||||
|
|
||||||
func (w *Watcher) redirectToStartEndpoint(rw http.ResponseWriter, r *http.Request) {
|
func (w *Watcher) redirectToStartEndpoint(rw http.ResponseWriter, r *http.Request) {
|
||||||
uri := "/"
|
uri := "/"
|
||||||
if w.cfg.StartEndpoint != "" {
|
if w.cfg.StartEndpoint != "" {
|
||||||
@@ -58,6 +55,25 @@ func (w *Watcher) redirectToStartEndpoint(rw http.ResponseWriter, r *http.Reques
|
|||||||
http.Redirect(rw, r, uri, http.StatusTemporaryRedirect)
|
http.Redirect(rw, r, uri, http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) getFavIcon(ctx context.Context) (result homepage.FetchResult, err error) {
|
||||||
|
r := w.route
|
||||||
|
hp := r.HomepageItem()
|
||||||
|
if hp.Icon != nil {
|
||||||
|
if hp.Icon.IconSource == homepage.IconSourceRelative {
|
||||||
|
result, err = homepage.FindIcon(ctx, r, *hp.Icon.FullURL)
|
||||||
|
} else {
|
||||||
|
result, err = homepage.FetchFavIconFromURL(ctx, hp.Icon)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// try extract from "link[rel=icon]"
|
||||||
|
result, err = homepage.FindIcon(ctx, r, "/")
|
||||||
|
}
|
||||||
|
if result.StatusCode == 0 {
|
||||||
|
result.StatusCode = http.StatusOK
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldNext bool) {
|
func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldNext bool) {
|
||||||
w.resetIdleTimer()
|
w.resetIdleTimer()
|
||||||
|
|
||||||
@@ -68,7 +84,7 @@ func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldN
|
|||||||
|
|
||||||
// handle favicon request
|
// handle favicon request
|
||||||
if isFaviconPath(r.URL.Path) {
|
if isFaviconPath(r.URL.Path) {
|
||||||
result, err := GetFavIconFromAlias(r.Context(), w.route.Name())
|
result, err := w.getFavIcon(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rw.WriteHeader(result.StatusCode)
|
rw.WriteHeader(result.StatusCode)
|
||||||
fmt.Fprint(rw, err)
|
fmt.Fprint(rw, err)
|
||||||
@@ -76,6 +92,7 @@ func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldN
|
|||||||
}
|
}
|
||||||
rw.Header().Set("Content-Type", result.ContentType())
|
rw.Header().Set("Content-Type", result.ContentType())
|
||||||
rw.WriteHeader(result.StatusCode)
|
rw.WriteHeader(result.StatusCode)
|
||||||
|
rw.Write(result.Icon)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user