Compare commits
3 Commits
e88fe268f1
...
9ba5647ddc
Author | SHA1 | Date | |
---|---|---|---|
9ba5647ddc | |||
3ac796e743 | |||
9723046c56 |
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.21.4
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||||
golang.org/x/oauth2 v0.23.0
|
golang.org/x/oauth2 v0.23.0
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
|||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||||
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||||
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
|
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
|
||||||
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
@ -184,5 +184,12 @@ func DownloadImage(url, slug string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -231,5 +231,13 @@ func DownloadImage(url, slug, suffix string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -151,5 +151,12 @@ func DownloadImage(url, slug string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,9 @@ rate: {{ .Rate }}
|
|||||||
pages: {{ .Pages }}
|
pages: {{ .Pages }}
|
||||||
progress: {{ .Progress }}
|
progress: {{ .Progress }}
|
||||||
image: {{ .Image }}
|
image: {{ .Image }}
|
||||||
|
image320: {{ imageSize .Image "320" }}
|
||||||
|
image640: {{ imageSize .Image "640" }}
|
||||||
|
image1024: {{ imageSize .Image "1024" }}
|
||||||
date: {{ .Date }}
|
date: {{ .Date }}
|
||||||
draft: false
|
draft: false
|
||||||
tags: {{ .Tags }}
|
tags: {{ .Tags }}
|
||||||
|
@ -6,8 +6,17 @@ year: {{ .Year }}
|
|||||||
rate: {{ .Rate }}
|
rate: {{ .Rate }}
|
||||||
progress: {{ .Progress }}
|
progress: {{ .Progress }}
|
||||||
image: {{ .Image }}
|
image: {{ .Image }}
|
||||||
|
image320: {{ imageSize .Image "320" }}
|
||||||
|
image640: {{ imageSize .Image "640" }}
|
||||||
|
image1024: {{ imageSize .Image "1024" }}
|
||||||
poster-image: {{ .Poster }}
|
poster-image: {{ .Poster }}
|
||||||
|
poster-image320: {{ imageSize .Poster "320" }}
|
||||||
|
poster-image640: {{ imageSize .Poster "640" }}
|
||||||
|
poster-image1024: {{ imageSize .Poster "1024" }}
|
||||||
background-image: {{ .Background }}
|
background-image: {{ .Background }}
|
||||||
|
background-image320: {{ imageSize .Background "320" }}
|
||||||
|
background-image640: {{ imageSize .Background "640" }}
|
||||||
|
background-image1024: {{ imageSize .Background "1024" }}
|
||||||
date: {{ .Date }}
|
date: {{ .Date }}
|
||||||
draft: false
|
draft: false
|
||||||
tags: {{ .Tags }}
|
tags: {{ .Tags }}
|
||||||
|
@ -6,8 +6,17 @@ year: {{ .Year }}
|
|||||||
rate: {{ .Rate }}
|
rate: {{ .Rate }}
|
||||||
progress: {{ .Progress }}
|
progress: {{ .Progress }}
|
||||||
image: {{ .Image }}
|
image: {{ .Image }}
|
||||||
|
image320: {{ imageSize .Image "320" }}
|
||||||
|
image640: {{ imageSize .Image "640" }}
|
||||||
|
image1024: {{ imageSize .Image "1024" }}
|
||||||
poster-image: {{ .Poster }}
|
poster-image: {{ .Poster }}
|
||||||
|
poster-image320: {{ imageSize .Poster "320" }}
|
||||||
|
poster-image640: {{ imageSize .Poster "640" }}
|
||||||
|
poster-image1024: {{ imageSize .Poster "1024" }}
|
||||||
background-image: {{ .Background }}
|
background-image: {{ .Background }}
|
||||||
|
background-image320: {{ imageSize .Background "320" }}
|
||||||
|
background-image640: {{ imageSize .Background "640" }}
|
||||||
|
background-image1024: {{ imageSize .Background "1024" }}
|
||||||
date: {{ .Date }}
|
date: {{ .Date }}
|
||||||
draft: false
|
draft: false
|
||||||
tags: {{ .Tags }}
|
tags: {{ .Tags }}
|
||||||
|
@ -6,6 +6,9 @@ artist: "{{ .Artist }}"
|
|||||||
year: {{ .Year }}
|
year: {{ .Year }}
|
||||||
rate: {{ .Rate }}
|
rate: {{ .Rate }}
|
||||||
image: {{ .Image }}
|
image: {{ .Image }}
|
||||||
|
image320: {{ imageSize .Image "320" }}
|
||||||
|
image640: {{ imageSize .Image "640" }}
|
||||||
|
image1024: {{ imageSize .Image "1024" }}
|
||||||
tracks: {{ .Tracks }}
|
tracks: {{ .Tracks }}
|
||||||
date: {{ .Date }}
|
date: {{ .Date }}
|
||||||
draft: false
|
draft: false
|
||||||
|
@ -7,8 +7,17 @@ rate: {{ .Rate }}
|
|||||||
progress: {{ .Progress }}
|
progress: {{ .Progress }}
|
||||||
episode: {{ .Episode }}
|
episode: {{ .Episode }}
|
||||||
image: {{ .Image }}
|
image: {{ .Image }}
|
||||||
|
image320: {{ imageSize .Image "320" }}
|
||||||
|
image640: {{ imageSize .Image "640" }}
|
||||||
|
image1024: {{ imageSize .Image "1024" }}
|
||||||
poster-image: {{ .Poster }}
|
poster-image: {{ .Poster }}
|
||||||
|
poster-image320: {{ imageSize .Poster "320" }}
|
||||||
|
poster-image640: {{ imageSize .Poster "640" }}
|
||||||
|
poster-image1024: {{ imageSize .Poster "1024" }}
|
||||||
background-image: {{ .Background }}
|
background-image: {{ .Background }}
|
||||||
|
background-image320: {{ imageSize .Background "320" }}
|
||||||
|
background-image640: {{ imageSize .Background "640" }}
|
||||||
|
background-image1024: {{ imageSize .Background "1024" }}
|
||||||
date: {{ .Date }}
|
date: {{ .Date }}
|
||||||
draft: false
|
draft: false
|
||||||
tags: {{ .Tags }}
|
tags: {{ .Tags }}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -22,7 +23,15 @@ type FrontMatter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GenerateMarkdown(templatePath string, outputPath string, data map[string]interface{}) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -97,7 +106,7 @@ func SaveUpdatedMarkdown(filepath string, frontmatter FrontMatter, content strin
|
|||||||
fileContent := string(fileBytes)
|
fileContent := string(fileBytes)
|
||||||
|
|
||||||
fieldPatterns := map[string]string{
|
fieldPatterns := map[string]string{
|
||||||
"progress": `(?m)^progress: (.*)%$`,
|
"progress": `(?m)^progress: "(.*)%"$`,
|
||||||
"episode": `(?m)^episode: "(.*)"$`,
|
"episode": `(?m)^episode: "(.*)"$`,
|
||||||
"date": `(?m)^date: (.*)$`,
|
"date": `(?m)^date: (.*)$`,
|
||||||
"rate": `(?m)^rate: (\d+\.?\d*)$`,
|
"rate": `(?m)^rate: (\d+\.?\d*)$`,
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateDirIfNotExists(dir string) error {
|
func CreateDirIfNotExists(dir string) error {
|
||||||
@ -74,3 +83,49 @@ func Sluggify(s string) string {
|
|||||||
|
|
||||||
return slug
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user