diff --git a/internal/books/api_googlebooks.go b/internal/books/api_googlebooks.go index ed501ad..8ce75ed 100644 --- a/internal/books/api_googlebooks.go +++ b/internal/books/api_googlebooks.go @@ -184,5 +184,12 @@ func DownloadImage(url, slug string) error { } fmt.Printf(" - Image saved successfully at: %s\n", filePath) + + // Generate thumbnails for the specified resolutions + resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails + if err := utils.GenerateThumbnails(filePath, slug, imageDir, resolutions); err != nil { + return fmt.Errorf("error generating thumbnails: %w", err) + } + return nil } diff --git a/internal/games/api_igdb.go b/internal/games/api_igdb.go index b73d970..8d47bb1 100644 --- a/internal/games/api_igdb.go +++ b/internal/games/api_igdb.go @@ -231,5 +231,13 @@ func DownloadImage(url, slug, suffix string) error { } fmt.Printf(" - Image saved successfully at: %s\n", filePath) + + // Generate thumbnails for the specified resolutions + thumbnail_slug := fmt.Sprintf("%s%s", slug, suffix) + resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails + if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil { + return fmt.Errorf("error generating thumbnails: %w", err) + } + return nil } diff --git a/internal/movies/api_fanart.go b/internal/movies/api_fanart.go index 3f056a5..e4f57b5 100644 --- a/internal/movies/api_fanart.go +++ b/internal/movies/api_fanart.go @@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error { } fmt.Printf(" - Image saved successfully at: %s\n", filePath) + + // Generate thumbnails for the specified resolutions + thumbnail_slug := fmt.Sprintf("%s-%s", slug, imageType) + resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails + if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil { + return fmt.Errorf("error generating thumbnails: %w", err) + } + return nil } diff --git a/internal/music/api_spotify.go b/internal/music/api_spotify.go index ad4d8c6..cd74a98 100644 --- a/internal/music/api_spotify.go +++ b/internal/music/api_spotify.go @@ -151,5 +151,12 @@ func DownloadImage(url, slug string) error { } fmt.Printf(" - Image saved successfully at: %s\n", filePath) + + // Generate thumbnails for the specified resolutions + resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails + if err := utils.GenerateThumbnails(filePath, slug, imageDir, resolutions); err != nil { + return fmt.Errorf("error generating thumbnails: %w", err) + } + return nil } diff --git a/internal/series/api_fanart.go b/internal/series/api_fanart.go index cc565d5..e52635d 100644 --- a/internal/series/api_fanart.go +++ b/internal/series/api_fanart.go @@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error { } fmt.Printf(" - Image saved successfully at: %s\n", filePath) + + // Generate thumbnails for the specified resolutions + thumbnail_slug := fmt.Sprintf("%s-%s", slug, imageType) + resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails + if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil { + return fmt.Errorf("error generating thumbnails: %w", err) + } + return nil } diff --git a/templates/book.md.tpl b/templates/book.md.tpl index 0370519..3777ef6 100644 --- a/templates/book.md.tpl +++ b/templates/book.md.tpl @@ -8,6 +8,9 @@ rate: {{ .Rate }} pages: {{ .Pages }} progress: {{ .Progress }} image: {{ .Image }} +image320: {{ imageSize .Image "320" }} +image640: {{ imageSize .Image "640" }} +image1024: {{ imageSize .Image "1024" }} date: {{ .Date }} draft: false tags: {{ .Tags }} diff --git a/templates/game.md.tpl b/templates/game.md.tpl index e9718d0..e000915 100644 --- a/templates/game.md.tpl +++ b/templates/game.md.tpl @@ -6,8 +6,17 @@ year: {{ .Year }} rate: {{ .Rate }} progress: {{ .Progress }} image: {{ .Image }} +image320: {{ imageSize .Image "320" }} +image640: {{ imageSize .Image "640" }} +image1024: {{ imageSize .Image "1024" }} poster-image: {{ .Poster }} +poster-image320: {{ imageSize .Poster "320" }} +poster-image640: {{ imageSize .Poster "640" }} +poster-image1024: {{ imageSize .Poster "1024" }} background-image: {{ .Background }} +background-image320: {{ imageSize .Background "320" }} +background-image640: {{ imageSize .Background "640" }} +background-image1024: {{ imageSize .Background "1024" }} date: {{ .Date }} draft: false tags: {{ .Tags }} diff --git a/templates/movie.md.tpl b/templates/movie.md.tpl index e9718d0..e000915 100644 --- a/templates/movie.md.tpl +++ b/templates/movie.md.tpl @@ -6,8 +6,17 @@ year: {{ .Year }} rate: {{ .Rate }} progress: {{ .Progress }} image: {{ .Image }} +image320: {{ imageSize .Image "320" }} +image640: {{ imageSize .Image "640" }} +image1024: {{ imageSize .Image "1024" }} poster-image: {{ .Poster }} +poster-image320: {{ imageSize .Poster "320" }} +poster-image640: {{ imageSize .Poster "640" }} +poster-image1024: {{ imageSize .Poster "1024" }} background-image: {{ .Background }} +background-image320: {{ imageSize .Background "320" }} +background-image640: {{ imageSize .Background "640" }} +background-image1024: {{ imageSize .Background "1024" }} date: {{ .Date }} draft: false tags: {{ .Tags }} diff --git a/templates/music.md.tpl b/templates/music.md.tpl index 39891f8..3b3fc8c 100644 --- a/templates/music.md.tpl +++ b/templates/music.md.tpl @@ -6,6 +6,9 @@ artist: "{{ .Artist }}" year: {{ .Year }} rate: {{ .Rate }} image: {{ .Image }} +image320: {{ imageSize .Image "320" }} +image640: {{ imageSize .Image "640" }} +image1024: {{ imageSize .Image "1024" }} tracks: {{ .Tracks }} date: {{ .Date }} draft: false diff --git a/templates/serie.md.tpl b/templates/serie.md.tpl index cacf982..4d784c9 100644 --- a/templates/serie.md.tpl +++ b/templates/serie.md.tpl @@ -7,8 +7,17 @@ rate: {{ .Rate }} progress: {{ .Progress }} episode: {{ .Episode }} image: {{ .Image }} +image320: {{ imageSize .Image "320" }} +image640: {{ imageSize .Image "640" }} +image1024: {{ imageSize .Image "1024" }} poster-image: {{ .Poster }} +poster-image320: {{ imageSize .Poster "320" }} +poster-image640: {{ imageSize .Poster "640" }} +poster-image1024: {{ imageSize .Poster "1024" }} background-image: {{ .Background }} +background-image320: {{ imageSize .Background "320" }} +background-image640: {{ imageSize .Background "640" }} +background-image1024: {{ imageSize .Background "1024" }} date: {{ .Date }} draft: false tags: {{ .Tags }} diff --git a/utils/markdown.go b/utils/markdown.go index 4e048c8..411afd9 100644 --- a/utils/markdown.go +++ b/utils/markdown.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "regexp" "strings" "text/template" @@ -22,7 +23,15 @@ type FrontMatter struct { } func GenerateMarkdown(templatePath string, outputPath string, data map[string]interface{}) error { - tmpl, err := template.ParseFiles(templatePath) + // Define a template funcition that generates image versions + funcMap := template.FuncMap{ + "imageSize": func(imagePath string, size string) string { + base := strings.TrimSuffix(imagePath, filepath.Ext(imagePath)) + return fmt.Sprintf("%s-%s.jpg", base, size) + }, + } + + tmpl, err := template.New(filepath.Base(templatePath)).Funcs(funcMap).ParseFiles(templatePath) if err != nil { return err } diff --git a/utils/utils.go b/utils/utils.go index 0ea6d96..5ca2caa 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,12 +1,21 @@ package utils import ( + "bytes" "encoding/json" "fmt" + "image" + "image/jpeg" + _ "image/jpeg" + _ "image/png" + "io/ioutil" "os" + "path/filepath" "regexp" "strings" "unicode" + + "github.com/nfnt/resize" ) func CreateDirIfNotExists(dir string) error { @@ -74,3 +83,49 @@ func Sluggify(s string) string { return slug } + +func GenerateThumbnails(originalPath, slug, outputDir string, resolutions []uint) error { + // Open the original image + file, err := os.Open(originalPath) + if err != nil { + return fmt.Errorf("error opening original image: %w", err) + } + defer file.Close() + + imgData, err := ioutil.ReadAll(file) + if err != nil { + return fmt.Errorf("error reading original image data: %w", err) + } + + // Decode the original image + img, _, err := image.Decode(bytes.NewReader(imgData)) + if err != nil { + return fmt.Errorf("error decoding image: %w", err) + } + + // Generate thumbnails for each resolution + for _, res := range resolutions { + // Resize the image while maintaining the aspect ratio + thumbnail := resize.Resize(res, 0, img, resize.Lanczos3) + + // Define the new filename with resolution suffix + thumbnailFilename := fmt.Sprintf("%s-%d.jpg", slug, res) + thumbnailPath := filepath.Join(outputDir, thumbnailFilename) + + // Save the resized image + thumbnailFile, err := os.Create(thumbnailPath) + if err != nil { + return fmt.Errorf("error creating thumbnail file: %w", err) + } + defer thumbnailFile.Close() + + err = jpeg.Encode(thumbnailFile, thumbnail, nil) + if err != nil { + return fmt.Errorf("error encoding thumbnail: %w", err) + } + + fmt.Printf(" - Thumbnail saved successfully at: %s\n", thumbnailPath) + } + + return nil +}