|
|
@ -2,15 +2,12 @@ package v1 |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"gin-vue-admin/global" |
|
|
|
"gin-vue-admin/global/response" |
|
|
|
"gin-vue-admin/model" |
|
|
|
"gin-vue-admin/model/request" |
|
|
|
resp "gin-vue-admin/model/response" |
|
|
|
"gin-vue-admin/service" |
|
|
|
"gin-vue-admin/utils" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
|
// @Tags ExaFileUploadAndDownload
|
|
|
@ -22,42 +19,19 @@ import ( |
|
|
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
|
|
|
|
// @Router /fileUploadAndDownload/upload [post]
|
|
|
|
func UploadFile(c *gin.Context) { |
|
|
|
var file model.ExaFileUploadAndDownload |
|
|
|
noSave := c.DefaultQuery("noSave", "0") |
|
|
|
_, header, err := c.Request.FormFile("file") |
|
|
|
if err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c) |
|
|
|
} else { |
|
|
|
// 文件上传后拿到文件路径
|
|
|
|
var uploadErr error |
|
|
|
var filePath string |
|
|
|
var key string |
|
|
|
if global.GVA_CONFIG.LocalUpload.Local { |
|
|
|
// 本地上传
|
|
|
|
uploadErr, filePath, key = utils.UploadFileLocal(header) |
|
|
|
} else { |
|
|
|
// 七牛云上传
|
|
|
|
uploadErr, filePath, key = utils.UploadRemote(header) |
|
|
|
} |
|
|
|
if uploadErr != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c) |
|
|
|
} else { |
|
|
|
// 修改数据库后得到修改后的user并且返回供前端使用
|
|
|
|
var file model.ExaFileUploadAndDownload |
|
|
|
file.Url = filePath |
|
|
|
file.Name = header.Filename |
|
|
|
s := strings.Split(file.Name, ".") |
|
|
|
file.Tag = s[len(s)-1] |
|
|
|
file.Key = key |
|
|
|
if noSave == "0" { |
|
|
|
err = service.Upload(file) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("修改数据库链接失败,%v", err), c) |
|
|
|
} else { |
|
|
|
response.OkDetailed(resp.ExaFileResponse{File: file}, "上传成功", c) |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
err, file = service.UploadFile(header, noSave) // 文件上传后拿到文件路径
|
|
|
|
if err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("修改数据库链接失败,%v", err), c) |
|
|
|
return |
|
|
|
} |
|
|
|
response.OkDetailed(resp.ExaFileResponse{File: file}, "上传成功", c) |
|
|
|
} |
|
|
|
|
|
|
|
// @Tags ExaFileUploadAndDownload
|
|
|
@ -70,23 +44,11 @@ func UploadFile(c *gin.Context) { |
|
|
|
func DeleteFile(c *gin.Context) { |
|
|
|
var file model.ExaFileUploadAndDownload |
|
|
|
_ = c.ShouldBindJSON(&file) |
|
|
|
err, f := service.FindFile(file.ID) |
|
|
|
if err != nil { |
|
|
|
if err := service.DeleteFile(file); err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c) |
|
|
|
} else { |
|
|
|
err = utils.DeleteFile(f.Key) |
|
|
|
if err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c) |
|
|
|
|
|
|
|
} else { |
|
|
|
err = service.DeleteFile(f) |
|
|
|
if err != nil { |
|
|
|
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c) |
|
|
|
} else { |
|
|
|
response.OkWithMessage("删除成功", c) |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
response.OkWithMessage("删除成功", c) |
|
|
|
} |
|
|
|
|
|
|
|
// @Tags ExaFileUploadAndDownload
|
|
|
|