Split up max value for img height and with into values for img and banner
This commit is contained in:
parent
d86b9027bf
commit
878f57af08
@ -6,12 +6,14 @@ tmp_dir = "tmp"
|
||||
args_bin = [
|
||||
"-aes tmp/cpolis.aes",
|
||||
"-articles tmp/articles",
|
||||
"-banner-width 512",
|
||||
"-config tmp/config.toml",
|
||||
"-desc 'Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität'",
|
||||
"-domain localhost",
|
||||
"-feed tmp/cpolis.atom",
|
||||
"-firebase tmp/firebase.json",
|
||||
"-gob tmp/cpolis.gob",
|
||||
"-img-width 256",
|
||||
"-link https://distrikt-ni-st.de",
|
||||
"-log tmp/cpolis.log",
|
||||
"-pdfs tmp/pdfs",
|
||||
@ -19,7 +21,6 @@ args_bin = [
|
||||
"-port 8080",
|
||||
"-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'",
|
||||
"-web web",
|
||||
"-width 1024",
|
||||
]
|
||||
bin = "./tmp/main"
|
||||
cmd = "go build -o ./tmp/main ./cmd/main.go"
|
||||
|
@ -29,6 +29,8 @@ type Config struct {
|
||||
Title string
|
||||
Version string
|
||||
WebDir string
|
||||
MaxBannerHeight int
|
||||
MaxBannerWidth int
|
||||
MaxImgHeight int
|
||||
MaxImgWidth int
|
||||
}
|
||||
@ -43,6 +45,8 @@ func newConfig() *Config {
|
||||
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",
|
||||
@ -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
|
||||
}
|
||||
|
@ -348,7 +348,7 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
|
||||
|
||||
imgURL := strings.Split(data.Article.BannerLink, "/")
|
||||
imgFileName := imgURL[len(imgURL)-1]
|
||||
data.BannerImage, err = serveBase64Image(c, imgFileName)
|
||||
data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
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, "/")
|
||||
imgFileName := imgURL[len(imgURL)-1]
|
||||
data.BannerImage, err = serveBase64Image(c, imgFileName)
|
||||
data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
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, "/")
|
||||
imgFileName := imgURL[len(imgURL)-1]
|
||||
data.BannerImage, err = serveBase64Image(c, imgFileName)
|
||||
data.BannerImage, err = b.ServeBase64Image(c, imgFileName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -1,35 +1,14 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
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()
|
||||
|
||||
filename, err := b.SaveImage(c, file)
|
||||
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
|
||||
if err != nil {
|
||||
if err == b.ErrUnsupportedFormat {
|
||||
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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
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()
|
||||
|
||||
filename, err := b.SaveImage(c, file)
|
||||
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
|
||||
if err != nil {
|
||||
if err == b.ErrUnsupportedFormat {
|
||||
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
|
||||
}
|
||||
|
||||
base64Img, err := serveBase64Image(c, filename)
|
||||
base64Img, err := b.ServeBase64Image(c, filename)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
Loading…
x
Reference in New Issue
Block a user