forked from jason/cpolis
Automatically resize images and save them as webp
This commit is contained in:
@ -4,16 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
@ -448,7 +444,7 @@ func UploadArticleImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("article-image")
|
||||
file, _, err := r.FormFile("article-image")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@ -456,30 +452,13 @@ func UploadArticleImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
nameStrings := strings.Split(header.Filename, ".")
|
||||
extension := "." + nameStrings[len(nameStrings)-1]
|
||||
filename := fmt.Sprint(uuid.New(), extension)
|
||||
absFilepath, err := filepath.Abs(fmt.Sprint(c.PicsDir, "/", filename))
|
||||
filename, err := b.SaveImage(c, file)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
img, err := os.Create(absFilepath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
if _, err = io.Copy(img, file); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
url := fmt.Sprint(c.Domain, "/image/serve/", filename)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(url)
|
||||
|
@ -3,16 +3,13 @@ package frontend
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
@ -152,7 +149,7 @@ func UploadIssueImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("issue-image")
|
||||
file, _, err := r.FormFile("issue-image")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -160,30 +157,13 @@ func UploadIssueImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
nameStrings := strings.Split(header.Filename, ".")
|
||||
extension := "." + nameStrings[len(nameStrings)-1]
|
||||
filename := fmt.Sprint(uuid.New(), extension)
|
||||
absFilepath, err := filepath.Abs(fmt.Sprint(c.PicsDir, "/", filename))
|
||||
filename, err := b.SaveImage(c, file)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
img, err := os.Create(absFilepath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer img.Close()
|
||||
|
||||
if _, err = io.Copy(img, file); err != nil {
|
||||
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)
|
||||
|
Reference in New Issue
Block a user