From bb8c6763c2c9bfccd210d998e17eaa66aa20e593 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期二, 08 八月 2023 10:49:29 +0800
Subject: [PATCH] 增加文件下载和预览接口,并统计下载和预览次数
---
service/file.go | 9
api/v1/file.go | 102 +++++++-
model/request/file.go | 7
docs/swagger.yaml | 129 ++++-------
model/file.go | 4
docs/docs.go | 186 ++++++----------
router/file.go | 4
docs/swagger.json | 186 ++++++----------
8 files changed, 293 insertions(+), 334 deletions(-)
diff --git a/api/v1/file.go b/api/v1/file.go
index 9fbdc78..348a72c 100644
--- a/api/v1/file.go
+++ b/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, ¶ms)
+// 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, ¶ms)
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, ¶ms)
+ 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)
}
diff --git a/docs/docs.go b/docs/docs.go
index 54c4026..7b8fec7 100644
--- a/docs/docs.go
+++ b/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": {
diff --git a/docs/swagger.json b/docs/swagger.json
index 3957900..04b602a 100644
--- a/docs/swagger.json
+++ b/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": {
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index f8a7c7f..0a744e3 100644
--- a/docs/swagger.yaml
+++ b/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:
diff --git a/model/file.go b/model/file.go
index bd1c46a..11225f1 100644
--- a/model/file.go
+++ b/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"` // 鏂囦欢绫诲瀷
diff --git a/model/request/file.go b/model/request/file.go
index 096675b..749b82f 100644
--- a/model/request/file.go
+++ b/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
+}
diff --git a/router/file.go b/router/file.go
index 3946e5d..a44ff23 100644
--- a/router/file.go
+++ b/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) // 闄勪欢棰勮
}
}
diff --git a/service/file.go b/service/file.go
index 584aa11..4ed8750 100644
--- a/service/file.go
+++ b/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)
--
Gitblit v1.8.0