added dynamic loading of favicons
This commit is contained in:
parent
255acb360f
commit
81fb811111
4 changed files with 355 additions and 35 deletions
55
text.go
55
text.go
|
@ -1,7 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -52,12 +55,11 @@ func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string
|
|||
|
||||
elapsedTime := time.Since(startTime)
|
||||
|
||||
// Prepare safe decorated results
|
||||
// Simplified result structure without waiting for favicons
|
||||
type DecoratedResult struct {
|
||||
TextSearchResult
|
||||
FaviconURL string
|
||||
FaviconID string
|
||||
PrettyLink LinkParts
|
||||
FaviconID string // Just the ID, URL will be generated client-side
|
||||
}
|
||||
|
||||
var decoratedResults []DecoratedResult
|
||||
|
@ -66,39 +68,52 @@ func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string
|
|||
continue
|
||||
}
|
||||
|
||||
// First format the link
|
||||
prettyLink := FormatLinkHTML(r.URL)
|
||||
faviconID := faviconIDFromURL(prettyLink.RootURL)
|
||||
faviconURL := getFaviconProxyURL("", prettyLink.RootURL)
|
||||
|
||||
decoratedResults = append(decoratedResults, DecoratedResult{
|
||||
TextSearchResult: r,
|
||||
PrettyLink: prettyLink,
|
||||
FaviconID: faviconID,
|
||||
FaviconURL: faviconURL,
|
||||
})
|
||||
|
||||
// Start async favicon fetch if not already cached
|
||||
go ensureFaviconIsCached(faviconID, prettyLink.RootURL)
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Results": decoratedResults,
|
||||
"Query": query,
|
||||
"Fetched": FormatElapsedTime(elapsedTime),
|
||||
"Page": page,
|
||||
"HasPrevPage": hasPrevPage,
|
||||
"HasNextPage": len(combinedResults) >= 50,
|
||||
"NoResults": len(combinedResults) == 0,
|
||||
"LanguageOptions": languageOptions,
|
||||
"CurrentLang": settings.SearchLanguage,
|
||||
"Theme": settings.Theme,
|
||||
"Safe": settings.SafeSearch,
|
||||
"IsThemeDark": settings.IsThemeDark,
|
||||
"Trans": Translate,
|
||||
"Results": decoratedResults,
|
||||
"Query": query,
|
||||
"Fetched": FormatElapsedTime(elapsedTime),
|
||||
"Page": page,
|
||||
"HasPrevPage": hasPrevPage,
|
||||
"HasNextPage": len(combinedResults) >= 50,
|
||||
"NoResults": len(combinedResults) == 0,
|
||||
"LanguageOptions": languageOptions,
|
||||
"CurrentLang": settings.SearchLanguage,
|
||||
"Theme": settings.Theme,
|
||||
"Safe": settings.SafeSearch,
|
||||
"IsThemeDark": settings.IsThemeDark,
|
||||
"Trans": Translate,
|
||||
"HardCacheEnabled": config.DriveCacheEnabled,
|
||||
}
|
||||
|
||||
// Render the template
|
||||
renderTemplate(w, "text.html", data)
|
||||
}
|
||||
|
||||
func ensureFaviconIsCached(faviconID, rootURL string) {
|
||||
// Check if already exists in cache
|
||||
filename := fmt.Sprintf("%s_thumb.webp", faviconID)
|
||||
cachedPath := filepath.Join(config.DriveCache.Path, "images", filename)
|
||||
|
||||
if _, err := os.Stat(cachedPath); err == nil {
|
||||
return // Already cached
|
||||
}
|
||||
|
||||
// Not cached, initiate download
|
||||
getFaviconProxyURL("", rootURL) // This will trigger async download
|
||||
}
|
||||
|
||||
func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []TextSearchResult {
|
||||
cacheChan := make(chan []SearchResult)
|
||||
var combinedResults []TextSearchResult
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue