|
|
@@ -1,22 +1,58 @@
|
|
|
package partial
|
|
|
|
|
|
import (
|
|
|
+ "crypto/sha1"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/pkg/errors"
|
|
|
"io"
|
|
|
+ "io/ioutil"
|
|
|
+ "mime"
|
|
|
+ "mime/multipart"
|
|
|
"os"
|
|
|
- "fmt"
|
|
|
"path"
|
|
|
- "mime"
|
|
|
- "time"
|
|
|
"strconv"
|
|
|
- "io/ioutil"
|
|
|
- "crypto/sha1"
|
|
|
+ "time"
|
|
|
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
syslogs "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
- sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
|
|
|
sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
|
|
|
+ sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
|
|
|
)
|
|
|
|
|
|
+/**
|
|
|
+ * 附件上传/下载接口
|
|
|
+ */
|
|
|
+type AttachementUpDownloader interface {
|
|
|
+ /**
|
|
|
+ * @brief: 附件上传接口
|
|
|
+ * @param1 file: post 多媒体file
|
|
|
+ * @param2 fileHeader: 文件相关头部信息
|
|
|
+ * @return1: 附件信息,主要包括id,url, hash和大小。其他字段会自动设置
|
|
|
+ * @return4: 错误信息
|
|
|
+ */
|
|
|
+ Upload(multipart.File, *multipart.FileHeader)(*sysmodel.SysAttachment, error)
|
|
|
+ /**
|
|
|
+ * @brief: 附件下载
|
|
|
+ * @param1 ctx: 上下文
|
|
|
+ * @param2 attach: 福建对象
|
|
|
+ */
|
|
|
+ Download(*entitys.CtrlContext, *sysmodel.SysAttachment)
|
|
|
+}
|
|
|
+
|
|
|
+// 附件上传下载接口
|
|
|
+var _attachementUpDownloader AttachementUpDownloader = &defaultAttachUpDownloader{}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief: 设置附件上传下载接口
|
|
|
+ * @param1 uder: 上传下载接口
|
|
|
+ */
|
|
|
+func SetAttachementUpDownloader(uder AttachementUpDownloader){
|
|
|
+ if uder != nil{
|
|
|
+ _attachementUpDownloader = uder
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// _Upload
|
|
|
// @Title _Upload
|
|
|
// @Description 上传文件
|
|
|
@@ -39,117 +75,22 @@ func SysAttachment_Upload(c *entitys.CtrlContext) {
|
|
|
// @Success 200 {object} Account
|
|
|
// @Failure 403 :id is empty
|
|
|
func SysAttachment_Download(c *entitys.CtrlContext) {
|
|
|
- attrId := c.Ctx.Query("id")
|
|
|
- //gt := c.Ctx.Query("get_thumb")
|
|
|
+ attachId := c.Ctx.Query("id")
|
|
|
+
|
|
|
var engine = c.PlatformDbEngine
|
|
|
var attach sysmodel.SysAttachment
|
|
|
- has, err := engine.ID(attrId).Get(&attach)
|
|
|
+ has, err := engine.ID(attachId).Get(&attach)
|
|
|
if err != nil || !has {
|
|
|
syslogs.Error("获取附件出错了:", err)
|
|
|
c.Ctx.JSON(500, sysmodel.SysReturn{500, "获取附件出错了", nil})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- filePath := "files/" + attach.Id
|
|
|
-
|
|
|
- c.Ctx.Writer.Header().Add("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
|
|
|
- c.Ctx.Writer.Header().Add("Content-Type", mime.TypeByExtension(attach.Ext))
|
|
|
- c.Ctx.Writer.Header().Add("Content-Length", strconv.FormatInt(int64(attach.Size), 10))
|
|
|
- c.Ctx.Writer.Header().Add("Accept-Ranges", "bytes")
|
|
|
-
|
|
|
- if !sysutils.Exists(filePath) {
|
|
|
- c.Ctx.Writer.WriteHeader(400)
|
|
|
- return
|
|
|
- }
|
|
|
- fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Disposition"))
|
|
|
- fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Type"))
|
|
|
- fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Length"))
|
|
|
- fmt.Println("---->", c.Ctx.Writer.Header().Get("Accept-Ranges"))
|
|
|
-
|
|
|
- c.Ctx.File(filePath)
|
|
|
- /*attrId := c.Ctx.Query("id")
|
|
|
- //c.Ctx.Writer.WriteHeader(200)
|
|
|
- /*attrId := c.Ctx.Query("id")
|
|
|
-
|
|
|
- filePath := fmt.Sprintf("files/%s", attrId)
|
|
|
-
|
|
|
- c.Ctx.Header("Content-Type", "image/png")
|
|
|
- c.Ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", attrId))
|
|
|
-
|
|
|
- file, err := ioutil.ReadFile(filePath)
|
|
|
- if err != nil {
|
|
|
- c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- return
|
|
|
+ if _attachementUpDownloader != nil{
|
|
|
+ _attachementUpDownloader.Download(c, &attach)
|
|
|
+ }else{
|
|
|
+ c.Ctx.JSON(500, gin.H{"code": 500, "msg": "附件上传下载接口为nil", "data": nil})
|
|
|
}
|
|
|
- c.Ctx.Writer.Write(file)*/
|
|
|
-
|
|
|
- ////gt := c.Ctx.Query("get_thumb")
|
|
|
- //var attach sysmodel.SysAttachment
|
|
|
- //exist, err := c.Db.SqlMapClient("selectone_sys_attachment", &map[string]interface{}{"id": attrId}).Get(&attach)
|
|
|
- //if !exist && err == nil {
|
|
|
- // err = errors.New("record does not exist")
|
|
|
- //}
|
|
|
- //if err != nil {
|
|
|
- // syslogs.Error("获取附件出错了:", err)
|
|
|
- // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- // return
|
|
|
- //}
|
|
|
- //
|
|
|
- ////domain := c.Ctx.GetString("domain")
|
|
|
- ////filePath := fmt.Sprintf("files/%s/%s", domain, attrId)
|
|
|
- //filePath := fmt.Sprintf("files/%s", attrId)
|
|
|
- //file, err := os.Open(filePath)
|
|
|
- //if err != nil {
|
|
|
- // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- // return
|
|
|
- //}
|
|
|
- //defer file.Close()
|
|
|
- //
|
|
|
- ///*durl := attach.Url
|
|
|
- //if gt != "" {
|
|
|
- // durl += "?getthumb=" + gt
|
|
|
- //}
|
|
|
- //resultBody, err := sysutils.GetFile(durl)
|
|
|
- //if err != nil {
|
|
|
- // syslogs.Error("下载附件出错了:", err)
|
|
|
- // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- // return
|
|
|
- //}
|
|
|
- //defer resultBody.Close()*/
|
|
|
- //
|
|
|
- //bbuf := bytes.NewBuffer([]byte{})
|
|
|
- //
|
|
|
- //c.Ctx.Header("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
|
|
|
- ////c.Ctx.Header("Content-Type", strings.Replace(mime.TypeByExtension(attach.Ext), "charset=utf-8", "", -1))
|
|
|
- //c.Ctx.Header("Content-Type", "image/jpeg")
|
|
|
- //
|
|
|
- //buff := make([]byte, 1024)
|
|
|
- //var ssize int64 = 0
|
|
|
- //
|
|
|
- //for {
|
|
|
- // n, err := io.ReadFull(file, buff)
|
|
|
- // if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
|
|
- // syslogs.Error("下载附件出错了:", err)
|
|
|
- // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- // return
|
|
|
- // }
|
|
|
- // if n <= 0 {
|
|
|
- // break
|
|
|
- // }
|
|
|
- // bbuf.Write(buff[:n])
|
|
|
- // ssize += int64(n)
|
|
|
- //}
|
|
|
- //
|
|
|
- //c.Ctx.Header("Content-Length", strconv.FormatInt(ssize, 10))
|
|
|
- //_, err = c.Ctx.Writer.Write(bbuf.Bytes())
|
|
|
- //if err != nil {
|
|
|
- // syslogs.Error("输出流断开:", attach.Name, attach.Size)
|
|
|
- // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- // return
|
|
|
- //}
|
|
|
- //
|
|
|
- //syslogs.Debug("下载附件成功:", attach.Name, attach.Size)
|
|
|
- //c.Ctx.JSON(200, sysmodel.SysReturn{200, "", attach.Id})
|
|
|
}
|
|
|
|
|
|
// _Delete
|
|
|
@@ -231,39 +172,30 @@ func doUpload2(c *entitys.CtrlContext) (*sysmodel.SysAttachment, error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer file.Close()
|
|
|
- //domain := c.Ctx.GetString("domain")
|
|
|
- //fileDir := fmt.Sprintf("files/%s/", domain)
|
|
|
- fileDir := fmt.Sprintf("files/")
|
|
|
- if err = mkdir(fileDir); err != nil {
|
|
|
- syslogs.Info("文件夹创建失败")
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- attachId := sysutils.NewUUID()
|
|
|
- filePath := fileDir + attachId
|
|
|
- fW, err := os.Create(filePath)
|
|
|
- if err != nil {
|
|
|
- syslogs.Info("文件创建失败")
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- defer fW.Close()
|
|
|
- length, err := io.Copy(fW, file)
|
|
|
- if err != nil {
|
|
|
- syslogs.Info("文件保存失败")
|
|
|
- return nil, err
|
|
|
+
|
|
|
+ var attach *sysmodel.SysAttachment = nil
|
|
|
+ if _attachementUpDownloader != nil{
|
|
|
+ attach, err = _attachementUpDownloader.Upload(file, fInfo)
|
|
|
+ if err !=nil{
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return nil, errors.New("附件上传下载接口为nil")
|
|
|
}
|
|
|
+
|
|
|
userId := c.Ctx.GetString("user_id")
|
|
|
|
|
|
- attach := &sysmodel.SysAttachment{}
|
|
|
- attach.Id = attachId
|
|
|
+ if attach.Id == ""{
|
|
|
+ attach.Id = sysutils.NewUUID()
|
|
|
+ }
|
|
|
+ if attach.Size <= 0{
|
|
|
+ attach.Size = int32((fInfo.Size))
|
|
|
+ }
|
|
|
attach.Name = fInfo.Filename
|
|
|
- attach.Size = int32(length)
|
|
|
- attach.Url = "/api/v1/sys_attachment/download?id=" + attach.Id
|
|
|
attach.CreateBy = userId
|
|
|
attach.CreateTime = time.Now()
|
|
|
attach.Ext = path.Ext(attach.Name)
|
|
|
- attach.Hash = FileHasH(filePath)
|
|
|
|
|
|
- //_, err = c.Db.InsertOne(attach)
|
|
|
_, err = c.PlatformDbEngine.InsertOne(attach)
|
|
|
if err != nil {
|
|
|
syslogs.Error("保存附件出错了:", err)
|
|
|
@@ -289,6 +221,74 @@ func FileHasH(filePath string) string {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief: 默认
|
|
|
+ */
|
|
|
+type defaultAttachUpDownloader struct{
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief: 附件上传接口
|
|
|
+ * @param1 file: post 多媒体file
|
|
|
+ * @param1 header: 文件相头信息
|
|
|
+ * @return1: 附件信息,主要包括id,url, hash和大小。其他字段会自动设置
|
|
|
+ * @return2: 错误信息
|
|
|
+ */
|
|
|
+func(d *defaultAttachUpDownloader)Upload(file multipart.File, header *multipart.FileHeader)(*sysmodel.SysAttachment, error){
|
|
|
+ fileDir := fmt.Sprintf("files/")
|
|
|
+ if err := mkdir(fileDir); err != nil {
|
|
|
+ syslogs.Info("文件夹创建失败")
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ attachId := sysutils.NewUUID()
|
|
|
+ filePath := fileDir + attachId
|
|
|
+ fW, err := os.Create(filePath)
|
|
|
+ if err != nil {
|
|
|
+ syslogs.Info("文件创建失败")
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ defer fW.Close()
|
|
|
+ length, err := io.Copy(fW, file)
|
|
|
+ if err != nil {
|
|
|
+ syslogs.Info("文件保存失败")
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ attach := &sysmodel.SysAttachment{}
|
|
|
+ attach.Id = attachId
|
|
|
+ attach.Url = "/api/v1/sys_attachment/download?id=" + attach.Id
|
|
|
+ attach.Hash = ""
|
|
|
+ attach.Size = int32(length)
|
|
|
+
|
|
|
+ return attach, nil
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief: 附件下载
|
|
|
+ * @param1 ctx: 上下文
|
|
|
+ * @param2 id: 附件记录的id
|
|
|
+ */
|
|
|
+func(d *defaultAttachUpDownloader)Download(c *entitys.CtrlContext, attach *sysmodel.SysAttachment){
|
|
|
+ filePath := "files/" + attach.Id
|
|
|
+
|
|
|
+ c.Ctx.Writer.Header().Add("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
|
|
|
+ c.Ctx.Writer.Header().Add("Content-Type", mime.TypeByExtension(attach.Ext))
|
|
|
+ c.Ctx.Writer.Header().Add("Content-Length", strconv.FormatInt(int64(attach.Size), 10))
|
|
|
+ c.Ctx.Writer.Header().Add("Accept-Ranges", "bytes")
|
|
|
+
|
|
|
+ if !sysutils.Exists(filePath) {
|
|
|
+ c.Ctx.Writer.WriteHeader(400)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Disposition"))
|
|
|
+ fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Type"))
|
|
|
+ fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Length"))
|
|
|
+ fmt.Println("---->", c.Ctx.Writer.Header().Get("Accept-Ranges"))
|
|
|
+
|
|
|
+ c.Ctx.File(filePath)
|
|
|
+}
|
|
|
+
|
|
|
func __none_func_sys_attachment__(params ...interface{}) bool {
|
|
|
return true
|
|
|
}
|