Search/images.go

281 lines
9.4 KiB
Go
Raw Normal View History

2024-08-13 16:31:28 +02:00
package main
import (
2024-10-13 00:04:46 +02:00
"crypto/md5"
"encoding/hex"
2024-08-13 16:31:28 +02:00
"fmt"
"net/http"
"time"
)
var imageSearchEngines []SearchEngine
func init() {
imageSearchEngines = []SearchEngine{
2024-10-14 22:15:38 +02:00
{Name: "Qwant", Func: wrapImageSearchFunc(PerformQwantImageSearch)},
{Name: "Bing", Func: wrapImageSearchFunc(PerformBingImageSearch)},
{Name: "DeviantArt", Func: wrapImageSearchFunc(PerformDeviantArtImageSearch)},
2024-09-12 22:11:39 +02:00
//{Name: "Imgur", Func: wrapImageSearchFunc(PerformImgurImageSearch), Weight: 4}, // Image proxy not working
2024-08-13 16:31:28 +02:00
}
}
2024-11-13 16:59:42 +01:00
func handleImageSearch(w http.ResponseWriter, r *http.Request, settings UserSettings, query string, page int) {
2024-08-13 16:31:28 +02:00
startTime := time.Now()
2024-11-13 16:59:42 +01:00
cacheKey := CacheKey{
Query: query,
Page: page,
Safe: settings.SafeSearch == "active",
Lang: settings.SearchLanguage,
Type: "image",
}
// Check if JavaScript is disabled
jsDisabled := r.URL.Query().Get("js_disabled") == "true"
// Determine if we should cache images synchronously
synchronous := jsDisabled
combinedResults := getImageResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.SearchLanguage, page, synchronous)
2024-08-13 16:31:28 +02:00
elapsedTime := time.Since(startTime)
// Prepare the data to pass to the template
data := map[string]interface{}{
"Results": combinedResults,
"Query": query,
2024-11-13 16:59:42 +01:00
"Fetched": fmt.Sprintf("%.2f %s", elapsedTime.Seconds(), Translate("seconds")),
"Page": page,
"HasPrevPage": page > 1,
"HasNextPage": len(combinedResults) >= 50,
"NoResults": len(combinedResults) == 0,
"LanguageOptions": languageOptions,
"CurrentLang": settings.SearchLanguage,
"Theme": settings.Theme,
"Safe": settings.SafeSearch,
"IsThemeDark": settings.IsThemeDark,
2024-11-26 07:46:03 +01:00
"HardCacheEnabled": config.DriveCacheEnabled,
2024-11-13 16:59:42 +01:00
"JsDisabled": jsDisabled,
2024-08-13 16:31:28 +02:00
}
if r.URL.Query().Get("ajax") == "true" {
// Render only the images
renderTemplate(w, "images_only.html", data)
return
}
2024-11-13 16:59:42 +01:00
// Render the full page
renderTemplate(w, "images.html", data)
2024-08-13 16:31:28 +02:00
}
2024-11-13 16:59:42 +01:00
func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int, synchronous bool) []ImageSearchResult {
2024-08-13 16:31:28 +02:00
cacheChan := make(chan []SearchResult)
var combinedResults []ImageSearchResult
go func() {
results, exists := resultsCache.Get(cacheKey)
if exists {
2024-11-26 07:46:03 +01:00
printDebug("Cache hit")
2024-08-13 16:31:28 +02:00
cacheChan <- results
} else {
2024-11-26 07:46:03 +01:00
printDebug("Cache miss")
2024-08-13 16:31:28 +02:00
cacheChan <- nil
}
}()
select {
case results := <-cacheChan:
if results == nil {
2024-11-26 07:46:03 +01:00
if config.CrawlerEnabled {
combinedResults = fetchImageResults(query, safe, lang, page, synchronous)
if len(combinedResults) > 0 {
combinedResults = filterValidImages(combinedResults)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
}
} else {
printDebug("Crawler disabled; skipping fetching from image search engines.")
2024-08-13 16:31:28 +02:00
}
} else {
2024-12-05 19:39:45 +01:00
_, _, imageResults, _ := convertToSpecificResults(results)
combinedResults = filterValidImages(imageResults)
2024-08-13 16:31:28 +02:00
}
case <-time.After(2 * time.Second):
2024-11-26 07:46:03 +01:00
printDebug("Cache check timeout")
if config.CrawlerEnabled {
combinedResults = fetchImageResults(query, safe, lang, page, synchronous)
if len(combinedResults) > 0 {
combinedResults = filterValidImages(combinedResults)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
}
} else {
printDebug("Crawler disabled; skipping fetching from image search engines.")
2024-08-13 16:31:28 +02:00
}
}
return combinedResults
}
2024-11-13 16:59:42 +01:00
func fetchImageResults(query, safe, lang string, page int, synchronous bool) []ImageSearchResult {
2024-08-13 16:31:28 +02:00
var results []ImageSearchResult
2024-11-26 07:46:03 +01:00
// Check if CrawlerEnabled is false
if !config.CrawlerEnabled {
printDebug("Crawler is disabled; skipping image search engine fetching.")
return results
}
2024-10-31 19:38:31 +01:00
engineCount := len(imageSearchEngines)
2024-08-13 16:31:28 +02:00
2024-10-31 19:38:31 +01:00
// Determine the engine to use based on the page number
engineIndex := (page - 1) % engineCount
engine := imageSearchEngines[engineIndex]
2024-08-13 16:31:28 +02:00
2024-10-31 19:38:31 +01:00
// Calculate the specific page number for the selected engine
enginePage := (page-1)/engineCount + 1
2024-08-13 16:31:28 +02:00
2024-10-31 19:38:31 +01:00
// Fetch results from the selected engine
searchResults, _, err := engine.Func(query, safe, lang, enginePage)
if err != nil {
printWarn("Error performing image search with %s: %v", engine.Name, err)
} else {
2024-08-13 16:31:28 +02:00
for _, result := range searchResults {
2024-10-13 00:04:46 +02:00
imageResult := result.(ImageSearchResult)
// Skip image if thumbnail URL is empty
if imageResult.Thumb == "" {
printWarn("Skipping image with empty thumbnail URL. Full URL: %s", imageResult.Full)
continue
}
// Generate hash and set up caching
hasher := md5.New()
hasher.Write([]byte(imageResult.Full))
hash := hex.EncodeToString(hasher.Sum(nil))
imageResult.ID = hash
// Store mapping from imageID_full and imageID_thumb to URLs
imageURLMapMu.Lock()
imageURLMap[fmt.Sprintf("%s_full", hash)] = imageResult.Full
imageURLMap[fmt.Sprintf("%s_thumb", hash)] = imageResult.Thumb
imageURLMapMu.Unlock()
// Set ProxyFull and ProxyThumb
2024-11-26 07:46:03 +01:00
if config.DriveCacheEnabled {
// Cache the thumbnail image asynchronously
go func(imgResult ImageSearchResult) {
_, success, err := cacheImage(imgResult.Thumb, imgResult.ID, true)
2024-11-13 16:59:42 +01:00
if err != nil || !success {
printWarn("Failed to cache thumbnail image %s: %v", imgResult.Thumb, err)
removeImageResultFromCache(query, page, safe == "active", lang, imgResult.ID)
2024-10-13 00:04:46 +02:00
}
}(imageResult)
// Set ProxyThumb to the proxy URL (initially placeholder)
imageResult.ProxyThumb = fmt.Sprintf("/image/%s_thumb.webp", hash)
// Set ProxyFull to the proxy URL
imageResult.ProxyFull = fmt.Sprintf("/image/%s_full", hash)
} else {
// Hard cache disabled, proxy both thumb and full images
imageResult.ProxyThumb = fmt.Sprintf("/image/%s_thumb", hash)
imageResult.ProxyFull = fmt.Sprintf("/image/%s_full", hash)
2024-10-13 00:04:46 +02:00
}
2024-10-13 00:04:46 +02:00
results = append(results, imageResult)
2024-08-13 16:31:28 +02:00
}
}
2024-10-31 19:38:31 +01:00
// Fallback mechanism if no results are fetched from the selected engine
2024-08-13 16:31:28 +02:00
if len(results) == 0 {
2024-10-31 19:38:31 +01:00
printWarn("No image results found with engine %s, trying other engines.", engine.Name)
for i := 1; i < engineCount; i++ {
nextEngine := imageSearchEngines[(engineIndex+i)%engineCount]
enginePage = (page-1)/engineCount + 1 // Recalculate page for next engine
printInfo("Trying next image search engine: %s (engine page %d)", nextEngine.Name, enginePage)
searchResults, _, err := nextEngine.Func(query, safe, lang, enginePage)
if err != nil {
printWarn("Error performing image search with %s: %v", nextEngine.Name, err)
continue
}
for _, result := range searchResults {
imageResult := result.(ImageSearchResult)
// Skip image if thumbnail URL is empty
if imageResult.Thumb == "" {
printWarn("Skipping image with empty thumbnail URL. Full URL: %s", imageResult.Full)
continue
}
// Generate hash and set up caching
hasher := md5.New()
hasher.Write([]byte(imageResult.Full))
hash := hex.EncodeToString(hasher.Sum(nil))
imageResult.ID = hash
// Store mapping from imageID_full and imageID_thumb to URLs
imageURLMapMu.Lock()
imageURLMap[fmt.Sprintf("%s_full", hash)] = imageResult.Full
imageURLMap[fmt.Sprintf("%s_thumb", hash)] = imageResult.Thumb
imageURLMapMu.Unlock()
2024-11-26 07:46:03 +01:00
if config.DriveCacheEnabled {
// Cache the thumbnail image asynchronously
go func(imgResult ImageSearchResult) {
_, success, err := cacheImage(imgResult.Thumb, imgResult.ID, true)
if err != nil || !success {
printWarn("Failed to cache thumbnail image %s: %v", imgResult.Thumb, err)
removeImageResultFromCache(query, page, safe == "active", lang, imgResult.ID)
2024-10-31 19:38:31 +01:00
}
}(imageResult)
// Set ProxyThumb to the proxy URL (initially placeholder)
imageResult.ProxyThumb = fmt.Sprintf("/image/%s_thumb.webp", hash)
// Set ProxyFull to the proxy URL
imageResult.ProxyFull = fmt.Sprintf("/image/%s_full", hash)
2024-10-31 19:38:31 +01:00
} else {
// Hard cache disabled, proxy both thumb and full images
imageResult.ProxyThumb = fmt.Sprintf("/image/%s_thumb", hash)
imageResult.ProxyFull = fmt.Sprintf("/image/%s_full", hash)
2024-10-31 19:38:31 +01:00
}
2024-10-31 19:38:31 +01:00
results = append(results, imageResult)
}
if len(results) > 0 {
break
}
}
2024-08-13 16:31:28 +02:00
}
// // Filter out images that failed to cache or are invalid
// validResults := make([]ImageSearchResult, 0, len(results))
// for _, imageResult := range results {
// if imageResult.ProxyFull != "" {
// validResults = append(validResults, imageResult)
// } else {
// printWarn("Skipping invalid image with ID %s", imageResult.ID)
// }
// }
2024-10-31 19:38:31 +01:00
// Final debug print to show the count of results fetched
printInfo("Fetched %d image results for overall page %d", len(results), page)
return results
2024-08-13 16:31:28 +02:00
}
func wrapImageSearchFunc(f func(string, string, string, int) ([]ImageSearchResult, time.Duration, error)) func(string, string, string, int) ([]SearchResult, time.Duration, error) {
return func(query, safe, lang string, page int) ([]SearchResult, time.Duration, error) {
imageResults, duration, err := f(query, safe, lang, page)
if err != nil {
return nil, duration, err
}
searchResults := make([]SearchResult, len(imageResults))
for i, result := range imageResults {
searchResults[i] = result
}
return searchResults, duration, nil
}
}