|
@@ -0,0 +1,165 @@
|
|
|
|
|
+package controllers
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "bytes"
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
|
|
+ "io"
|
|
|
|
|
+ "mime"
|
|
|
|
|
+ "strconv"
|
|
|
|
|
+ "strings"
|
|
|
|
|
+
|
|
|
|
|
+ sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
|
|
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
|
|
|
|
|
+ "log"
|
|
|
|
|
+ "path"
|
|
|
|
|
+ "time"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// _Upload
|
|
|
|
|
+// @Title _Upload
|
|
|
|
|
+// @Description 上传文件
|
|
|
|
|
+// @Success 200 {object} models.Account
|
|
|
|
|
+// @Failure 403 :id is empty
|
|
|
|
|
+func SysAttachment_Upload(c *SysAttachmentController) {
|
|
|
|
|
+
|
|
|
|
|
+ // 单文件
|
|
|
|
|
+ file, _ := c.Ctx.FormFile("file")
|
|
|
|
|
+ f, _ := file.Open()
|
|
|
|
|
+ log.Println(file.Filename)
|
|
|
|
|
+
|
|
|
|
|
+ download, hash, length, err := utils.PostFile(f)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logs.Error("PostFile err: ", err.Error())
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ attach := &sysmodel.SysAttachment{}
|
|
|
|
|
+ attach.Id = utils.NewUUID()
|
|
|
|
|
+ attach.Name = file.Filename
|
|
|
|
|
+ attach.Size = length
|
|
|
|
|
+ attach.Url = download
|
|
|
|
|
+ attach.CreateTime = time.Now()
|
|
|
|
|
+ attach.Ext = path.Ext(attach.Name)
|
|
|
|
|
+ attach.Hash = hash
|
|
|
|
|
+
|
|
|
|
|
+ //插入数据库
|
|
|
|
|
+ _, err = c.Db.Insert(attach)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logs.Error("Insert Attachment err: ", err.Error())
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ret := __none_func_sys_attachment__()
|
|
|
|
|
+ if ret {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{200, "", attach.Id})
|
|
|
|
|
+ } else {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, "", nil})
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// _Download
|
|
|
|
|
+// @Title _Download
|
|
|
|
|
+// @Description 下载文件
|
|
|
|
|
+// @Param id false "文件ID"
|
|
|
|
|
+// @Success 200 {object} models.Account
|
|
|
|
|
+// @Failure 403 :id is empty
|
|
|
|
|
+func SysAttachment_Download(c *SysAttachmentController) {
|
|
|
|
|
+ gt := c.Ctx.GetString("get_thumb")
|
|
|
|
|
+ attrId := c.Ctx.Param("attrId")
|
|
|
|
|
+ fmt.Println("--------", attrId)
|
|
|
|
|
+ var attach sysmodel.SysAttachment
|
|
|
|
|
+ _, err := c.Db.Id(attrId).Get(&attach)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logs.Error("获取附件出错了:", err)
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logs.Debug("开始下载附件:", attach.Name, attach.Size)
|
|
|
|
|
+
|
|
|
|
|
+ durl := attach.Url
|
|
|
|
|
+ if gt != "" {
|
|
|
|
|
+ durl += "?getthumb=" + gt
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ resultBody, err := utils.GetFile(durl)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logs.Error("下载附件出错了:", err)
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ defer resultBody.Close()
|
|
|
|
|
+
|
|
|
|
|
+ bbuf := bytes.NewBuffer([]byte{})
|
|
|
|
|
+
|
|
|
|
|
+ c.Ctx.Header("Content-Disposition", "attachment;filename=\""+utils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
|
|
|
|
|
+ c.Ctx.Header("Content-Type", strings.Replace(mime.TypeByExtension(attach.Ext), "charset=utf-8", "", -1))
|
|
|
|
|
+
|
|
|
|
|
+ buff := make([]byte, 1024)
|
|
|
|
|
+ var ssize int64 = 0
|
|
|
|
|
+
|
|
|
|
|
+ for {
|
|
|
|
|
+ n, err := io.ReadFull(resultBody, buff)
|
|
|
|
|
+ if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
|
|
|
|
|
+ logs.Error("下载附件出错了:", err)
|
|
|
|
|
+ c.Ctx.JSON(200, 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 {
|
|
|
|
|
+ logs.Error("输出流断开:", attach.Name, attach.Size)
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logs.Debug("下载附件成功:", attach.Name, attach.Size)
|
|
|
|
|
+
|
|
|
|
|
+ ret := __none_func_sys_attachment__(attrId)
|
|
|
|
|
+
|
|
|
|
|
+ if ret {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{200, "", nil})
|
|
|
|
|
+ } else {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, "", nil})
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// _Delete
|
|
|
|
|
+// @Title _Delete
|
|
|
|
|
+// @Description 删除文件
|
|
|
|
|
+// @Param id false "文件ID"
|
|
|
|
|
+// @Success 200 {object} models.Account
|
|
|
|
|
+// @Failure 403 :id is empty
|
|
|
|
|
+func SysAttachment_Delete(c *SysAttachmentController) {
|
|
|
|
|
+ attrId := c.Ctx.Param("attrId")
|
|
|
|
|
+ var attach sysmodel.SysAttachment
|
|
|
|
|
+ _, err := c.Db.ID(attrId).Delete(&attach)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ret := __none_func_sys_attachment__(attrId)
|
|
|
|
|
+ if ret {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{200, "", nil})
|
|
|
|
|
+ } else {
|
|
|
|
|
+ c.Ctx.JSON(200, sysmodel.SysReturn{500, "", nil})
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func __none_func_sys_attachment__(params ...interface{}) bool {
|
|
|
|
|
+ return true
|
|
|
|
|
+}
|