i need better way to cache images

This commit is contained in:
partisan 2024-09-12 22:11:39 +02:00
parent 61af6fc453
commit d107d41d72
8 changed files with 59 additions and 33 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"sync"
"time"
)
@ -141,19 +142,37 @@ func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchR
return nil, 0, fmt.Errorf("decoding response: %v", err)
}
var results []ImageSearchResult
for _, item := range apiResp.Data.Result.Items {
results = append(results, ImageSearchResult{
Thumbnail: item.Thumbnail,
Title: item.Title,
Media: item.Media,
Source: item.Url,
ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Media), // New proxy not exactly working as intended
Width: item.Width,
Height: item.Height,
})
var wg sync.WaitGroup
results := make([]ImageSearchResult, len(apiResp.Data.Result.Items))
for i, item := range apiResp.Data.Result.Items {
wg.Add(1)
go func(i int, item struct {
Media string `json:"media"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
Url string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
}) {
defer wg.Done()
// Populate the result
results[i] = ImageSearchResult{
Thumbnail: item.Thumbnail,
Title: item.Title,
Media: item.Media,
Source: item.Url,
ThumbProxy: "/imgproxy?url=" + item.Media,
Width: item.Width,
Height: item.Height,
}
}(i, item)
}
// Wait for all goroutines to complete
wg.Wait()
duration := time.Since(startTime) // Calculate the duration
return results, duration, nil