zhangqian
2023-08-08 bb8c6763c2c9bfccd210d998e17eaa66aa20e593
增加文件下载和预览接口,并统计下载和预览次数
8个文件已修改
627 ■■■■ 已修改文件
api/v1/file.go 102 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 186 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 186 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 129 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/file.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/file.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/file.go 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/file.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/file.go
@@ -3,7 +3,6 @@
import (
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/httpx"
@@ -11,6 +10,7 @@
    "aps_crm/utils/upload"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "io"
    "os"
    "path/filepath"
)
@@ -131,28 +131,100 @@
//    ctx.Ok()
//}
// List
// @Tags        附件管理
// @Summary    获取附件列表
//// List
//// @Tags        附件管理
//// @Summary    获取附件列表
//// @Produce    application/json
//// @Param        object    query        request.GetFileList    true    "参数"
//// @Success    200    {object}    response.ListResponse{data=[]model.File}
//// @Router        /api/file/list [get]
//func (s *FileApi) List(c *gin.Context) {
//    var params request.GetFileList
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    file, total, errCode := service.NewFileService().GetFileList()
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//
//    ctx.OkWithDetailed(response.ListResponse{
//        Data:  file,
//        Count: total,
//    })
//}
// Download
// @Tags    附件管理
// @Summary    附件下载
// @Produce    application/json
// @Param        object    query        request.GetFileList    true    "参数"
// @Success    200    {object}    response.ListResponse{data=[]model.File}
// @Router        /api/file/list [get]
func (s *FileApi) List(c *gin.Context) {
    var params request.GetFileList
// @Param        object    body        request.DownloadFile    true    "参数"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/download [post]
func (s *FileApi) Download(c *gin.Context) {
    var params request.DownloadFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    file, total, errCode := service.NewFileService().GetFileList()
    file, errCode := service.NewFileService().GetFile(params.Id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    f, err := os.Open(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    file.DownloadCount++
    service.NewFileService().UpdateFile(file)
    ctx.OkWithDetailed(response.ListResponse{
        Data:  file,
        Count: total,
    })
    data, err := io.ReadAll(f)
    c.Writer.Header().Set("Content-Type", "application/octect-stream")
    c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name)
    c.Writer.Write(data)
}
// Preview
// @Tags    附件管理
// @Summary    附件预览
// @Produce    application/json
// @Param        object    body        request.DownloadFile    true    "参数"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/preview [post]
func (s *FileApi) Preview(c *gin.Context) {
    var params request.DownloadFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    file, errCode := service.NewFileService().GetFile(params.Id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    f, err := os.Open(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    file.PreviewCount++
    service.NewFileService().UpdateFile(file)
    data, err := io.ReadAll(f)
    c.Writer.Header().Set("Content-Type", "application/octect-stream")
    c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name)
    c.Writer.Write(data)
}
docs/docs.go
@@ -2727,76 +2727,61 @@
                }
            }
        },
        "/api/file/list": {
            "get": {
        "/api/file/download": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "附件管理"
                ],
                "summary": "获取附件列表",
                "summary": "附件下载",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "FileKeywordCustomerName"
                        ],
                        "name": "keywordType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "FileQueryClassExpireLessThen60Days"
                        ],
                        "name": "queryClass",
                        "in": "query"
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DownloadFile"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/response.ListResponse"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/model.File"
                                            }
                                        }
                                    }
                                }
                            ]
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/file/preview": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "附件管理"
                ],
                "summary": "附件预览",
                "parameters": [
                    {
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DownloadFile"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
@@ -9440,24 +9425,6 @@
                "FaqQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.FileKeywordType": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "FileKeywordCustomerName"
            ]
        },
        "constvar.FileQueryClass": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "FileQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.InvoiceKeywordType": {
            "type": "string",
            "enum": [
@@ -10236,50 +10203,6 @@
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "model.File": {
            "type": "object",
            "properties": {
                "bucket": {
                    "description": "对象存储bucket",
                    "type": "string"
                },
                "downloadCount": {
                    "description": "下次次数",
                    "type": "integer"
                },
                "filePath": {
                    "description": "文件路径",
                    "type": "string"
                },
                "fileType": {
                    "description": "文件类型",
                    "type": "string"
                },
                "key": {
                    "description": "对象存储key",
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "previewCount": {
                    "description": "预览次数",
                    "type": "integer"
                },
                "size": {
                    "description": "文件大小",
                    "type": "integer"
                },
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
@@ -13480,6 +13403,33 @@
                }
            }
        },
        "request.DownloadFile": {
            "type": "object",
            "required": [
                "id",
                "key",
                "sourceId",
                "sourceType"
            ],
            "properties": {
                "id": {
                    "description": "附件id",
                    "type": "integer"
                },
                "key": {
                    "description": "附件存储key",
                    "type": "string"
                },
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
        },
        "request.FollowRecord": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -2715,76 +2715,61 @@
                }
            }
        },
        "/api/file/list": {
            "get": {
        "/api/file/download": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "附件管理"
                ],
                "summary": "获取附件列表",
                "summary": "附件下载",
                "parameters": [
                    {
                        "type": "string",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "FileKeywordCustomerName"
                        ],
                        "name": "keywordType",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    },
                    {
                        "enum": [
                            ""
                        ],
                        "type": "string",
                        "x-enum-varnames": [
                            "FileQueryClassExpireLessThen60Days"
                        ],
                        "name": "queryClass",
                        "in": "query"
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DownloadFile"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/response.ListResponse"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/model.File"
                                            }
                                        }
                                    }
                                }
                            ]
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
            }
        },
        "/api/file/preview": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "附件管理"
                ],
                "summary": "附件预览",
                "parameters": [
                    {
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DownloadFile"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/contextx.Response"
                        }
                    }
                }
@@ -9428,24 +9413,6 @@
                "FaqQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.FileKeywordType": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "FileKeywordCustomerName"
            ]
        },
        "constvar.FileQueryClass": {
            "type": "string",
            "enum": [
                ""
            ],
            "x-enum-varnames": [
                "FileQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.InvoiceKeywordType": {
            "type": "string",
            "enum": [
@@ -10224,50 +10191,6 @@
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "model.File": {
            "type": "object",
            "properties": {
                "bucket": {
                    "description": "对象存储bucket",
                    "type": "string"
                },
                "downloadCount": {
                    "description": "下次次数",
                    "type": "integer"
                },
                "filePath": {
                    "description": "文件路径",
                    "type": "string"
                },
                "fileType": {
                    "description": "文件类型",
                    "type": "string"
                },
                "key": {
                    "description": "对象存储key",
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "previewCount": {
                    "description": "预览次数",
                    "type": "integer"
                },
                "size": {
                    "description": "文件大小",
                    "type": "integer"
                },
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
@@ -13468,6 +13391,33 @@
                }
            }
        },
        "request.DownloadFile": {
            "type": "object",
            "required": [
                "id",
                "key",
                "sourceId",
                "sourceType"
            ],
            "properties": {
                "id": {
                    "description": "附件id",
                    "type": "integer"
                },
                "key": {
                    "description": "附件存储key",
                    "type": "string"
                },
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
        },
        "request.FollowRecord": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -46,18 +46,6 @@
    type: string
    x-enum-varnames:
    - FaqQueryClassExpireLessThen60Days
  constvar.FileKeywordType:
    enum:
    - ""
    type: string
    x-enum-varnames:
    - FileKeywordCustomerName
  constvar.FileQueryClass:
    enum:
    - ""
    type: string
    x-enum-varnames:
    - FileQueryClassExpireLessThen60Days
  constvar.InvoiceKeywordType:
    enum:
    - ""
@@ -599,38 +587,6 @@
      id:
        type: integer
      name:
        type: string
    type: object
  model.File:
    properties:
      bucket:
        description: 对象存储bucket
        type: string
      downloadCount:
        description: 下次次数
        type: integer
      filePath:
        description: 文件路径
        type: string
      fileType:
        description: 文件类型
        type: string
      key:
        description: 对象存储key
        type: string
      name:
        type: string
      previewCount:
        description: 预览次数
        type: integer
      size:
        description: 文件大小
        type: integer
      sourceId:
        description: 来源id
        type: integer
      sourceType:
        description: 附件来源
        type: string
    type: object
  model.FollowRecord:
@@ -2784,6 +2740,26 @@
        items:
          type: integer
        type: array
    type: object
  request.DownloadFile:
    properties:
      id:
        description: 附件id
        type: integer
      key:
        description: 附件存储key
        type: string
      sourceId:
        description: 来源id
        type: integer
      sourceType:
        description: 附件来源
        type: string
    required:
    - id
    - key
    - sourceId
    - sourceType
    type: object
  request.FollowRecord:
    properties:
@@ -6938,49 +6914,42 @@
      summary: 删除附件
      tags:
      - 附件管理
  /api/file/list:
    get:
  /api/file/download:
    post:
      parameters:
      - in: query
        name: keyword
        type: string
      - enum:
        - ""
        in: query
        name: keywordType
        type: string
        x-enum-varnames:
        - FileKeywordCustomerName
      - description: 页码
        in: query
        name: page
        type: integer
      - description: 每页大小
        in: query
        name: pageSize
        type: integer
      - enum:
        - ""
        in: query
        name: queryClass
        type: string
        x-enum-varnames:
        - FileQueryClassExpireLessThen60Days
      - description: 参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.DownloadFile'
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            allOf:
            - $ref: '#/definitions/response.ListResponse'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/model.File'
                  type: array
              type: object
      summary: 获取附件列表
            $ref: '#/definitions/contextx.Response'
      summary: 附件下载
      tags:
      - 附件管理
  /api/file/preview:
    post:
      parameters:
      - description: 参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.DownloadFile'
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            $ref: '#/definitions/contextx.Response'
      summary: 附件预览
      tags:
      - 附件管理
  /api/followRecord/add:
model/file.go
@@ -13,9 +13,9 @@
    File struct {
        Name          string `gorm:"name" json:"name"`
        Size          int64  `gorm:"size" json:"size"`                    // 文件大小
        FilePath      string `gorm:"file_path" json:"filePath"`           // 文件路径
        FilePath      string `gorm:"file_path" json:"-"`                  // 文件路径
        Key           string `gorm:"key" json:"key"`                      // 对象存储key
        Bucket        string `gorm:"bucket" json:"bucket"`                // 对象存储bucket
        Bucket        string `gorm:"bucket" json:"-"`                     // 对象存储bucket
        DownloadCount int    `gorm:"download_count" json:"downloadCount"` // 下次次数
        PreviewCount  int    `gorm:"preview_count" json:"previewCount"`   // 预览次数
        FileType      string `gorm:"file_type" json:"fileType"`           // 文件类型
model/request/file.go
@@ -21,3 +21,10 @@
    KeywordType constvar.FileKeywordType `json:"keywordType"  form:"keywordType"`
    Keyword     string                   `json:"keyword" form:"keyword"`
}
type DownloadFile struct {
    SourceType string `json:"sourceType" form:"sourceType" binding:"required"` // 附件来源
    SourceId   int    `json:"sourceId" form:"sourceId"  binding:"required"`    // 来源id
    Id         uint   `json:"id" form:"id"  binding:"required"`                // 附件id
    Key        string `json:"key" form:"key"  binding:"required"`              // 附件存储key
}
router/file.go
@@ -11,6 +11,8 @@
    {
        FileRouter.POST("add", FileApi.Add)             // 添加附件
        FileRouter.DELETE("delete/:id", FileApi.Delete) // 删除附件
        FileRouter.GET("list", FileApi.List)            // 附件列表
        //FileRouter.GET("list", FileApi.List)            // 附件列表
        FileRouter.POST("download", FileApi.Download) // 附件下载
        FileRouter.POST("preview", FileApi.Preview)   // 附件预览
    }
}
service/file.go
@@ -12,6 +12,15 @@
    return FileService{}
}
func (FileService) GetFile(id uint) (*model.File, int) {
    file, err := model.NewFileSearch().SetId(id).First()
    if err != nil {
        return nil, ecode.DBErr
    }
    return file, ecode.OK
}
func (FileService) AddFile(fileRecord *model.File) int {
    err := model.NewFileSearch().Create(fileRecord)