forked from jason/cpolis
Split up max value for img height and with into values for img and banner
This commit is contained in:
@ -12,44 +12,48 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AESKeyFile string
|
||||
ArticleDir string
|
||||
ConfigFile string
|
||||
DBName string
|
||||
Description string
|
||||
Domain string
|
||||
AtomFeed string
|
||||
FirebaseKey string
|
||||
GOBKeyFile string
|
||||
Link string
|
||||
LogFile string
|
||||
PDFDir string
|
||||
PicsDir string
|
||||
Port string
|
||||
Title string
|
||||
Version string
|
||||
WebDir string
|
||||
MaxImgHeight int
|
||||
MaxImgWidth int
|
||||
AESKeyFile string
|
||||
ArticleDir string
|
||||
ConfigFile string
|
||||
DBName string
|
||||
Description string
|
||||
Domain string
|
||||
AtomFeed string
|
||||
FirebaseKey string
|
||||
GOBKeyFile string
|
||||
Link string
|
||||
LogFile string
|
||||
PDFDir string
|
||||
PicsDir string
|
||||
Port string
|
||||
Title string
|
||||
Version string
|
||||
WebDir string
|
||||
MaxBannerHeight int
|
||||
MaxBannerWidth int
|
||||
MaxImgHeight int
|
||||
MaxImgWidth int
|
||||
}
|
||||
|
||||
func newConfig() *Config {
|
||||
return &Config{
|
||||
AESKeyFile: "/var/www/cpolis/aes.key",
|
||||
ArticleDir: "/var/www/cpolis/articles",
|
||||
ConfigFile: "/etc/cpolis/config.toml",
|
||||
DBName: "cpolis",
|
||||
AtomFeed: "/var/www/cpolis/cpolis.atom",
|
||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||
GOBKeyFile: "/var/www/cpolis/gob.key",
|
||||
LogFile: "/var/log/cpolis.log",
|
||||
MaxImgHeight: 1080,
|
||||
MaxImgWidth: 1920,
|
||||
PDFDir: "/var/www/cpolis/pdfs",
|
||||
PicsDir: "/var/www/cpolis/pics",
|
||||
Port: ":8080",
|
||||
Version: "v0.13.0",
|
||||
WebDir: "/var/www/cpolis/web",
|
||||
AESKeyFile: "/var/www/cpolis/aes.key",
|
||||
ArticleDir: "/var/www/cpolis/articles",
|
||||
ConfigFile: "/etc/cpolis/config.toml",
|
||||
DBName: "cpolis",
|
||||
AtomFeed: "/var/www/cpolis/cpolis.atom",
|
||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||
GOBKeyFile: "/var/www/cpolis/gob.key",
|
||||
LogFile: "/var/log/cpolis.log",
|
||||
MaxBannerHeight: 1080,
|
||||
MaxBannerWidth: 1920,
|
||||
MaxImgHeight: 1080,
|
||||
MaxImgWidth: 1920,
|
||||
PDFDir: "/var/www/cpolis/pdfs",
|
||||
PicsDir: "/var/www/cpolis/pics",
|
||||
Port: ":8080",
|
||||
Version: "v0.13.0",
|
||||
WebDir: "/var/www/cpolis/web",
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,8 +123,10 @@ func (c *Config) handleCliArgs() error {
|
||||
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
|
||||
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
||||
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
||||
flag.IntVar(&c.MaxImgHeight, "height", c.MaxImgHeight, "maximum image height")
|
||||
flag.IntVar(&c.MaxImgWidth, "width", c.MaxImgWidth, "maximum image width")
|
||||
flag.IntVar(&c.MaxBannerHeight, "banner-height", c.MaxBannerHeight, "maximum banner height")
|
||||
flag.IntVar(&c.MaxBannerWidth, "banner-width", c.MaxBannerWidth, "maximum banner width")
|
||||
flag.IntVar(&c.MaxImgHeight, "img-height", c.MaxImgHeight, "maximum image height")
|
||||
flag.IntVar(&c.MaxImgWidth, "img-width", c.MaxImgWidth, "maximum image width")
|
||||
flag.IntVar(&port, "port", port, "port")
|
||||
flag.Parse()
|
||||
|
||||
@ -217,6 +223,14 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
return fmt.Errorf("error setting up file: %v", err)
|
||||
}
|
||||
|
||||
if cliConfig.MaxBannerHeight != defaultConfig.MaxBannerHeight {
|
||||
c.MaxBannerHeight = cliConfig.MaxBannerHeight
|
||||
}
|
||||
|
||||
if cliConfig.MaxBannerWidth != defaultConfig.MaxBannerWidth {
|
||||
c.MaxBannerWidth = cliConfig.MaxBannerWidth
|
||||
}
|
||||
|
||||
if cliConfig.MaxImgHeight != defaultConfig.MaxImgHeight {
|
||||
c.MaxImgHeight = cliConfig.MaxImgHeight
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
@ -13,7 +14,7 @@ import (
|
||||
|
||||
var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging
|
||||
|
||||
func SaveImage(c *Config, src io.Reader) (string, error) {
|
||||
func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, error) {
|
||||
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
if err == ErrUnsupportedFormat {
|
||||
@ -22,15 +23,15 @@ func SaveImage(c *Config, src io.Reader) (string, error) {
|
||||
return "", fmt.Errorf("error decoding image: %v", err)
|
||||
}
|
||||
|
||||
if img.Bounds().Dy() > c.MaxImgHeight {
|
||||
img = imaging.Resize(img, 0, c.MaxImgHeight, imaging.Lanczos)
|
||||
if img.Bounds().Dy() > maxHeight {
|
||||
img = imaging.Resize(img, 0, maxHeight, imaging.Lanczos)
|
||||
}
|
||||
if img.Bounds().Dx() > c.MaxImgWidth {
|
||||
img = imaging.Resize(img, c.MaxImgWidth, 0, imaging.Lanczos)
|
||||
if img.Bounds().Dx() > maxWidth {
|
||||
img = imaging.Resize(img, maxWidth, 0, imaging.Lanczos)
|
||||
}
|
||||
|
||||
filename := fmt.Sprint(uuid.New(), ".webp")
|
||||
file, err := os.Create(c.PicsDir + "/" + filename)
|
||||
file, err := os.Create(path + filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating new image file: %v", err)
|
||||
}
|
||||
@ -42,3 +43,20 @@ func SaveImage(c *Config, src io.Reader) (string, error) {
|
||||
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
func ServeBase64Image(c *Config, filename string) (string, error) {
|
||||
file := c.PicsDir + "/" + filename
|
||||
|
||||
img, err := os.Open(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error opening file %v: %v", file, err)
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
imgBytes, err := io.ReadAll(img)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error turning %v into bytes: %v", file, err)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(imgBytes), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user