From 54bbbf84f4094fe3ef70635593d7b93c571dae01 Mon Sep 17 00:00:00 2001 From: Lucas BEE Date: Tue, 7 Apr 2020 17:46:42 +0200 Subject: [PATCH] Change the shows image path From img/shows/ttXXXXX-1-1.png and img/shows/ttXXXXX-banner.png To img/shows/ttXXXXX/1-1.png and img/shows/ttXXXXX/banner.png Rewrite things while we're here Example of scripts to migrate from old path to new path: dir="shows" for file in "$dir"/*; do # Replace the first - by a / in order to get the new image path newfile=$(echo "$file" | sed 's/-/\//') # Delete everything after the first - in order to get the show path showpath=$(echo "$file" | sed 's/-.*//') [ ! -d "$showpath" ] && mkdir "$showpath" mv "$file" "$newfile" done --- backend/shows/shows.go | 56 +++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/backend/shows/shows.go b/backend/shows/shows.go index 4101416..2398b18 100644 --- a/backend/shows/shows.go +++ b/backend/shows/shows.go @@ -160,18 +160,41 @@ func (s *Show) GetImageURL(imgType string) string { // downloadImages will download the show images func (s *Show) downloadImages(env *web.Env) { - // Download the banner - err := web.Download(s.Show.Banner, s.imgFile("banner"), false) - if err != nil { - env.Log.Errorf("failed to dowload banner: %s", err) + // Create the directory for the show if it doesn't exist + if _, err := os.Stat(s.imgDirectory()); err != nil { + if err = os.Mkdir(s.imgDirectory(), os.ModePerm); err != nil { + env.Log.Warnf("couldn't create show directory %s: %s", s.imgDirectory(), err) + return + } } - err = web.Download(s.Show.Fanart, s.imgFile("fanart"), false) - if err != nil { - env.Log.Errorf("failed to dowload fanart: %s", err) - } - err = web.Download(s.Show.Poster, s.imgFile("poster"), true) - if err != nil { - env.Log.Errorf("failed to dowload poster: %s", err) + // Download the show images + for _, img := range []struct { + url string + urlType string + scale bool + }{ + { + url: s.Show.Banner, + urlType: "banner", + scale: false, + }, + { + url: s.Show.Fanart, + urlType: "fanart", + scale: false, + }, + { + url: s.Show.Poster, + urlType: "poster", + scale: true, + }, + } { + if img.url == "" { + continue + } + if err := web.Download(img.url, s.imgFile(img.urlType), img.scale); err != nil { + env.Log.Errorf("failed to dowload %s: %s", img.urlType, err) + } } // Download episode thumbs @@ -180,16 +203,21 @@ func (s *Show) downloadImages(env *web.Env) { continue } - err = web.Download(e.Thumb, s.imgFile(fmt.Sprintf("%d-%d", e.Season, e.Episode)), false) + err := web.Download(e.Thumb, s.imgFile(fmt.Sprintf("%d-%d", e.Season, e.Episode)), false) if err != nil { - env.Log.Errorf("failed to dowload the thumb for season %d episode %d: %s", e.Season, e.Episode, err) + env.Log.Errorf("failed to dowload the thumb for season %d episode %d ( %s ) : %s", e.Season, e.Episode, e.Thumb, err) } } } // imgURL returns the default image url func (s *Show) imgURL(imgType string) string { - return fmt.Sprintf("shows/%s-%s.jpg", s.ImdbID, imgType) + return fmt.Sprintf("shows/%s/%s.jpg", s.ImdbID, imgType) +} + +// imgDirectory returns the directory containing all the show images +func (s *Show) imgDirectory() string { + return filepath.Join(s.publicDir, "img", fmt.Sprintf("shows/%s", s.ImdbID)) } // imgFile returns the image location on disk -- 2.47.1