diff --git a/README.md b/README.md index ffd68d1..aa193ce 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,15 @@ This project is a versatile search engine built with Go that leverages external ```bash git clone https://weforgecode.xyz/Spitfire/Search.git cd search -go run main.go text.go images.go +go run main.go text.go images.go imageproxy.go ``` ## Project Structure - `main.go`: The entry point of the application, setting up the web server and routing. -- `images.go`: Contains logic for handling image search requests, including fetching data from the Qwant API and preparing it for the template. - `text.go`: Handles text search requests, fetching results from Google and processing them for display. +- `images.go`: Contains logic for handling image search requests, including fetching data from the Qwant API and preparing it for the template. +- `imageproxy.go`: Part of images.go srach logic, handles image reuslts and displays them using proxy. - `/templates`: Directory containing HTML templates for rendering the search interface and results. - `search.html`: The main search page template. - `images.html`: Template for displaying image search results. diff --git a/imageproxy.go b/imageproxy.go new file mode 100644 index 0000000..f828116 --- /dev/null +++ b/imageproxy.go @@ -0,0 +1,46 @@ +package main + +import ( + "io" + "log" + "net/http" +) + +func handleImageProxy(w http.ResponseWriter, r *http.Request) { + // Get the URL of the image from the query string + imageURL := r.URL.Query().Get("url") + if imageURL == "" { + http.Error(w, "URL parameter is required", http.StatusBadRequest) + return + } + + // Fetch the image from the external URL + resp, err := http.Get(imageURL) + if err != nil { + log.Printf("Error fetching image: %v", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + // Check if the request was successful + if resp.StatusCode != http.StatusOK { + http.Error(w, "Failed to fetch image", http.StatusBadGateway) + return + } + + // Set the Content-Type header to the type of the fetched image + contentType := resp.Header.Get("Content-Type") + if contentType != "" { + w.Header().Set("Content-Type", contentType) + } else { + // Default to octet-stream if Content-Type is not available + w.Header().Set("Content-Type", "application/octet-stream") + } + + // Write the image content to the response + if _, err := io.Copy(w, resp.Body); err != nil { + log.Printf("Error writing image to response: %v", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + } +} diff --git a/images.go b/images.go index b027e55..a1d2e1a 100644 --- a/images.go +++ b/images.go @@ -69,11 +69,11 @@ func fetchImageResults(query string) ([]ImageSearchResult, error) { var results []ImageSearchResult for _, item := range apiResp.Data.Result.Items { results = append(results, ImageSearchResult{ - Thumbnail: item.Thumbnail, // Thumbnail URL - Title: item.Title, // Image title - Media: item.Media, // Direct link to the image - Ensure this field is used appropriately in your template - Source: item.Media, // Using item.Media here ensures the direct image link is used - ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Thumbnail), // Proxy URL for the thumbnail, if needed + Thumbnail: item.Thumbnail, // Thumbnail URL + Title: item.Title, // Image title + Media: item.Media, // Direct link to the image - Ensure this field is used appropriately in your template + Source: item.Media, // Using item.Media here ensures the direct image link is used + ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Media), // Proxy URL for the thumbnail, if needed Width: item.Width, Height: item.Height, }) diff --git a/main.go b/main.go index 449e86a..731be03 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,7 @@ func main() { http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.HandleFunc("/", handleSearch) http.HandleFunc("/search", handleSearch) + http.HandleFunc("/img_proxy", handleImageProxy) fmt.Println("Server is listening on http://localhost:5000") log.Fatal(http.ListenAndServe(":5000", nil)) } diff --git a/templates/images.html b/templates/images.html index 0972b38..e319ecd 100644 --- a/templates/images.html +++ b/templates/images.html @@ -3,26 +3,64 @@ - Image Search Results + {{.Query}} - Spitfire Search +
+

TailsGo

+
+ +
+

Image Search Results

{{ if .Results }} -
- {{ range .Results }} -
- - {{ .Title }} - -
-
{{ .Title }}
-
Source: Visit
+
+ +
+ + +
+ + + +
+ + +
+ Selected Image
+
+ +
+ + {{ range .Results }} + {{ end }}
+ +
+ +
+ + + + +
+
{{ else }}
No results found for '{{ .Query }}'. Try different keywords.
{{ end }} diff --git a/templates/results.html b/templates/results.html index e6bbb12..05695c5 100644 --- a/templates/results.html +++ b/templates/results.html @@ -3,7 +3,7 @@ - {{.Query}} - Search Results + {{.Query}} - Spitfire Search