code cleanup & fixed compatibility for non-JS users & fixed fullscreen images being in low resolution
This commit is contained in:
parent
0d083f53e7
commit
db89f9c781
13 changed files with 474 additions and 300 deletions
169
images.go
169
images.go
|
@ -58,6 +58,12 @@ func handleImageSearch(w http.ResponseWriter, r *http.Request, settings UserSett
|
|||
"JsDisabled": jsDisabled,
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("ajax") == "true" {
|
||||
// Render only the images
|
||||
renderTemplate(w, "images_only.html", data)
|
||||
return
|
||||
}
|
||||
|
||||
// Render the full page
|
||||
renderTemplate(w, "images.html", data)
|
||||
}
|
||||
|
@ -104,7 +110,6 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string
|
|||
func fetchImageResults(query, safe, lang string, page int, synchronous bool) []ImageSearchResult {
|
||||
var results []ImageSearchResult
|
||||
engineCount := len(imageSearchEngines)
|
||||
safeBool := safe == "active"
|
||||
|
||||
// Determine the engine to use based on the page number
|
||||
engineIndex := (page - 1) % engineCount
|
||||
|
@ -120,37 +125,47 @@ func fetchImageResults(query, safe, lang string, page int, synchronous bool) []I
|
|||
} else {
|
||||
for _, result := range searchResults {
|
||||
imageResult := result.(ImageSearchResult)
|
||||
if config.HardCacheEnabled {
|
||||
// Generate hash and set up caching
|
||||
hasher := md5.New()
|
||||
hasher.Write([]byte(imageResult.Full))
|
||||
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||
filename := hash + ".webp"
|
||||
imageResult.ID = hash
|
||||
imageResult.ProxyFull = "/image_cache/" + filename
|
||||
|
||||
if synchronous {
|
||||
// Synchronously cache the image
|
||||
_, success, err := cacheImage(imageResult.Full, filename, imageResult.ID)
|
||||
if err != nil || !success {
|
||||
printWarn("Failed to cache image %s: %v", imageResult.Full, err)
|
||||
// Fallback to proxy URL
|
||||
imageResult.ProxyFull = "/imgproxy?url=" + imageResult.Full
|
||||
}
|
||||
} else {
|
||||
// Start caching and validation in the background
|
||||
go func(imgResult ImageSearchResult, originalURL, filename string) {
|
||||
_, success, err := cacheImage(originalURL, filename, imgResult.ID)
|
||||
if err != nil || !success {
|
||||
printWarn("Failed to cache image %s: %v", originalURL, err)
|
||||
removeImageResultFromCache(query, page, safeBool, lang, imgResult.ID)
|
||||
}
|
||||
}(imageResult, imageResult.Full, filename)
|
||||
}
|
||||
} else {
|
||||
// Use proxied URLs when hard cache is disabled
|
||||
imageResult.ProxyFull = "/imgproxy?url=" + imageResult.Full
|
||||
// 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
|
||||
if config.HardCacheEnabled {
|
||||
// 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)
|
||||
}
|
||||
}(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)
|
||||
}
|
||||
|
||||
results = append(results, imageResult)
|
||||
}
|
||||
}
|
||||
|
@ -170,44 +185,46 @@ func fetchImageResults(query, safe, lang string, page int, synchronous bool) []I
|
|||
}
|
||||
for _, result := range searchResults {
|
||||
imageResult := result.(ImageSearchResult)
|
||||
if config.HardCacheEnabled {
|
||||
// Generate hash and set up caching
|
||||
hasher := md5.New()
|
||||
hasher.Write([]byte(imageResult.Full))
|
||||
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||
filename := hash + ".webp"
|
||||
imageResult.ID = hash
|
||||
imageResult.ProxyFull = "/image_cache/" + filename
|
||||
|
||||
if synchronous {
|
||||
// Synchronously cache the image
|
||||
_, success, err := cacheImage(imageResult.Full, filename, imageResult.ID)
|
||||
if err != nil {
|
||||
printWarn("Failed to cache image %s: %v", imageResult.Full, err)
|
||||
// Skip this image
|
||||
continue
|
||||
}
|
||||
if !success {
|
||||
// Skip this image
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// Start caching and validation in the background
|
||||
go func(imgResult ImageSearchResult, originalURL, filename string) {
|
||||
_, success, err := cacheImage(originalURL, filename, imgResult.ID)
|
||||
if err != nil {
|
||||
printWarn("Failed to cache image %s: %v", originalURL, err)
|
||||
}
|
||||
if !success {
|
||||
removeImageResultFromCache(query, page, safeBool, lang, imgResult.ID)
|
||||
}
|
||||
}(imageResult, imageResult.Full, filename)
|
||||
}
|
||||
} else {
|
||||
// Use proxied URLs when hard cache is disabled
|
||||
imageResult.ProxyThumb = "/imgproxy?url=" + imageResult.Thumb
|
||||
imageResult.ProxyFull = "/imgproxy?url=" + imageResult.Full
|
||||
// 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()
|
||||
|
||||
if config.HardCacheEnabled {
|
||||
// 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)
|
||||
}
|
||||
}(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)
|
||||
}
|
||||
|
||||
results = append(results, imageResult)
|
||||
}
|
||||
|
||||
|
@ -217,20 +234,20 @@ func fetchImageResults(query, safe, lang string, page int, synchronous bool) []I
|
|||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
// // 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)
|
||||
// }
|
||||
// }
|
||||
|
||||
// Final debug print to show the count of results fetched
|
||||
printInfo("Fetched %d image results for overall page %d", len(results), page)
|
||||
|
||||
return validResults
|
||||
return results
|
||||
}
|
||||
|
||||
func wrapImageSearchFunc(f func(string, string, string, int) ([]ImageSearchResult, time.Duration, error)) func(string, string, string, int) ([]SearchResult, time.Duration, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue