Return the movie URL from the API

This commit is contained in:
Grégoire Delattre 2016-11-23 22:06:13 +01:00
parent fa48dcac5d
commit 884af0f8b8
3 changed files with 51 additions and 37 deletions

View File

@ -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)
}

View File

@ -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 (
<div className="col-xs-12 col-md-3">
<a className={imgClass}>
<img
src={this.state.src}
onClick={this.props.onClick}
onError={this.handleError}
/>
</a>
</div>
);
}
function MoviePoster(props) {
const selected = props.selected ? ' thumbnail-selected' : '';
const imgClass = 'thumbnail' + selected;
return (
<div className="col-xs-12 col-md-3">
<a className={imgClass}>
<img
src={props.data.poster_url}
onClick={props.onClick}
/>
</a>
</div>
);
}
function MovieDetails(props) {

View File

@ -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