|
@@ -3,7 +3,6 @@ package controllers
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"eta/eta_api/models"
|
|
|
- "eta/eta_api/models/company"
|
|
|
"eta/eta_api/models/report_approve"
|
|
|
"eta/eta_api/services"
|
|
|
"eta/eta_api/services/alarm_msg"
|
|
@@ -11,9 +10,11 @@ import (
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/server/web"
|
|
|
+ "github.com/h2non/filetype"
|
|
|
"github.com/rdlucklib/rdluck_tools/paging"
|
|
|
"github.com/tealeg/xlsx"
|
|
|
"html"
|
|
|
+ "io/ioutil"
|
|
|
"os"
|
|
|
"path"
|
|
|
"path/filepath"
|
|
@@ -1226,7 +1227,7 @@ func (this *ReportController) SaveReportContent() {
|
|
|
// @Param File query file true "文件"
|
|
|
// @Success 200 上传成功
|
|
|
// @router /uploadImg [post]
|
|
|
-func (this *ReportController) UploadImg() {
|
|
|
+func (this *ReportCommonController) UploadImg() {
|
|
|
br := new(models.BaseResponse).Init()
|
|
|
defer func() {
|
|
|
if br.ErrMsg == "" {
|
|
@@ -1235,13 +1236,6 @@ func (this *ReportController) UploadImg() {
|
|
|
this.Data["json"] = br
|
|
|
this.ServeJSON()
|
|
|
}()
|
|
|
- sysUser := this.SysUser
|
|
|
- if sysUser == nil {
|
|
|
- br.Msg = "请登录"
|
|
|
- br.ErrMsg = "请登录,SysUser Is Empty"
|
|
|
- br.Ret = 408
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
var err error
|
|
|
defer func() {
|
|
@@ -1255,24 +1249,23 @@ func (this *ReportController) UploadImg() {
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+ defer f.Close() //关闭上传文件
|
|
|
|
|
|
- // 限制文件类型
|
|
|
- confVal, e := company.GetConfigDetailByCode(company.ConfUploadAllowImgExt)
|
|
|
- if e != nil && e.Error() != utils.ErrNoRow() {
|
|
|
- br.Msg = "操作失败"
|
|
|
- br.ErrMsg = "文件上传失败, Err: " + e.Error()
|
|
|
+ // 不依赖于文件扩展名检查文件格式
|
|
|
+ fileData, e := ioutil.ReadAll(f)
|
|
|
+ if e != nil {
|
|
|
+ br.Msg = "上传失败"
|
|
|
+ br.ErrMsg = "读取文件失败, Err: " + e.Error()
|
|
|
return
|
|
|
}
|
|
|
- allowExt := make([]string, 0)
|
|
|
- if confVal.ConfigValue != "" {
|
|
|
- allowExt = strings.Split(confVal.ConfigValue, ",")
|
|
|
- }
|
|
|
- ext := path.Ext(h.Filename)
|
|
|
- if !utils.InArrayByStr(allowExt, ext) {
|
|
|
- br.Msg = "图片格式有误"
|
|
|
+ pass := filetype.IsImage(fileData)
|
|
|
+ if !pass {
|
|
|
+ br.Msg = "文件格式有误"
|
|
|
+ br.ErrMsg = "文件格式有误"
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ ext := path.Ext(h.Filename)
|
|
|
dateDir := time.Now().Format("20060102")
|
|
|
uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
|
|
|
err = os.MkdirAll(uploadDir, utils.DIR_MOD)
|
|
@@ -1282,24 +1275,16 @@ func (this *ReportController) UploadImg() {
|
|
|
randStr := utils.GetRandStringNoSpecialChar(28)
|
|
|
fileName := randStr + ext
|
|
|
fpath := uploadDir + "/" + fileName
|
|
|
- defer f.Close() //关闭上传文件
|
|
|
err = this.SaveToFile("file", fpath)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+ defer func() {
|
|
|
+ os.Remove(fpath)
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 上传至对象存储
|
|
|
resourceUrl := ``
|
|
|
- //上传到阿里云 和 minio
|
|
|
- //if utils.ObjectStorageClient == "minio" {
|
|
|
- // resourceUrl, err = services.UploadImgToMinIo(fileName, fpath)
|
|
|
- // if err != nil {
|
|
|
- // return
|
|
|
- // }
|
|
|
- //} else {
|
|
|
- // resourceUrl, err = services.UploadAliyunV2(fileName, fpath)
|
|
|
- // if err != nil {
|
|
|
- // return
|
|
|
- // }
|
|
|
- //}
|
|
|
ossClient := services.NewOssClient()
|
|
|
if ossClient == nil {
|
|
|
err = fmt.Errorf("初始化OSS服务失败")
|
|
@@ -1311,10 +1296,6 @@ func (this *ReportController) UploadImg() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- defer func() {
|
|
|
- os.Remove(fpath)
|
|
|
- }()
|
|
|
-
|
|
|
item := new(models.Resource)
|
|
|
item.ResourceUrl = resourceUrl
|
|
|
item.ResourceType = 1
|