feat:update gen

This commit is contained in:
zhangwenjian committed 2020-09-12 01:07:35 +08:00
1 parent 8a7a9db7fd
commit 2d15f91f2e
15 files changed
+220 -256

No files matched your search

+46 -7
View File
@@ -5,14 +5,25 @@ import (
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
imgType "github.com/shamsher31/goimgtype"
"go-admin/pkg/utils"
"go-admin/tools"
"go-admin/tools/app"
)
type FileResponse struct {
Size int64 `json:"size"`
Path string `json:"path"`
FullPath string `json:"full_path"`
Name string `json:"name"`
Type string `json:"type"`
}
// @Summary 上传图片
// @Description 获取JSON
// @Tags 公共接口
@@ -25,6 +36,7 @@ import (
func UploadFile(c *gin.Context) {
tag, _ := c.GetPostForm("type")
urlPerfix := fmt.Sprintf("http://%s/", c.Request.Host)
var fileResponse FileResponse
if tag == "" {
app.Error(c, 200, errors.New(""), "缺少标识")
return
@@ -41,25 +53,52 @@ func UploadFile(c *gin.Context) {
singleFile := "static/uploadfile/" + guid + utils.GetExt(files.Filename)
_ = c.SaveUploadedFile(files, singleFile)
app.OK(c, urlPerfix+singleFile, "上传成功")
fileType, _ := imgType.Get(singleFile)
fileResponse = FileResponse{
Size: tools.GetFileSize(singleFile),
Path: singleFile,
FullPath: urlPerfix + singleFile,
Name: files.Filename,
Type: fileType,
}
app.OK(c, fileResponse, "上传成功")
return
case "2": // 多图
files := c.Request.MultipartForm.File["file"]
multipartFile := make([]string, len(files))
var multipartFile []FileResponse
for _, f := range files {
guid := uuid.New().String()
multipartFileName := "static/uploadfile/" + guid + utils.GetExt(f.Filename)
_ = c.SaveUploadedFile(f, multipartFileName)
multipartFile = append(multipartFile, urlPerfix+multipartFileName)
e := c.SaveUploadedFile(f, multipartFileName)
fileType, _ := imgType.Get(multipartFileName)
if e == nil {
multipartFile = append(multipartFile, FileResponse{
Size: tools.GetFileSize(multipartFileName),
Path: multipartFileName,
FullPath: urlPerfix + multipartFileName,
Name: f.Filename,
Type: fileType,
})
}
}
app.OK(c, multipartFile, "上传成功")
return
case "3": // base64
files, _ := c.GetPostForm("file")
ddd, _ := base64.StdEncoding.DecodeString(files)
file2list := strings.Split(files, ",")
ddd, _ := base64.StdEncoding.DecodeString(file2list[1])
guid := uuid.New().String()
_ = ioutil.WriteFile("static/uploadfile/"+guid+".jpg", ddd, 0666)
app.OK(c, urlPerfix+"static/uploadfile/"+guid+".jpg", "上传成功")
base64File := "static/uploadfile/" + guid + ".jpg"
_ = ioutil.WriteFile(base64File, ddd, 0666)
typeStr := strings.Replace(strings.Replace(file2list[0], "data:", "", -1), ";base64", "", -1)
fileResponse = FileResponse{
Size: tools.GetFileSize(base64File),
Path: base64File,
FullPath: urlPerfix + base64File,
Name: "",
Type: typeStr,
}
app.OK(c, fileResponse, "上传成功")
}
}
}