- Updated `fs.go` and `minio.go` to replace hardcoded storage paths with values from the new config package. - Introduced a new `config` package to manage application configuration, including database and storage settings. - Removed the `models.go` file from `typedefs` as it was no longer needed. - Added a new `config.go` file to handle loading environment variables and application settings. - Created a new `types.go` file in `typedefs` to define storage strategies. - Updated `storage_handler.go` to utilize the new configuration structure.
119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/nfnt/resize"
|
|
"github.com/nickalie/go-webpbin"
|
|
)
|
|
|
|
func Conversion(input string) error {
|
|
var width uint = 1920
|
|
output := filepath.Base(input)
|
|
outputdir := filepath.Dir(input)
|
|
ext := filepath.Ext(output)
|
|
output = output[0 : len(output)-len(ext)] // Remove file extension // Add .webp extension
|
|
log.Println("Converting", input, "to", output+".webp")
|
|
f, err := os.Open(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
var img image.Image
|
|
|
|
switch ext {
|
|
case ".webp":
|
|
log.Println("Image already in WebP format")
|
|
img, err = webpbin.Decode(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case ".png", ".PNG":
|
|
// Convert from PNG
|
|
img, err = png.Decode(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case ".jpeg", ".jpg", ".JPG", ".JPEG":
|
|
// Convert from JPEG
|
|
img, err = jpeg.Decode(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
return fmt.Errorf("error: Unsupported file type: %s", ext)
|
|
}
|
|
|
|
err = image_creation(img, width, output, outputdir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = thumbnail_creation(img, output, outputdir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("Conversion completed successfully")
|
|
|
|
return nil
|
|
}
|
|
|
|
func image_creation(img image.Image, width uint, output string, outputdir string) error {
|
|
// Resize the image
|
|
resizedImg := resize.Resize(width, 0, img, resize.Lanczos3)
|
|
|
|
// Create the output file
|
|
outputPath := fmt.Sprintf("%s/%s.webp", outputdir, output)
|
|
log.Println("Creating", outputPath)
|
|
out, err := os.Create(outputPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer out.Close()
|
|
|
|
// Encode the resized image to the output file
|
|
err = webpbin.Encode(out, resizedImg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func thumbnail_creation(img image.Image, output string, outputdir string) error {
|
|
// Resize the image
|
|
resizedImg := resize.Resize(32, 0, img, resize.Lanczos3)
|
|
|
|
// Create the output file
|
|
outputPath := fmt.Sprintf("%s/thumbnail-%s.webp", outputdir, output)
|
|
log.Println("Creating", outputPath)
|
|
out, err := os.Create(outputPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer out.Close()
|
|
|
|
// Encode the resized image to the output file
|
|
err = webpbin.Encode(out, resizedImg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FallbackImage(c echo.Context) error {
|
|
// Serve a fallback image if the requested image is not found
|
|
fallbackPath := "fallback.webp" // Path to your fallback image
|
|
if _, err := os.Stat(fallbackPath); os.IsNotExist(err) {
|
|
return c.String(http.StatusNotFound, "Fallback image not found")
|
|
}
|
|
return c.File(fallbackPath)
|
|
}
|