ferry/apis/public/file.go

115 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-08-19 01:09:01 +08:00
package public
import (
"encoding/base64"
"errors"
"ferry/tools/app"
"fmt"
"io/ioutil"
2020-09-17 14:54:36 +08:00
"os"
2020-08-19 17:11:39 +08:00
"strings"
"github.com/spf13/viper"
2020-08-19 01:09:01 +08:00
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// @Summary 上传图片
// @Description 获取JSON
// @Tags 公共接口
// @Accept multipart/form-data
// @Param type query string true "type" (1单图2多图, 3base64图片)
// @Param file formData file true "file"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/public/uploadFile [post]
func UploadFile(c *gin.Context) {
2020-08-19 17:11:39 +08:00
var (
2020-09-17 14:54:36 +08:00
urlPrefix string
tag string
fileType string
saveFilePath string
err error
2020-09-18 16:31:19 +08:00
protocol string = "http"
2020-10-28 10:11:34 +08:00
requestHost string
2020-08-19 17:11:39 +08:00
)
tag, _ = c.GetPostForm("type")
2020-09-17 14:54:36 +08:00
fileType = c.DefaultQuery("file_type", "images")
2020-08-21 18:35:16 +08:00
2020-09-17 14:58:48 +08:00
if fileType != "images" && fileType != "files" {
app.Error(c, -1, fmt.Errorf("上传接口目前,仅支持图片上传和文件上传"), "")
return
}
2020-09-18 16:31:19 +08:00
if strings.HasPrefix(c.Request.Header.Get("Origin"), "https") {
protocol = "https"
}
2020-10-28 10:11:34 +08:00
requestHostList := strings.Split(c.Request.Host, ":")
if len(requestHostList) > 1 && requestHostList[1] == "80" {
2020-10-28 10:11:34 +08:00
requestHost = requestHostList[0]
} else {
requestHost = c.Request.Host
}
2020-08-21 18:35:16 +08:00
if viper.GetBool("settings.domain.getHost") {
2020-10-28 10:11:34 +08:00
urlPrefix = fmt.Sprintf("%s://%s/", protocol, requestHost)
2020-08-19 17:11:39 +08:00
} else {
2020-10-28 10:51:35 +08:00
urlPrefix = fmt.Sprintf("%s://%s", protocol, viper.GetString("settings.domain.url"))
if !strings.HasSuffix(viper.GetString("settings.domain.url"), "/") {
urlPrefix = urlPrefix + "/"
2020-08-21 18:35:16 +08:00
}
2020-08-19 17:11:39 +08:00
}
2020-08-21 18:35:16 +08:00
2020-08-19 01:09:01 +08:00
if tag == "" {
2020-09-17 14:54:36 +08:00
tag = "1"
}
2020-08-19 01:09:01 +08:00
2020-09-17 14:54:36 +08:00
saveFilePath = "static/uploadfile/" + fileType + "/"
_, err = os.Stat(saveFilePath)
if os.IsNotExist(err) {
err = os.MkdirAll(saveFilePath, 0755)
if err != nil {
app.Error(c, -1, err, fmt.Sprintf("创建图片目录失败,%v", err.Error()))
2020-08-19 01:09:01 +08:00
return
2020-09-17 14:54:36 +08:00
}
}
2020-09-27 02:51:27 +08:00
guid := strings.ReplaceAll(uuid.New().String(), "-", "")
2020-09-17 14:54:36 +08:00
switch tag {
case "1": // 单图
files, err := c.FormFile("file")
if err != nil {
app.Error(c, 200, errors.New(""), "图片不能为空")
2020-08-19 01:09:01 +08:00
return
2020-09-17 14:54:36 +08:00
}
// 上传文件至指定目录
2020-09-27 02:51:27 +08:00
singleFile := saveFilePath + guid + "-" + files.Filename
2020-09-17 14:54:36 +08:00
_ = c.SaveUploadedFile(files, singleFile)
app.OK(c, urlPrefix+singleFile, "上传成功")
return
case "2": // 多图
files := c.Request.MultipartForm.File["file"]
multipartFile := make([]string, len(files))
for _, f := range files {
2020-09-27 02:51:27 +08:00
guid = strings.ReplaceAll(uuid.New().String(), "-", "")
multipartFileName := saveFilePath + guid + "-" + f.Filename
2020-09-17 14:54:36 +08:00
_ = c.SaveUploadedFile(f, multipartFileName)
multipartFile = append(multipartFile, urlPrefix+multipartFileName)
2020-08-19 01:09:01 +08:00
}
2020-09-17 14:54:36 +08:00
app.OK(c, multipartFile, "上传成功")
return
case "3": // base64
files, _ := c.GetPostForm("file")
ddd, _ := base64.StdEncoding.DecodeString(files)
_ = ioutil.WriteFile(saveFilePath+guid+".jpg", ddd, 0666)
app.OK(c, urlPrefix+saveFilePath+guid+".jpg", "上传成功")
default:
app.Error(c, 200, errors.New(""), "标识不正确")
return
2020-08-19 01:09:01 +08:00
}
}