From 76a9183b528e372952a09caf74e21dd865c642c8 Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Wed, 11 Dec 2024 12:24:16 +0800 Subject: [PATCH] refactor: reduce default poster image size to w500 --- cmd/main.go | 14 +++++------ server/core/client.go | 1 + server/core/importlist.go | 53 +++++++++++++++++++++++++++++++++++++++ ui/lib/welcome_page.dart | 2 +- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index f4ba8081..2242ad54 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,9 +3,7 @@ package main import ( "polaris/db" "polaris/log" - "polaris/pkg/utils" "polaris/server" - "time" ) func main() { @@ -18,13 +16,13 @@ func main() { log.Panicf("init db error: %v", err) } - go func() { - time.Sleep(2 * time.Second) - if err := utils.OpenURL("http://127.0.0.1:8080"); err != nil { - log.Errorf("open url error: %v", err) - } + // go func() { + // time.Sleep(2 * time.Second) + // if err := utils.OpenURL("http://127.0.0.1:8080"); err != nil { + // log.Errorf("open url error: %v", err) + // } - }() + // }() s := server.NewServer(dbClient) if err := s.Serve(); err != nil { log.Errorf("server start error: %v", err) diff --git a/server/core/client.go b/server/core/client.go index 52f64e44..440eb72d 100644 --- a/server/core/client.go +++ b/server/core/client.go @@ -46,6 +46,7 @@ func (c *Client) registerCronJob(name string, cron string, f func() error) { func (c *Client) Init() { go c.reloadTasks() c.addSysCron() + go c.checkW500PosterOnStartup() } func (c *Client) reloadTasks() { diff --git a/server/core/importlist.go b/server/core/importlist.go index 1adcad25..eec7084b 100644 --- a/server/core/importlist.go +++ b/server/core/importlist.go @@ -223,6 +223,10 @@ func (c *Client) AddTv2Watchlist(in AddWatchlistIn) (interface{}, error) { if err := c.downloadPoster(detail.PosterPath, r.ID); err != nil { log.Errorf("download poster error: %v", err) } + if err := c.downloadW500Poster(detail.PosterPath, r.ID); err != nil { + log.Errorf("download w500 poster error: %v", err) + } + if err := c.downloadBackdrop(detail.BackdropPath, r.ID); err != nil { log.Errorf("download poster error: %v", err) } @@ -301,6 +305,10 @@ func (c *Client) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) { if err := c.downloadPoster(detail.PosterPath, r.ID); err != nil { log.Errorf("download poster error: %v", err) } + if err := c.downloadW500Poster(detail.PosterPath, r.ID); err != nil { + log.Errorf("download w500 poster error: %v", err) + } + if err := c.downloadBackdrop(detail.BackdropPath, r.ID); err != nil { log.Errorf("download backdrop error: %v", err) } @@ -372,6 +380,11 @@ func (c *Client) downloadPoster(path string, mediaID int) error { return c.downloadImage(url, mediaID, "poster.jpg") } +func (c *Client) downloadW500Poster(path string, mediaID int) error { + url := "https://image.tmdb.org/t/p/w500" + path + return c.downloadImage(url, mediaID, "poster_w500.jpg") +} + func (c *Client) downloadImage(url string, mediaID int, name string) error { log.Infof("try to download image: %v", url) @@ -397,6 +410,46 @@ func (c *Client) downloadImage(url string, mediaID int, name string) error { } +func (c *Client) checkW500PosterOnStartup() { + log.Infof("check all w500 posters") + all := c.db.GetMediaWatchlist(media.MediaTypeTv) + movies := c.db.GetMediaWatchlist(media.MediaTypeMovie) + all = append(all, movies...) + for _, e := range all { + targetFile := filepath.Join(fmt.Sprintf("%v/%d", db.ImgPath, e.ID), "poster_w500.jpg") + if _, err := os.Stat(targetFile); err != nil { + log.Infof("poster_w500.jpg not exist for %s, will download it", e.NameEn) + + if e.MediaType ==media.MediaTypeTv { + detail, err := c.MustTMDB().GetTvDetails(e.TmdbID, db.LanguageCN) + if err != nil { + log.Warnf("get tmdb detail for %s error: %v", e.NameEn, err) + continue + } + + if err := c.downloadW500Poster(detail.PosterPath, e.ID); err != nil { + log.Warnf("download w500 poster error: %v", err) + continue + } + + } else { + detail, err := c.MustTMDB().GetMovieDetails(e.TmdbID, db.LanguageCN) + if err != nil { + log.Warnf("get tmdb detail for %s error: %v", e.NameEn, err) + continue + } + + if err := c.downloadW500Poster(detail.PosterPath, e.ID); err != nil { + log.Warnf("download w500 poster error: %v", err) + continue + } + + } + + } + } +} + func (c *Client) SuggestedMovieFolderName(tmdbId int) (string, error) { d1, err := c.MustTMDB().GetMovieDetails(tmdbId, c.language) diff --git a/ui/lib/welcome_page.dart b/ui/lib/welcome_page.dart index 11aa1b72..e38fd655 100644 --- a/ui/lib/welcome_page.dart +++ b/ui/lib/welcome_page.dart @@ -273,7 +273,7 @@ class MediaCard extends StatelessWidget { child: Ink.image( fit: BoxFit.cover, image: NetworkImage( - "${APIs.imagesUrl}/${item.id}/poster.jpg", + "${APIs.imagesUrl}/${item.id}/poster_w500.jpg", )), ), SizedBox(