|
@@ -2,12 +2,14 @@ package controllers
|
|
|
|
|
|
import (
|
|
import (
|
|
"bufio"
|
|
"bufio"
|
|
|
|
+ "encoding/base64"
|
|
"eta/eta_api/models"
|
|
"eta/eta_api/models"
|
|
"eta/eta_api/services"
|
|
"eta/eta_api/services"
|
|
"eta/eta_api/utils"
|
|
"eta/eta_api/utils"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/kgiannakakis/mp3duration/src/mp3duration"
|
|
"github.com/kgiannakakis/mp3duration/src/mp3duration"
|
|
"io"
|
|
"io"
|
|
|
|
+ "net/http"
|
|
"os"
|
|
"os"
|
|
"path"
|
|
"path"
|
|
"regexp"
|
|
"regexp"
|
|
@@ -21,6 +23,11 @@ type ResourceController struct {
|
|
BaseCommonController
|
|
BaseCommonController
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ResourceAuthController 文件资源
|
|
|
|
+type ResourceAuthController struct {
|
|
|
|
+ BaseAuthController
|
|
|
|
+}
|
|
|
|
+
|
|
// @Title 图片上传
|
|
// @Title 图片上传
|
|
// @Description 图片上传接口
|
|
// @Description 图片上传接口
|
|
// @Param file query file true "文件"
|
|
// @Param file query file true "文件"
|
|
@@ -914,3 +921,76 @@ func (this *ResourceController) OssSTSToken() {
|
|
// br.Success = true
|
|
// br.Success = true
|
|
//}
|
|
//}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// FileDownload
|
|
|
|
+// @Title 文件下载
|
|
|
|
+// @Description 文件下载
|
|
|
|
+// @Param FileUrl query string true "文件路径"
|
|
|
|
+// @Success 200 Ret=200 操作成功
|
|
|
|
+// @router /file/download [get]
|
|
|
|
+func (this *ResourceAuthController) FileDownload() {
|
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
|
+ defer func() {
|
|
|
|
+ if br.ErrMsg == "" {
|
|
|
|
+ br.IsSendEmail = false
|
|
|
|
+ }
|
|
|
|
+ this.Data["json"] = br
|
|
|
|
+ this.ServeJSON()
|
|
|
|
+ }()
|
|
|
|
+ sysUser := this.SysUser
|
|
|
|
+ if sysUser == nil {
|
|
|
|
+ br.Msg = "请登录"
|
|
|
|
+ br.ErrMsg = "请登录,SysUser Is Empty"
|
|
|
|
+ br.Ret = 408
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fileName := this.GetString("FileName")
|
|
|
|
+ fileName = strings.TrimSpace(fileName)
|
|
|
|
+ if fileName == "" {
|
|
|
|
+ br.Msg = "参数有误"
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fileEncode := this.GetString("FileUrl")
|
|
|
|
+ fileEncode = strings.TrimSpace(fileEncode)
|
|
|
|
+ if fileEncode == "" {
|
|
|
|
+ br.Msg = "参数有误"
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fileByte, e := base64.StdEncoding.DecodeString(fileEncode)
|
|
|
|
+ if e != nil {
|
|
|
|
+ br.Msg = "下载失败"
|
|
|
|
+ br.ErrMsg = "文件地址解析失败, Err: " + e.Error()
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fileUrl := string(fileByte)
|
|
|
|
+
|
|
|
|
+ // 获取文件
|
|
|
|
+ down, e := http.Get(fileUrl)
|
|
|
|
+ if e != nil {
|
|
|
|
+ br.Msg = "下载失败"
|
|
|
|
+ br.ErrMsg = "文件下载失败, http get: " + e.Error()
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ defer down.Body.Close()
|
|
|
|
+ if down.StatusCode != http.StatusOK {
|
|
|
|
+ br.Msg = "下载失败"
|
|
|
|
+ br.ErrMsg = fmt.Sprintf("文件下载失败, http status: %d", down.StatusCode)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 写入响应流
|
|
|
|
+ _, e = io.Copy(this.Ctx.ResponseWriter, down.Body)
|
|
|
|
+ if e != nil {
|
|
|
|
+ br.Msg = "下载失败"
|
|
|
|
+ br.ErrMsg = "复制文件资源失败, Err: " + e.Error()
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 设置响应头
|
|
|
|
+ this.Ctx.ResponseWriter.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
|
|
|
|
+ this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
+
|
|
|
|
+ br.Ret = 200
|
|
|
|
+ br.Msg = "下载成功"
|
|
|
|
+ br.Success = true
|
|
|
|
+}
|