1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package controller
- import (
- "cold-logistics/common/file_store"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "gogs.baozhida.cn/zoie/OAuth-core/api"
- "os"
- "path/filepath"
- "strings"
- "time"
- )
- type UploadController struct {
- api.Api
- }
- // FilesUpload 文件上传
- // @Summary 文件上传
- // @Description 文件上传
- // @Tags 工具
- // @Success 200 {object} response.Response{data=string,id=string,msg=string} "{"code": 200, "data": [...]}"
- // @Router /api/captcha [get]
- func (e UploadController) FilesUpload(c *gin.Context) {
- err := e.MakeContext(c).Errors
- if err != nil {
- e.Error(500, err, "服务初始化失败!")
- return
- }
- // 处理文件上传逻辑
- file, err := c.FormFile("file")
- //bizType := c.PostForm("bizType")
- //correlationCode := c.PostForm("correlationCode")
- uploadFileName := file.Filename // 获取文件名
- fileType := filepath.Ext(uploadFileName) // 获取文件类型(扩展名)
- if err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- filePath := file_store.GetCurrentDirectory() + "/uploads/" + uploadFileName
- err = c.SaveUploadedFile(file, filePath)
- if err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- defer func() {
- os.Remove(filePath)
- }()
- // TODO: 文件上传到省平台
- filename := fmt.Sprintf("%s/%s%s", time.Now().Format("2006-01-02"), strings.Replace(uuid.New().String(), "-", "", -1), fileType)
- err = file_store.QiniuFileStore.UpLoad(filename, filePath)
- if err != nil {
- c.JSON(400, gin.H{"error": "文件上传失败"})
- return
- }
- e.Custom(gin.H{
- "code": 200,
- "data": filename,
- "msg": "success",
- })
- }
|