From 88324f3d47dab99da5b4bd9422e50b63b97c242c Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期三, 30 八月 2023 19:51:00 +0800
Subject: [PATCH] fix
---
api/v1/test/contract.go | 79 ++++++++++++++++++++++++++++++++++++++-
1 files changed, 76 insertions(+), 3 deletions(-)
diff --git a/api/v1/test/contract.go b/api/v1/test/contract.go
index 02b469c..6ae88f9 100644
--- a/api/v1/test/contract.go
+++ b/api/v1/test/contract.go
@@ -1,8 +1,12 @@
package test
import (
+ "bytes"
+ "fmt"
"github.com/gin-gonic/gin"
+ "github.com/h2non/filetype"
"go.uber.org/zap"
+ "net/http"
"srm/global"
"srm/model/common/request"
"srm/model/common/response"
@@ -10,6 +14,7 @@
testReq "srm/model/test/request"
"srm/service"
"strconv"
+ "time"
)
type ContractApi struct {
@@ -28,7 +33,7 @@
// @Success 200 {string} string "{"success":true,"data":{},"msg":"鑾峰彇鎴愬姛"}"
// @Router /con/createContract [post]
func (conApi *ContractApi) CreateContract(c *gin.Context) {
- name := c.Param("name")
+ name := c.PostForm("name")
file, err := c.FormFile("file")
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
@@ -40,9 +45,20 @@
defer f.Close()
f.Read(fileContent)
+ // 閲嶇疆鏂囦欢鎸囬拡
+ f.Seek(0, 0)
+
+ // 浣跨敤 filetype 搴撴娴嬫枃浠剁被鍨�
+ kind, _ := filetype.Match(fileContent)
+ if kind == filetype.Unknown {
+ c.JSON(http.StatusBadRequest, gin.H{"error": "Unknown file type"})
+ return
+ }
+
contract := test.Contract{
FileName: name,
FileContent: fileContent,
+ FileType: kind.MIME.Value,
}
if err, id := conService.CreateContract(&contract); err != nil {
@@ -50,7 +66,7 @@
response.FailWithMessage("鍒涘缓澶辫触", c)
} else {
//response.OkWithMessage("鍒涘缓鎴愬姛", c)
- response.OkWithData(gin.H{"id": id}, c)
+ response.OkWithData(gin.H{"id": id, "name": name}, c)
}
}
@@ -220,6 +236,63 @@
//c.Writer.Header().Set("Content-Type", "application/octect-stream")
//c.Writer.Header().Set("Content-Disposition", "attachment;filename="+contract.FileName)
//c.Writer.Write(contract.FileContent)
- c.Data(200, "application/octect-stream", contract.FileContent)
+ //c.Data(200, "application/octect-stream", contract.FileContent)
+ //reader := bytes.NewReader(contract.FileContent)
+ //c.DataFromReader(http.StatusOK, int64(len(contract.FileContent)), "application/pdf", reader, map[string]string{"Content-Disposition": fmt.Sprintf("attachment; filename=%s", contract.FileName)})
+ reader := bytes.NewReader(contract.FileContent)
+ c.Header("Content-Type", contract.FileType)
+ c.Writer.Header().Set("Content-Disposition", "inline; filename="+contract.FileName)
+ http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader)
+ }
+}
+
+// DownloadContract 涓嬭浇Contract
+// @Tags Contract
+// @Summary 涓嬭浇Contract
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data query test.Contract true "鐢╥d鏌ヨContract"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"涓嬭浇鎴愬姛"}"
+// @Router /con/downloadContract [get]
+func (conApi *ContractApi) DownloadContract(c *gin.Context) {
+ var con test.Contract
+ err := c.ShouldBindQuery(&con)
+ if err != nil {
+ response.FailWithMessage(err.Error(), c)
+ return
+ }
+
+ id := c.Query("id")
+ if id == "" {
+ response.FailWithMessage("id涓嶈兘涓虹┖", c)
+ return
+ }
+
+ val64, err := strconv.ParseUint(id, 10, 64)
+ if err != nil {
+ response.FailWithMessage("id鏍煎紡閿欒", c)
+ return
+ }
+
+ // Convert uint64 to uint
+ conId := uint(val64)
+
+ contract, err := conService.GetContract(conId)
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触", c)
+ return
+ } else {
+ reader := bytes.NewReader(contract.FileContent)
+ // 璁剧疆蹇呰鐨勫ご淇℃伅
+ c.Header("Content-Description", "File Transfer")
+ c.Header("Content-Transfer-Encoding", "binary")
+ c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", contract.FileName))
+ c.Header("Content-Type", contract.FileType)
+
+ // 灏嗘枃浠朵綔涓哄搷搴斾綋鍙戦��
+ http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader)
+ c.File(contract.FileName)
}
}
--
Gitblit v1.8.0