This commit is contained in:
partisan 2024-12-02 21:32:30 +01:00
parent 96b92fed5e
commit a17deb4af1
6 changed files with 27 additions and 87 deletions

View file

@ -76,7 +76,7 @@ A self-hosted private <a href="https://en.wikipedia.org/wiki/Metasearch_engine">
Linux:
```bash
git clone https://weforgecode.xyz/Spitfire/Search.git
git clone https://weforge.xyz/Spitfire/Search.git
cd Search
chmod +x ./run.sh
./run.sh
@ -85,7 +85,7 @@ chmod +x ./run.sh
Windows:
```powershell
git clone https://weforgecode.xyz/Spitfire/Search.git
git clone https://weforge.xyz/Spitfire/Search.git
cd Search
.\run.bat
```

View file

@ -298,7 +298,6 @@ func handleImageServe(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(w, resp.Body); err != nil {
printWarn("Error writing image to response: %v", err)
}
return
}
func handleImageStatus(w http.ResponseWriter, r *http.Request) {
@ -505,19 +504,12 @@ func cleanupCache() {
}
}
func getContentType(ext string) string {
switch strings.ToLower(ext) {
case "svg":
return "image/svg+xml"
case "jpg", "jpeg":
return "image/jpeg"
case "png":
return "image/png"
case "gif":
return "image/gif"
case "webp":
return "image/webp"
default:
return "application/octet-stream"
}
// Serve missing.svg
func serveMissingImage(w http.ResponseWriter, r *http.Request) {
missingImagePath := filepath.Join("static", "images", "missing.svg")
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
http.ServeFile(w, r, missingImagePath)
}

View file

@ -151,17 +151,6 @@ func (rc *ResultsCache) currentMemoryUsage() uint64 {
return v.Used // Used memory in bytes
}
// memoryUsage calculates the current memory usage as a percentage.
func (rc *ResultsCache) memoryUsage() float64 {
v, err := mem.VirtualMemory()
if err != nil {
printErr("Failed to get memory info: %v", err)
return 0
}
return v.UsedPercent
}
func (rc *ResultsCache) cleanOldestItems() {
rc.mu.Lock()
defer rc.mu.Unlock()

View file

@ -29,7 +29,6 @@ var (
return string(jsonBytes), nil
},
}
searchEngines []SearchEngine
)
type SearchEngine struct {

View file

@ -1,52 +0,0 @@
package main
import (
"io"
"net/http"
"path/filepath"
"strings"
)
func serveImageProxy(w http.ResponseWriter, imageURL string) {
// Fetch the image from the external URL
resp, err := http.Get(imageURL)
if err != nil {
printWarn("Error fetching image: %v", err)
serveMissingImage(w, nil)
return
}
defer resp.Body.Close()
// Check if the request was successful
if resp.StatusCode != http.StatusOK {
serveMissingImage(w, nil)
return
}
// Set the Content-Type header to the type of the fetched image
contentType := resp.Header.Get("Content-Type")
if contentType != "" && strings.HasPrefix(contentType, "image/") {
w.Header().Set("Content-Type", contentType)
} else {
serveMissingImage(w, nil)
return
}
// Write the image content to the response
if _, err := io.Copy(w, resp.Body); err != nil {
printWarn("Error writing image to response: %v", err)
// Serve missing.svg
// Note: At this point, headers are already sent, so serving missing.svg won't work.
// It's better to just log the error here.
}
}
// Serve missing.svg
func serveMissingImage(w http.ResponseWriter, r *http.Request) {
missingImagePath := filepath.Join("static", "images", "missing.svg")
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
http.ServeFile(w, r, missingImagePath)
}

View file

@ -2,6 +2,8 @@
// Configuration
const imageStatusInterval = 500; // Interval in milliseconds to check image status
const scrollThreshold = 500; // Distance from bottom of the page to trigger loading
const loadingIndicator = document.getElementById('message-bottom-left');
let loadingTimer;
let isFetching = false;
let page = parseInt(document.getElementById('template-data').getAttribute('data-page')) || 1;
let query = document.getElementById('template-data').getAttribute('data-query');
@ -51,17 +53,25 @@
*/
function fetchNextPage() {
if (isFetching || noMoreImages) return;
// Start the timer for loading indicator
loadingTimer = setTimeout(() => {
loadingIndicator.style.display = 'flex';
}, 150);
isFetching = true;
page += 1;
fetch(`/search?q=${encodeURIComponent(query)}&t=image&p=${page}&ajax=true`)
.then(response => response.text())
.then(html => {
// Parse the returned HTML and extract image elements
clearTimeout(loadingTimer); // Clear the timer if fetch is successful
loadingIndicator.style.display = 'none'; // Hide the loading indicator
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
let newImages = doc.querySelectorAll('.image');
if (newImages.length > 0) {
let resultsContainer = document.querySelector('.images');
newImages.forEach(imageDiv => {
@ -77,9 +87,9 @@
img.onerror = function() {
handleImageError(img);
};
let id = img.getAttribute('data-id');
if (id) { // Only include if ID is not empty
if (id) {
imageElements.push(img);
imageIds.push(id);
}
@ -98,6 +108,8 @@
isFetching = false;
})
.catch(error => {
clearTimeout(loadingTimer); // Clear the timer if fetch fails
loadingIndicator.style.display = 'none'; // Hide the loading indicator
console.error('Error fetching next page:', error);
isFetching = false;
});