From a17deb4af1cd320c47c0dcbd7a14aabde14d862c Mon Sep 17 00:00:00 2001 From: partisan Date: Mon, 2 Dec 2024 21:32:30 +0100 Subject: [PATCH] clean up --- README.md | 4 +-- cache-images.go | 24 +++++-------- cache.go | 11 ------ common.go | 1 - imageproxy.go | 52 ----------------------------- static/js/dynamicscrollingimages.js | 22 +++++++++--- 6 files changed, 27 insertions(+), 87 deletions(-) delete mode 100644 imageproxy.go diff --git a/README.md b/README.md index a3fb83d..23e8bf5 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ A self-hosted private 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 ``` diff --git a/cache-images.go b/cache-images.go index 8c4e55e..6813a87 100644 --- a/cache-images.go +++ b/cache-images.go @@ -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) } diff --git a/cache.go b/cache.go index d4b7cb8..7ad54ca 100644 --- a/cache.go +++ b/cache.go @@ -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() diff --git a/common.go b/common.go index c54cb6c..75f6b91 100755 --- a/common.go +++ b/common.go @@ -29,7 +29,6 @@ var ( return string(jsonBytes), nil }, } - searchEngines []SearchEngine ) type SearchEngine struct { diff --git a/imageproxy.go b/imageproxy.go deleted file mode 100644 index 8451ed4..0000000 --- a/imageproxy.go +++ /dev/null @@ -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) -} diff --git a/static/js/dynamicscrollingimages.js b/static/js/dynamicscrollingimages.js index 5213f7f..223017f 100644 --- a/static/js/dynamicscrollingimages.js +++ b/static/js/dynamicscrollingimages.js @@ -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; });