2024-10-27 07:21:36 +01:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-10-30 03:24:29 +01:00
|
|
|
if _, err := GetSession(w, r, c, s); err != nil {
|
2024-10-27 07:21:36 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _, err := r.FormFile("article-image")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-10-27 15:05:11 +01:00
|
|
|
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
|
2024-10-27 07:21:36 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
url := c.Domain + "/image/serve/" + filename
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UploadBanner(c *b.Config, s *b.CookieStore, fileKey, htmlFile, htmlTemplate string) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-10-30 03:24:29 +01:00
|
|
|
if _, err := GetSession(w, r, c, s); err != nil {
|
2024-10-27 07:21:36 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _, err := r.FormFile(fileKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-10-27 15:05:11 +01:00
|
|
|
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
|
2024-10-27 07:21:36 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-29 16:36:40 +01:00
|
|
|
data := new(struct{ BannerImage string })
|
|
|
|
data.BannerImage = filename
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/" + htmlFile)
|
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, htmlTemplate, data); err != nil {
|
2024-10-27 07:21:36 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-10-29 16:36:40 +01:00
|
|
|
}
|
|
|
|
}
|