clean up
This commit is contained in:
parent
96b92fed5e
commit
a17deb4af1
6 changed files with 27 additions and 87 deletions
|
@ -76,7 +76,7 @@ A self-hosted private <a href="https://en.wikipedia.org/wiki/Metasearch_engine">
|
||||||
Linux:
|
Linux:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://weforgecode.xyz/Spitfire/Search.git
|
git clone https://weforge.xyz/Spitfire/Search.git
|
||||||
cd Search
|
cd Search
|
||||||
chmod +x ./run.sh
|
chmod +x ./run.sh
|
||||||
./run.sh
|
./run.sh
|
||||||
|
@ -85,7 +85,7 @@ chmod +x ./run.sh
|
||||||
Windows:
|
Windows:
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
git clone https://weforgecode.xyz/Spitfire/Search.git
|
git clone https://weforge.xyz/Spitfire/Search.git
|
||||||
cd Search
|
cd Search
|
||||||
.\run.bat
|
.\run.bat
|
||||||
```
|
```
|
||||||
|
|
|
@ -298,7 +298,6 @@ func handleImageServe(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, err := io.Copy(w, resp.Body); err != nil {
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
||||||
printWarn("Error writing image to response: %v", err)
|
printWarn("Error writing image to response: %v", err)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleImageStatus(w http.ResponseWriter, r *http.Request) {
|
func handleImageStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -505,19 +504,12 @@ func cleanupCache() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContentType(ext string) string {
|
// Serve missing.svg
|
||||||
switch strings.ToLower(ext) {
|
func serveMissingImage(w http.ResponseWriter, r *http.Request) {
|
||||||
case "svg":
|
missingImagePath := filepath.Join("static", "images", "missing.svg")
|
||||||
return "image/svg+xml"
|
w.Header().Set("Content-Type", "image/svg+xml")
|
||||||
case "jpg", "jpeg":
|
w.Header().Set("Cache-Control", "no-store, must-revalidate")
|
||||||
return "image/jpeg"
|
w.Header().Set("Pragma", "no-cache")
|
||||||
case "png":
|
w.Header().Set("Expires", "0")
|
||||||
return "image/png"
|
http.ServeFile(w, r, missingImagePath)
|
||||||
case "gif":
|
|
||||||
return "image/gif"
|
|
||||||
case "webp":
|
|
||||||
return "image/webp"
|
|
||||||
default:
|
|
||||||
return "application/octet-stream"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
11
cache.go
11
cache.go
|
@ -151,17 +151,6 @@ func (rc *ResultsCache) currentMemoryUsage() uint64 {
|
||||||
return v.Used // Used memory in bytes
|
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() {
|
func (rc *ResultsCache) cleanOldestItems() {
|
||||||
rc.mu.Lock()
|
rc.mu.Lock()
|
||||||
defer rc.mu.Unlock()
|
defer rc.mu.Unlock()
|
||||||
|
|
|
@ -29,7 +29,6 @@ var (
|
||||||
return string(jsonBytes), nil
|
return string(jsonBytes), nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
searchEngines []SearchEngine
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SearchEngine struct {
|
type SearchEngine struct {
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Configuration
|
// Configuration
|
||||||
const imageStatusInterval = 500; // Interval in milliseconds to check image status
|
const imageStatusInterval = 500; // Interval in milliseconds to check image status
|
||||||
const scrollThreshold = 500; // Distance from bottom of the page to trigger loading
|
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 isFetching = false;
|
||||||
let page = parseInt(document.getElementById('template-data').getAttribute('data-page')) || 1;
|
let page = parseInt(document.getElementById('template-data').getAttribute('data-page')) || 1;
|
||||||
let query = document.getElementById('template-data').getAttribute('data-query');
|
let query = document.getElementById('template-data').getAttribute('data-query');
|
||||||
|
@ -51,17 +53,25 @@
|
||||||
*/
|
*/
|
||||||
function fetchNextPage() {
|
function fetchNextPage() {
|
||||||
if (isFetching || noMoreImages) return;
|
if (isFetching || noMoreImages) return;
|
||||||
|
|
||||||
|
// Start the timer for loading indicator
|
||||||
|
loadingTimer = setTimeout(() => {
|
||||||
|
loadingIndicator.style.display = 'flex';
|
||||||
|
}, 150);
|
||||||
|
|
||||||
isFetching = true;
|
isFetching = true;
|
||||||
page += 1;
|
page += 1;
|
||||||
|
|
||||||
fetch(`/search?q=${encodeURIComponent(query)}&t=image&p=${page}&ajax=true`)
|
fetch(`/search?q=${encodeURIComponent(query)}&t=image&p=${page}&ajax=true`)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(html => {
|
.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 parser = new DOMParser();
|
||||||
let doc = parser.parseFromString(html, 'text/html');
|
let doc = parser.parseFromString(html, 'text/html');
|
||||||
let newImages = doc.querySelectorAll('.image');
|
let newImages = doc.querySelectorAll('.image');
|
||||||
|
|
||||||
if (newImages.length > 0) {
|
if (newImages.length > 0) {
|
||||||
let resultsContainer = document.querySelector('.images');
|
let resultsContainer = document.querySelector('.images');
|
||||||
newImages.forEach(imageDiv => {
|
newImages.forEach(imageDiv => {
|
||||||
|
@ -77,9 +87,9 @@
|
||||||
img.onerror = function() {
|
img.onerror = function() {
|
||||||
handleImageError(img);
|
handleImageError(img);
|
||||||
};
|
};
|
||||||
|
|
||||||
let id = img.getAttribute('data-id');
|
let id = img.getAttribute('data-id');
|
||||||
if (id) { // Only include if ID is not empty
|
if (id) {
|
||||||
imageElements.push(img);
|
imageElements.push(img);
|
||||||
imageIds.push(id);
|
imageIds.push(id);
|
||||||
}
|
}
|
||||||
|
@ -98,6 +108,8 @@
|
||||||
isFetching = false;
|
isFetching = false;
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.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);
|
console.error('Error fetching next page:', error);
|
||||||
isFetching = false;
|
isFetching = false;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue