Get the movie poster with a get details
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Grégoire Delattre 2020-04-11 19:07:11 +02:00
parent 5b68ddb098
commit 2f0497ebc6
4 changed files with 24 additions and 15 deletions

View File

@ -49,7 +49,11 @@ func run() error {
if err != nil { if err != nil {
return err return err
} }
backend := &models.Backend{Database: db} backend := &models.Backend{
Database: db,
PublicDir: cf.PublicDir,
ImgURLPrefix: cf.ImgURLPrefix,
}
// Generate auth params // Generate auth params
authParams := auth.Params{ authParams := auth.Params{

View File

@ -8,6 +8,8 @@ import (
// Backend represents the data backend // Backend represents the data backend
type Backend struct { type Backend struct {
Database *sqlx.DB Database *sqlx.DB
PublicDir string
ImgURLPrefix string
configured bool configured bool
} }

View File

@ -2,6 +2,9 @@ package models
import ( import (
"errors" "errors"
"fmt"
"os"
"path/filepath"
polochon "github.com/odwrtw/polochon/lib" polochon "github.com/odwrtw/polochon/lib"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -28,6 +31,15 @@ func (b *Backend) GetMovieDetails(pMovie *polochon.Movie, log *logrus.Entry) err
return err return err
} }
// Add the movie images
imgURL := fmt.Sprintf("movies/%s.jpg", pMovie.ImdbID)
imgFile := filepath.Join(b.PublicDir, "img", imgURL)
posterURL := ""
if _, err := os.Stat(imgFile); !os.IsNotExist(err) {
posterURL = b.ImgURLPrefix + imgURL
}
pMovie.Thumb = posterURL
log.Debugf("got movie %s from backend", pMovie.ImdbID) log.Debugf("got movie %s from backend", pMovie.ImdbID)
return nil return nil

View File

@ -4,7 +4,6 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"time" "time"
@ -44,7 +43,8 @@ func (m *Movie) MarshalJSON() ([]byte, error) {
Container string `json:"container"` Container string `json:"container"`
}{ }{
Alias: (*Alias)(m), Alias: (*Alias)(m),
PosterURL: m.PosterURL(), // TODO: remove this field to use m.Thumb
PosterURL: m.Thumb,
Subtitles: []subtitles.Subtitle{}, Subtitles: []subtitles.Subtitle{},
} }
@ -244,15 +244,6 @@ func (m *Movie) imgFile() string {
return filepath.Join(m.publicDir, "img", m.imgURL()) return filepath.Join(m.publicDir, "img", m.imgURL())
} }
// PosterURL returns the image URL or the default image if the poster is not yet downloaded
func (m *Movie) PosterURL() string {
// Check if the movie image exists
if _, err := os.Stat(m.imgFile()); os.IsNotExist(err) {
return ""
}
return m.imgURLPrefix + m.imgURL()
}
// getPolochonMovies returns an array of the user's polochon movies // getPolochonMovies returns an array of the user's polochon movies
func getPolochonMovies(user *models.User, env *web.Env) ([]*Movie, error) { func getPolochonMovies(user *models.User, env *web.Env) ([]*Movie, error) {
movies := []*Movie{} movies := []*Movie{}