package frontend import ( "encoding/json" "html/template" "log" "net/http" b "streifling.com/jason/cpolis/cmd/backend" ) func UploadEasyMDEImage(c *b.Config, s map[string]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if _, err := ManageSession(w, r, c, s); err != nil { http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized) return } file, _, err := r.FormFile("article-image") if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusBadRequest) return } defer file.Close() 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) 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 UploadImage(c *b.Config, s map[string]*Session, fileKey, htmlFile, htmlTemplate string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if _, err := ManageSession(w, r, c, s); err != nil { http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized) return } file, _, err := r.FormFile(fileKey) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusBadRequest) return } defer file.Close() 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) return } log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } data := new(struct{ Image string }) data.Image = filename tmpl, err := template.ParseFiles(c.WebDir + "/templates/" + htmlFile) if err = template.Must(tmpl, err).ExecuteTemplate(w, htmlTemplate, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } }