go-skeletor/controller/upload.go

218 lines
5.0 KiB
Go
Raw Normal View History

2021-11-02 11:25:21 +01:00
package controller
import (
"arno/skeletor/config"
"arno/skeletor/service"
"arno/skeletor/tool"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"github.com/nfnt/resize"
"github.com/google/uuid"
"github.com/martini-contrib/sessions"
"github.com/martini-contrib/render"
)
func Upload(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, typeupload string, id string) {
myconfig := config.Must(ctn)
isonefile := true
iscallback := true
iscropped := false
path := setPath(ctn, "http", typeupload, id)
redirect := ""
acceptedFiles := ""
if typeupload == "logo" {
acceptedFiles = ".jpg,.jpeg,.gif,.png,.svg"
}
if typeupload == "avatar" {
iscropped = true
}
if iscropped {
iscallback = false
redirect = myconfig.AppRoutes["uploadcrop"] + typeupload + "/" + id
}
rendermap := map[string]interface{}{
"conf": myconfig,
"session": tool.Rendersession(session),
"useheader": false,
"usesidebar": false,
"usecontainer": true,
"typeupload": typeupload,
"id": id,
"path": path,
"acceptedFiles": acceptedFiles,
"isonefile": isonefile,
"iscallback": iscallback,
"iscropped": iscropped,
"redirect": redirect,
}
r.HTML(200, "upload/upload", rendermap)
}
func Uploaded(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, typeupload string, id string) {
file, header, _ := req.FormFile("file")
defer file.Close()
isrename := true
isresize := false
resizeheight := uint(1200)
if typeupload == "logo" {
isresize = true
resizeheight = uint(120)
}
filename := saveFile(ctn, file, header, typeupload, id, isrename, isresize, resizeheight)
newmap := map[string]interface{}{"file": filename}
r.JSON(200, newmap)
}
func UploadCrop(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, typeupload string, id string) {
myconfig := config.Must(ctn)
pathfile, _ := req.URL.Query()["pathfile"]
filename, _ := req.URL.Query()["filename"]
path := setPath(ctn, "http", typeupload, id)
iscallback := false
redirect := ""
if typeupload == "avatar" {
iscallback = true
}
rendermap := map[string]interface{}{
"conf": myconfig,
"session": tool.Rendersession(session),
"useheader": false,
"usesidebar": false,
"usecontainer": true,
"typeupload": typeupload,
"id": id,
"path": path,
"submitroute": myconfig.AppRoutes["uploadcropped"] + typeupload + "/" + id,
"filename": filename[0],
"pathfile": pathfile[0],
"iscallback": iscallback,
"redirect": redirect,
}
r.HTML(200, "upload/crop", rendermap)
}
func UploadCropped(ctn *service.Container, session sessions.Session, req *http.Request, r render.Render, typeupload string, id string) {
file, header, _ := req.FormFile("file")
defer file.Close()
isrename := false
isresize := false
resizeheight := uint(1200)
if typeupload == "avatar" {
isresize = true
resizeheight = uint(120)
}
filename := saveFile(ctn, file, header, typeupload, id, isrename, isresize, resizeheight)
newmap := map[string]interface{}{"file": filename}
r.JSON(200, newmap)
}
func saveFile(ctn *service.Container, file multipart.File, handle *multipart.FileHeader, typeupload string, id string, isrename bool, isresize bool, resizeheight uint) string {
data, _ := ioutil.ReadAll(file)
filename := handle.Filename
if isrename {
filename = uuid.New().String()
}
mineType := handle.Header.Get("Content-Type")
if isrename {
if mineType == "image/jpeg" {
filename = filename + ".jpg"
}
if mineType == "image/png" {
filename = filename + ".png"
}
if mineType == "image/gif" {
filename = filename + ".gif"
}
if mineType == "image/svg+xml" {
filename = filename + ".svg"
}
}
path := setPath(ctn, "os", typeupload, id) + filename
ioutil.WriteFile(path, data, 0666)
if isresize {
resizeFile(mineType, path, resizeheight)
}
return filename
}
func setPath(ctn *service.Container, mode string, typeupload string, id string) string {
myconfig := config.Must(ctn)
racine := ""
if mode == "http" {
racine = myconfig.AppWeburl
} else {
racine = "./public"
}
path := racine + "/uploads/" + typeupload + "/"
return path
}
func resizeFile(mineType string, path string, resizeheight uint) {
file, _ := os.Open(path)
if mineType == "image/jpeg" {
img, _ := jpeg.Decode(file)
file.Close()
m := resize.Resize(0, resizeheight, img, resize.Lanczos3)
out, _ := os.Create(path)
defer out.Close()
jpeg.Encode(out, m, nil)
}
if mineType == "image/png" {
img, _ := png.Decode(file)
file.Close()
m := resize.Resize(0, resizeheight, img, resize.Lanczos3)
out, _ := os.Create(path)
defer out.Close()
png.Encode(out, m)
}
if mineType == "image/gif" {
img, _ := gif.Decode(file)
file.Close()
m := resize.Resize(0, resizeheight, img, resize.Lanczos3)
out, _ := os.Create(path)
defer out.Close()
gif.Encode(out, m, nil)
}
if mineType == "image/svg+xml" {
// No resize for svg image
}
}