diff --git a/src/internal/movies/movies.go b/src/internal/movies/movies.go index f1f60bd..92148ef 100644 --- a/src/internal/movies/movies.go +++ b/src/internal/movies/movies.go @@ -2,6 +2,7 @@ package movies import ( "fmt" + "os" "path/filepath" "github.com/Sirupsen/logrus" @@ -50,7 +51,8 @@ var ( type Movie struct { sqly.BaseModel polochon.Movie - PolochonURL string + PolochonURL string `json:"polochon_url"` + PosterURL string `json:"poster_url"` } func New(imdbID string) *Movie { @@ -62,12 +64,12 @@ func New(imdbID string) *Movie { } // Get returns show details in database from id or imdbid or an error -func (m *Movie) Get(db *sqlx.DB) error { +func (m *Movie) Get(env *web.Env) error { var err error if m.ID != "" { - err = db.QueryRowx(getMovieQueryByID, m.ID).StructScan(m) + err = env.Database.QueryRowx(getMovieQueryByID, m.ID).StructScan(m) } else if m.ImdbID != "" { - err = db.QueryRowx(getMovieQueryByImdbID, m.ImdbID).StructScan(m) + err = env.Database.QueryRowx(getMovieQueryByImdbID, m.ImdbID).StructScan(m) } else { err = fmt.Errorf("Can't get movie details, you have to specify an ID or ImdbID") } @@ -77,6 +79,10 @@ func (m *Movie) Get(db *sqlx.DB) error { } return err } + + // Set the poster url + m.PosterURL = m.GetPosterURL(env) + return nil } @@ -101,7 +107,7 @@ func (m *Movie) GetDetails(env *web.Env, force bool) error { var dbFunc func(db *sqlx.DB) error var err error - err = m.Get(env.Database) + err = m.Get(env) switch err { case nil: log.Debug("movie found in database") @@ -133,14 +139,16 @@ func (m *Movie) GetDetails(env *web.Env, force bool) error { log.Debug("movie added in database") // Download poster - imgPath := filepath.Join(env.Config.PublicDir, "img", "movies", m.ImdbID+".jpg") - err = web.Download(m.Thumb, imgPath) + err = web.Download(m.Thumb, m.imgFile(env)) if err != nil { return err } log.Debug("poster downloaded") + // Set the poster url + m.PosterURL = m.GetPosterURL(env) + return nil } @@ -177,3 +185,23 @@ func (m *Movie) Delete(db *sqlx.DB) error { } return nil } + +// imgURL returns the default image url +func (m *Movie) imgURL(env *web.Env) string { + return fmt.Sprintf("img/movies/%s.jpg", m.ImdbID) +} + +// imgFile returns the image location on disk +func (m *Movie) imgFile(env *web.Env) string { + return filepath.Join(env.Config.PublicDir, m.imgURL(env)) +} + +// GetPosterURL returns the image URL or the default image if the poster is not yet downloaded +func (m *Movie) GetPosterURL(env *web.Env) string { + // Check if the movie image exists + if _, err := os.Stat(m.imgFile(env)); os.IsNotExist(err) { + // TODO image in the config ? + return "img/noimage.png" + } + return m.imgURL(env) +} diff --git a/src/public/js/components/movies/list.js b/src/public/js/components/movies/list.js index 23a86ec..02e06e4 100644 --- a/src/public/js/components/movies/list.js +++ b/src/public/js/components/movies/list.js @@ -32,32 +32,19 @@ function MoviePosters(props) { ); } -class MoviePoster extends React.Component { - constructor(props) { - super(props); - this.state = { - src: `/img/movies/${this.props.data.imdb_id}.jpg`, - } - this.handleError = this.handleError.bind(this); - } - handleError() { - this.setState({ src: '/img/noimage.png' }); - } - render() { - const selected = this.props.selected ? ' thumbnail-selected' : ''; - const imgClass = 'thumbnail' + selected; - return ( -
- - - -
- ); - } +function MoviePoster(props) { + const selected = props.selected ? ' thumbnail-selected' : ''; + const imgClass = 'thumbnail' + selected; + return ( +
+ + + +
+ ); } function MovieDetails(props) { diff --git a/src/public/js/reducers/movies.js b/src/public/js/reducers/movies.js index be83b97..220b9cd 100644 --- a/src/public/js/reducers/movies.js +++ b/src/public/js/reducers/movies.js @@ -33,11 +33,10 @@ export default function movieStore(state = defaultState, action) { return state } - const selectedMovie = Object.assign({}, state.selectedMovie, { - index: action.index, - }) return Object.assign({}, state, { - selectedMovie: selectedMovie, + selectedMovie: Object.assign({}, state.selectedMovie, { + index: action.index, + }), }) default: return state