mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-27 17:38:51 +05:00
155 lines
4.0 KiB
Go
155 lines
4.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"neomovies-api/pkg/config"
|
|
)
|
|
|
|
type ImagesHandler struct{}
|
|
|
|
func NewImagesHandler() *ImagesHandler { return &ImagesHandler{} }
|
|
|
|
func (h *ImagesHandler) GetImage(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
size := vars["size"]
|
|
imagePath := vars["path"]
|
|
|
|
if size == "" || imagePath == "" {
|
|
http.Error(w, "Size and path are required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if imagePath == "placeholder.jpg" {
|
|
h.servePlaceholder(w, r)
|
|
return
|
|
}
|
|
|
|
validSizes := []string{"w92", "w154", "w185", "w342", "w500", "w780", "original"}
|
|
if !h.isValidSize(size, validSizes) {
|
|
size = "original"
|
|
}
|
|
|
|
var imageURL string
|
|
if strings.HasPrefix(imagePath, "http://") || strings.HasPrefix(imagePath, "https://") {
|
|
// Проксируем внешний абсолютный URL (например, Kinopoisk)
|
|
// Поддержим случай, когда на фронте параметр уже был url-encoded
|
|
decoded, _ := url.QueryUnescape(imagePath)
|
|
imageURL = decoded
|
|
} else {
|
|
// TMDB относительный путь
|
|
imageURL = fmt.Sprintf("%s/%s/%s", config.TMDBImageBaseURL, size, imagePath)
|
|
}
|
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
req, err := http.NewRequest("GET", imageURL, nil)
|
|
if err != nil {
|
|
h.servePlaceholder(w, r)
|
|
return
|
|
}
|
|
// Проксируем важные заголовки, чтобы источники не резали по UA
|
|
if ua := r.Header.Get("User-Agent"); ua != "" { req.Header.Set("User-Agent", ua) }
|
|
if ref := r.Header.Get("Referer"); ref != "" { req.Header.Set("Referer", ref) }
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
h.servePlaceholder(w, r)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
h.servePlaceholder(w, r)
|
|
return
|
|
}
|
|
|
|
if contentType := resp.Header.Get("Content-Type"); contentType != "" {
|
|
w.Header().Set("Content-Type", contentType)
|
|
}
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
|
if err != nil {
|
|
h.servePlaceholder(w, r)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *ImagesHandler) servePlaceholder(w http.ResponseWriter, r *http.Request) {
|
|
placeholderPaths := []string{
|
|
"./assets/placeholder.jpg",
|
|
"./public/images/placeholder.jpg",
|
|
"./static/placeholder.jpg",
|
|
}
|
|
|
|
var placeholderPath string
|
|
for _, path := range placeholderPaths {
|
|
if _, err := os.Stat(path); err == nil {
|
|
placeholderPath = path
|
|
break
|
|
}
|
|
}
|
|
|
|
if placeholderPath == "" {
|
|
h.serveSVGPlaceholder(w, r)
|
|
return
|
|
}
|
|
|
|
file, err := os.Open(placeholderPath)
|
|
if err != nil {
|
|
h.serveSVGPlaceholder(w, r)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
ext := strings.ToLower(filepath.Ext(placeholderPath))
|
|
switch ext {
|
|
case ".jpg", ".jpeg":
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
case ".png":
|
|
w.Header().Set("Content-Type", "image/png")
|
|
case ".gif":
|
|
w.Header().Set("Content-Type", "image/gif")
|
|
case ".webp":
|
|
w.Header().Set("Content-Type", "image/webp")
|
|
default:
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
_, err = io.Copy(w, file)
|
|
if err != nil {
|
|
h.serveSVGPlaceholder(w, r)
|
|
}
|
|
}
|
|
|
|
func (h *ImagesHandler) serveSVGPlaceholder(w http.ResponseWriter, r *http.Request) {
|
|
svgPlaceholder := `<svg width="300" height="450" xmlns="http://www.w3.org/2000/svg">
|
|
<rect width="100%" height="100%" fill="#f0f0f0"/>
|
|
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-family="Arial, sans-serif" font-size="16" fill="#666">
|
|
Изображение не найдено
|
|
</text>
|
|
</svg>`
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
w.Write([]byte(svgPlaceholder))
|
|
}
|
|
|
|
func (h *ImagesHandler) isValidSize(size string, validSizes []string) bool {
|
|
for _, validSize := range validSizes {
|
|
if size == validSize {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|