Split up max value for img height and with into values for img and banner

This commit is contained in:
Jason Streifling 2024-10-27 15:05:11 +01:00
parent d86b9027bf
commit 878f57af08
5 changed files with 82 additions and 115 deletions

View File

@ -6,12 +6,14 @@ tmp_dir = "tmp"
args_bin = [ args_bin = [
"-aes tmp/cpolis.aes", "-aes tmp/cpolis.aes",
"-articles tmp/articles", "-articles tmp/articles",
"-banner-width 512",
"-config tmp/config.toml", "-config tmp/config.toml",
"-desc 'Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität'", "-desc 'Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität'",
"-domain localhost", "-domain localhost",
"-feed tmp/cpolis.atom", "-feed tmp/cpolis.atom",
"-firebase tmp/firebase.json", "-firebase tmp/firebase.json",
"-gob tmp/cpolis.gob", "-gob tmp/cpolis.gob",
"-img-width 256",
"-link https://distrikt-ni-st.de", "-link https://distrikt-ni-st.de",
"-log tmp/cpolis.log", "-log tmp/cpolis.log",
"-pdfs tmp/pdfs", "-pdfs tmp/pdfs",
@ -19,7 +21,6 @@ args_bin = [
"-port 8080", "-port 8080",
"-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'", "-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'",
"-web web", "-web web",
"-width 1024",
] ]
bin = "./tmp/main" bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/main.go" cmd = "go build -o ./tmp/main ./cmd/main.go"

View File

@ -12,44 +12,48 @@ import (
) )
type Config struct { type Config struct {
AESKeyFile string AESKeyFile string
ArticleDir string ArticleDir string
ConfigFile string ConfigFile string
DBName string DBName string
Description string Description string
Domain string Domain string
AtomFeed string AtomFeed string
FirebaseKey string FirebaseKey string
GOBKeyFile string GOBKeyFile string
Link string Link string
LogFile string LogFile string
PDFDir string PDFDir string
PicsDir string PicsDir string
Port string Port string
Title string Title string
Version string Version string
WebDir string WebDir string
MaxImgHeight int MaxBannerHeight int
MaxImgWidth int MaxBannerWidth int
MaxImgHeight int
MaxImgWidth int
} }
func newConfig() *Config { func newConfig() *Config {
return &Config{ return &Config{
AESKeyFile: "/var/www/cpolis/aes.key", AESKeyFile: "/var/www/cpolis/aes.key",
ArticleDir: "/var/www/cpolis/articles", ArticleDir: "/var/www/cpolis/articles",
ConfigFile: "/etc/cpolis/config.toml", ConfigFile: "/etc/cpolis/config.toml",
DBName: "cpolis", DBName: "cpolis",
AtomFeed: "/var/www/cpolis/cpolis.atom", AtomFeed: "/var/www/cpolis/cpolis.atom",
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json", FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
GOBKeyFile: "/var/www/cpolis/gob.key", GOBKeyFile: "/var/www/cpolis/gob.key",
LogFile: "/var/log/cpolis.log", LogFile: "/var/log/cpolis.log",
MaxImgHeight: 1080, MaxBannerHeight: 1080,
MaxImgWidth: 1920, MaxBannerWidth: 1920,
PDFDir: "/var/www/cpolis/pdfs", MaxImgHeight: 1080,
PicsDir: "/var/www/cpolis/pics", MaxImgWidth: 1920,
Port: ":8080", PDFDir: "/var/www/cpolis/pdfs",
Version: "v0.13.0", PicsDir: "/var/www/cpolis/pics",
WebDir: "/var/www/cpolis/web", 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.PicsDir, "pics", c.PicsDir, "pictures directory")
flag.StringVar(&c.Title, "title", c.Title, "channel title") flag.StringVar(&c.Title, "title", c.Title, "channel title")
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory") flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
flag.IntVar(&c.MaxImgHeight, "height", c.MaxImgHeight, "maximum image height") flag.IntVar(&c.MaxBannerHeight, "banner-height", c.MaxBannerHeight, "maximum banner height")
flag.IntVar(&c.MaxImgWidth, "width", c.MaxImgWidth, "maximum image width") 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.IntVar(&port, "port", port, "port")
flag.Parse() flag.Parse()
@ -217,6 +223,14 @@ func (c *Config) setupConfig(cliConfig *Config) error {
return fmt.Errorf("error setting up file: %v", err) 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 { if cliConfig.MaxImgHeight != defaultConfig.MaxImgHeight {
c.MaxImgHeight = cliConfig.MaxImgHeight c.MaxImgHeight = cliConfig.MaxImgHeight
} }

View File

@ -1,6 +1,7 @@
package backend package backend
import ( import (
"encoding/base64"
"fmt" "fmt"
"image" "image"
"io" "io"
@ -13,7 +14,7 @@ import (
var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging 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)) img, err := imaging.Decode(src, imaging.AutoOrientation(true))
if err != nil { if err != nil {
if err == ErrUnsupportedFormat { if err == ErrUnsupportedFormat {
@ -22,15 +23,15 @@ func SaveImage(c *Config, src io.Reader) (string, error) {
return "", fmt.Errorf("error decoding image: %v", err) return "", fmt.Errorf("error decoding image: %v", err)
} }
if img.Bounds().Dy() > c.MaxImgHeight { if img.Bounds().Dy() > maxHeight {
img = imaging.Resize(img, 0, c.MaxImgHeight, imaging.Lanczos) img = imaging.Resize(img, 0, maxHeight, imaging.Lanczos)
} }
if img.Bounds().Dx() > c.MaxImgWidth { if img.Bounds().Dx() > maxWidth {
img = imaging.Resize(img, c.MaxImgWidth, 0, imaging.Lanczos) img = imaging.Resize(img, maxWidth, 0, imaging.Lanczos)
} }
filename := fmt.Sprint(uuid.New(), ".webp") filename := fmt.Sprint(uuid.New(), ".webp")
file, err := os.Create(c.PicsDir + "/" + filename) file, err := os.Create(path + filename)
if err != nil { if err != nil {
return "", fmt.Errorf("error creating new image file: %v", err) 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 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
}

View File

@ -348,7 +348,7 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
imgURL := strings.Split(data.Article.BannerLink, "/") imgURL := strings.Split(data.Article.BannerLink, "/")
imgFileName := imgURL[len(imgURL)-1] imgFileName := imgURL[len(imgURL)-1]
data.BannerImage, err = serveBase64Image(c, imgFileName) data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -625,7 +625,7 @@ func ReviewArticle(c *b.Config, db *b.DB, s *b.CookieStore, action, title, butto
imgURL := strings.Split(article.BannerLink, "/") imgURL := strings.Split(article.BannerLink, "/")
imgFileName := imgURL[len(imgURL)-1] imgFileName := imgURL[len(imgURL)-1]
data.BannerImage, err = serveBase64Image(c, imgFileName) data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -808,7 +808,7 @@ func EditArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
imgURL := strings.Split(data.Article.BannerLink, "/") imgURL := strings.Split(data.Article.BannerLink, "/")
imgFileName := imgURL[len(imgURL)-1] imgFileName := imgURL[len(imgURL)-1]
data.BannerImage, err = serveBase64Image(c, imgFileName) data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -1,35 +1,14 @@
package frontend package frontend
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"fmt"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"os"
b "streifling.com/jason/cpolis/cmd/backend" b "streifling.com/jason/cpolis/cmd/backend"
) )
func serveBase64Image(c *b.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
}
func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc { func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if _, err := getSession(w, r, c, s); err != nil { if _, err := getSession(w, r, c, s); err != nil {
@ -52,7 +31,7 @@ func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
} }
defer file.Close() defer file.Close()
filename, err := b.SaveImage(c, file) filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
if err != nil { if err != nil {
if err == b.ErrUnsupportedFormat { if err == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest) http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
@ -69,51 +48,6 @@ func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
} }
} }
func UploadIssueImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := getSession(w, r, c, s)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := r.ParseMultipartForm(10 << 20); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
file, _, err := r.FormFile("issue-image")
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
filename, err := b.SaveImage(c, file)
if err != nil {
if err == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
return
}
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["issue-image"] = filename
if err = session.Save(r, w); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func UploadBanner(c *b.Config, s *b.CookieStore, fileKey, htmlFile, htmlTemplate string) http.HandlerFunc { func UploadBanner(c *b.Config, s *b.CookieStore, fileKey, htmlFile, htmlTemplate string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if _, err := getSession(w, r, c, s); err != nil { if _, err := getSession(w, r, c, s); err != nil {
@ -130,7 +64,7 @@ func UploadBanner(c *b.Config, s *b.CookieStore, fileKey, htmlFile, htmlTemplate
} }
defer file.Close() defer file.Close()
filename, err := b.SaveImage(c, file) filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
if err != nil { if err != nil {
if err == b.ErrUnsupportedFormat { if err == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest) http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
@ -141,7 +75,7 @@ func UploadBanner(c *b.Config, s *b.CookieStore, fileKey, htmlFile, htmlTemplate
return return
} }
base64Img, err := serveBase64Image(c, filename) base64Img, err := b.ServeBase64Image(c, filename)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)