|
|
@@ -1,27 +1,22 @@
|
|
|
package partial
|
|
|
|
|
|
import (
|
|
|
+ "crypto/sha1"
|
|
|
+ "fmt"
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
+ syslogs "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
|
|
|
sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
|
|
|
- syslogs "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
- "crypto/sha1"
|
|
|
+ "io"
|
|
|
"io/ioutil"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
- "errors"
|
|
|
- "bytes"
|
|
|
+ "os"
|
|
|
"path"
|
|
|
- "mime"
|
|
|
"time"
|
|
|
- "fmt"
|
|
|
- "io"
|
|
|
- "os"
|
|
|
)
|
|
|
|
|
|
// _Upload
|
|
|
// @Title _Upload
|
|
|
-// @Description 上传文件
|
|
|
+// @Description 上传文件
|
|
|
// @Success 200 {object} Account
|
|
|
// @Failure 403 :id is empty
|
|
|
func SysAttachment_Upload(c *entitys.CtrlContext) {
|
|
|
@@ -36,83 +31,99 @@ func SysAttachment_Upload(c *entitys.CtrlContext) {
|
|
|
|
|
|
// _Download
|
|
|
// @Title _Download
|
|
|
-// @Description 下载文件
|
|
|
-// @Param id string false "文件ID"
|
|
|
+// @Description 下载文件
|
|
|
+// @Param id string false "文件ID"
|
|
|
// @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")
|
|
|
- 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, attach.Id)
|
|
|
- file, err := os.Open(filePath)
|
|
|
- if err != nil {
|
|
|
- c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
|
|
|
- return
|
|
|
- }
|
|
|
- defer file.Close()
|
|
|
+ filePath := fmt.Sprintf("files/%s", attrId)
|
|
|
|
|
|
- /*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))
|
|
|
-
|
|
|
- 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-Type", "image/png")
|
|
|
+ c.Ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", attrId))
|
|
|
|
|
|
- c.Ctx.Header("Content-Length", strconv.FormatInt(ssize, 10))
|
|
|
- _, err = c.Ctx.Writer.Write(bbuf.Bytes())
|
|
|
+ file, err := ioutil.ReadFile(filePath)
|
|
|
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})
|
|
|
+ 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
|
|
|
// @Title _Delete
|
|
|
-// @Description 删除文件
|
|
|
-// @Param id string false "文件ID"
|
|
|
+// @Description 删除文件
|
|
|
+// @Param id string false "文件ID"
|
|
|
// @Success 200 {object} Account
|
|
|
// @Failure 403 :id is empty
|
|
|
func SysAttachment_Delete(c *entitys.CtrlContext) {
|
|
|
@@ -167,8 +178,9 @@ 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)
|
|
|
+ //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
|
|
|
@@ -198,7 +210,8 @@ func doUpload2(c *entitys.CtrlContext) (*sysmodel.SysAttachment, error) {
|
|
|
attach.Ext = path.Ext(attach.Name)
|
|
|
attach.Hash = FileHasH(filePath)
|
|
|
|
|
|
- _, err = c.Db.InsertOne(attach)
|
|
|
+ //_, err = c.Db.InsertOne(attach)
|
|
|
+ _, err = c.PlatformDbEngine.InsertOne(attach)
|
|
|
if err != nil {
|
|
|
syslogs.Error("保存附件出错了:", err)
|
|
|
return nil, err
|
|
|
@@ -223,6 +236,6 @@ func FileHasH(filePath string) string {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
-func __none_func_sys_attachment__(params ... interface{}) bool {
|
|
|
+func __none_func_sys_attachment__(params ...interface{}) bool {
|
|
|
return true
|
|
|
}
|