Improved icon fetching with DriveCache disabled
Some checks failed
Run Integration Tests / test (push) Failing after 37s

This commit is contained in:
partisan 2025-07-04 15:24:16 +02:00
parent 43d7068c7a
commit b17b9bc05f
6 changed files with 239 additions and 131 deletions

View file

@ -52,8 +52,11 @@ type faviconDownloadRequest struct {
func init() {
// Start 5 worker goroutines to process favicon downloads
for i := 0; i < 5; i++ {
go faviconDownloadWorker()
if !config.DriveCacheEnabled {
for i := 0; i < 5; i++ {
go faviconDownloadWorker()
}
}
}
@ -72,7 +75,7 @@ func faviconIDFromURL(rawURL string) string {
// Resolves favicon URL using multiple methods
func resolveFaviconURL(rawFavicon, pageURL string) (faviconURL, cacheID string) {
cacheID = faviconIDFromURL(pageURL)
// cacheID = faviconIDFromURL(pageURL)
// Handle data URLs first
if strings.HasPrefix(rawFavicon, "data:image") {
@ -135,7 +138,8 @@ func findFaviconInHeaders(pageURL string) string {
client := &http.Client{
Timeout: 3 * time.Second, // like 3 seconds for favicon should be enough
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
},
}
@ -214,7 +218,7 @@ func checkURLExists(url string) bool {
}
// Add User-Agent
userAgent, err := GetUserAgent("Text-Search-Brave")
userAgent, err := GetUserAgent("Text-Search-Favicons")
if err != nil {
printWarn("Error getting User-Agent: %v", err)
}
@ -321,10 +325,6 @@ func getFaviconProxyURL(rawFavicon, pageURL string) string {
filename := fmt.Sprintf("%s_icon.webp", cacheID)
cachedPath := filepath.Join(config.DriveCache.Path, "images", filename)
if _, err := os.Stat(cachedPath); err == nil {
return fmt.Sprintf("/image/%s_icon.webp", cacheID)
}
// Resolve URL
faviconURL, _ := resolveFaviconURL(rawFavicon, pageURL)
if faviconURL == "" {
@ -333,23 +333,33 @@ func getFaviconProxyURL(rawFavicon, pageURL string) string {
}
// Check if already downloading
faviconCache.RLock()
downloading := faviconCache.m[cacheID]
faviconCache.RUnlock()
imageURLMapMu.Lock()
imageURLMap[fmt.Sprintf("%s_icon", cacheID)] = faviconURL
imageURLMapMu.Unlock()
if !downloading {
faviconCache.Lock()
faviconCache.m[cacheID] = true
faviconCache.Unlock()
// Send to download queue instead of starting goroutine
faviconDownloadQueue <- faviconDownloadRequest{
faviconURL: faviconURL,
pageURL: pageURL,
cacheID: cacheID,
if config.DriveCacheEnabled {
if _, err := os.Stat(cachedPath); err == nil {
return fmt.Sprintf("/image/%s_icon.webp", cacheID)
}
faviconCache.RLock()
downloading := faviconCache.m[cacheID]
faviconCache.RUnlock()
if !downloading {
faviconCache.Lock()
faviconCache.m[cacheID] = true
faviconCache.Unlock()
faviconDownloadQueue <- faviconDownloadRequest{
faviconURL: faviconURL,
pageURL: pageURL,
cacheID: cacheID,
}
}
return fmt.Sprintf("/image/%s_icon.webp", cacheID)
}
// Always proxy if cache is off
return fmt.Sprintf("/image/%s_icon.webp", cacheID)
}
@ -451,7 +461,7 @@ func cacheFavicon(imageURL, imageID string) (string, bool, error) {
}
// Add User-Agent
userAgent, err := GetUserAgent("Text-Search-Brave")
userAgent, err := GetUserAgent("Text-Search-Favicons")
if err != nil {
printWarn("Error getting User-Agent: %v", err)
}