mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-27 17:38:51 +05:00
feat(unified): return poster/backdrop URLs via our /api/v1/images proxy for TMDB and KP
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -10,20 +11,26 @@ import (
|
|||||||
|
|
||||||
const tmdbImageBase = "https://image.tmdb.org/t/p"
|
const tmdbImageBase = "https://image.tmdb.org/t/p"
|
||||||
|
|
||||||
func BuildTMDBImageURL(path string, size string) string {
|
// BuildAPIImageProxyURL строит относительный URL до нашего прокси-эндпоинта изображений.
|
||||||
if path == "" {
|
// Если передан абсолютный URL (KP и пр.) — он кодируется и передаётся как path параметр.
|
||||||
|
// Если передан относительный TMDB-путь, он используется как есть (без ведущего '/').
|
||||||
|
func BuildAPIImageProxyURL(pathOrURL string, size string) string {
|
||||||
|
if strings.TrimSpace(pathOrURL) == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
if size == "" {
|
if size == "" {
|
||||||
size = "w500"
|
size = "w500"
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(path, "/") {
|
// Абсолютные ссылки (Kinopoisk и пр.) — кодируем целиком
|
||||||
path = "/" + path
|
if strings.HasPrefix(pathOrURL, "http://") || strings.HasPrefix(pathOrURL, "https://") {
|
||||||
|
return fmt.Sprintf("/api/v1/images/%s/%s", size, url.QueryEscape(pathOrURL))
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s/%s%s", tmdbImageBase, size, path)
|
// TMDB относительный путь
|
||||||
|
clean := pathOrURL
|
||||||
|
if strings.HasPrefix(clean, "/") {
|
||||||
|
clean = clean[1:]
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("/api/v1/images/%s/%s", size, clean)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MapTMDBToUnifiedMovie(movie *models.Movie, external *models.ExternalIDs) *models.UnifiedContent {
|
func MapTMDBToUnifiedMovie(movie *models.Movie, external *models.ExternalIDs) *models.UnifiedContent {
|
||||||
@@ -74,8 +81,8 @@ func MapTMDBToUnifiedMovie(movie *models.Movie, external *models.ExternalIDs) *m
|
|||||||
Type: "movie",
|
Type: "movie",
|
||||||
Genres: genres,
|
Genres: genres,
|
||||||
Rating: movie.VoteAverage,
|
Rating: movie.VoteAverage,
|
||||||
PosterURL: BuildTMDBImageURL(movie.PosterPath, "w500"),
|
PosterURL: BuildAPIImageProxyURL(movie.PosterPath, "w500"),
|
||||||
BackdropURL: BuildTMDBImageURL(movie.BackdropPath, "w1280"),
|
BackdropURL: BuildAPIImageProxyURL(movie.BackdropPath, "w1280"),
|
||||||
Director: "",
|
Director: "",
|
||||||
Cast: []models.UnifiedCastMember{},
|
Cast: []models.UnifiedCastMember{},
|
||||||
Duration: movie.Runtime,
|
Duration: movie.Runtime,
|
||||||
@@ -136,8 +143,8 @@ func MapTMDBTVToUnified(tv *models.TVShow, external *models.ExternalIDs) *models
|
|||||||
Type: "tv",
|
Type: "tv",
|
||||||
Genres: genres,
|
Genres: genres,
|
||||||
Rating: tv.VoteAverage,
|
Rating: tv.VoteAverage,
|
||||||
PosterURL: BuildTMDBImageURL(tv.PosterPath, "w500"),
|
PosterURL: BuildAPIImageProxyURL(tv.PosterPath, "w500"),
|
||||||
BackdropURL: BuildTMDBImageURL(tv.BackdropPath, "w1280"),
|
BackdropURL: BuildAPIImageProxyURL(tv.BackdropPath, "w1280"),
|
||||||
Director: "",
|
Director: "",
|
||||||
Cast: []models.UnifiedCastMember{},
|
Cast: []models.UnifiedCastMember{},
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
@@ -160,7 +167,7 @@ func MapTMDBTVToUnified(tv *models.TVShow, external *models.ExternalIDs) *models
|
|||||||
SeasonNumber: s.SeasonNumber,
|
SeasonNumber: s.SeasonNumber,
|
||||||
EpisodeCount: s.EpisodeCount,
|
EpisodeCount: s.EpisodeCount,
|
||||||
ReleaseDate: s.AirDate,
|
ReleaseDate: s.AirDate,
|
||||||
PosterURL: BuildTMDBImageURL(s.PosterPath, "w500"),
|
PosterURL: BuildAPIImageProxyURL(s.PosterPath, "w500"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +192,7 @@ func MapTMDBMultiToUnifiedItems(m *models.MultiSearchResponse) []models.UnifiedS
|
|||||||
if r.MediaType == "tv" {
|
if r.MediaType == "tv" {
|
||||||
release = r.FirstAirDate
|
release = r.FirstAirDate
|
||||||
}
|
}
|
||||||
poster := BuildTMDBImageURL(r.PosterPath, "w500")
|
poster := BuildAPIImageProxyURL(r.PosterPath, "w500")
|
||||||
tmdbId := r.ID
|
tmdbId := r.ID
|
||||||
items = append(items, models.UnifiedSearchItem{
|
items = append(items, models.UnifiedSearchItem{
|
||||||
ID: strconv.Itoa(tmdbId),
|
ID: strconv.Itoa(tmdbId),
|
||||||
@@ -216,6 +223,7 @@ func MapKPSearchToUnifiedItems(kps *KPSearchResponse) []models.UnifiedSearchItem
|
|||||||
if poster == "" {
|
if poster == "" {
|
||||||
poster = f.PosterUrl
|
poster = f.PosterUrl
|
||||||
}
|
}
|
||||||
|
poster = BuildAPIImageProxyURL(poster, "w500")
|
||||||
rating := 0.0
|
rating := 0.0
|
||||||
if strings.TrimSpace(f.Rating) != "" {
|
if strings.TrimSpace(f.Rating) != "" {
|
||||||
if v, err := strconv.ParseFloat(f.Rating, 64); err == nil {
|
if v, err := strconv.ParseFloat(f.Rating, 64); err == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user