hugo-medialog/utils/utils.go

132 lines
2.9 KiB
Go
Raw Normal View History

2024-10-08 21:08:01 +02:00
package utils
import (
2024-10-28 12:57:45 +01:00
"bytes"
2024-10-09 21:04:10 +02:00
"encoding/json"
"fmt"
2024-10-28 12:57:45 +01:00
"image"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
2024-10-08 21:08:01 +02:00
"os"
2024-10-28 12:57:45 +01:00
"path/filepath"
2024-10-10 14:13:55 +02:00
"regexp"
"strings"
"unicode"
2024-10-28 12:57:45 +01:00
"github.com/nfnt/resize"
2024-10-08 21:08:01 +02:00
)
func CreateDirIfNotExists(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, 0755)
}
return nil
}
2024-10-09 21:04:10 +02:00
func Debug(v interface{}, args ...string) {
// utils.Debug(variable)
debug, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
// Print a title if there is one
if len(args) > 0 {
fmt.Printf("%s\n", args[0])
}
fmt.Printf("DEBUG:\n%s\n", string(debug))
}
func DebugBody(body []byte) {
// utils.DebugBody(body)
var jsonResponse interface{}
err := json.Unmarshal(body, &jsonResponse)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
}
Debug(jsonResponse, "--API RESPONSE--")
}
2024-10-10 20:04:06 +02:00
func Print(v interface{}) {
fmt.Println("%#v", v)
}
2024-10-09 21:04:10 +02:00
func Sep() {
separator := "════════════════════════════════════════════════"
fmt.Println(separator)
}
2024-10-10 14:13:55 +02:00
func Sluggify(s string) string {
slug := strings.ToLower(s)
var b strings.Builder
for _, r := range slug {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
b.WriteRune(r)
} else {
switch r {
case ' ', '-', '_':
b.WriteRune('-')
}
}
}
slug = b.String()
reg := regexp.MustCompile("[^a-z0-9-]+")
slug = reg.ReplaceAllString(slug, "")
slug = strings.ReplaceAll(slug, "--", "-")
slug = strings.Trim(slug, "-")
return slug
}
2024-10-28 12:57:45 +01:00
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
}