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 {
return err
}
backend := &models.Backend{Database: db}
backend := &models.Backend{
Database: db,
PublicDir: cf.PublicDir,
ImgURLPrefix: cf.ImgURLPrefix,
}
// Generate auth params
authParams := auth.Params{

View File

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

View File

@ -2,6 +2,9 @@ package models
import (
"errors"
"fmt"
"os"
"path/filepath"
polochon "github.com/odwrtw/polochon/lib"
"github.com/sirupsen/logrus"
@ -28,6 +31,15 @@ func (b *Backend) GetMovieDetails(pMovie *polochon.Movie, log *logrus.Entry) 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)
return nil

View File

@ -4,7 +4,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
@ -44,7 +43,8 @@ func (m *Movie) MarshalJSON() ([]byte, error) {
Container string `json:"container"`
}{
Alias: (*Alias)(m),
PosterURL: m.PosterURL(),
// TODO: remove this field to use m.Thumb
PosterURL: m.Thumb,
Subtitles: []subtitles.Subtitle{},
}
@ -244,15 +244,6 @@ func (m *Movie) imgFile() string {
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
func getPolochonMovies(user *models.User, env *web.Env) ([]*Movie, error) {
movies := []*Movie{}