From f22238066f4901370059bb6aaa94742709c4e8bd Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期一, 28 八月 2023 19:18:17 +0800
Subject: [PATCH] fix

---
 api/v1/test/contract.go |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/api/v1/test/contract.go b/api/v1/test/contract.go
index a3411c5..c1ed00e 100644
--- a/api/v1/test/contract.go
+++ b/api/v1/test/contract.go
@@ -4,6 +4,7 @@
 	"bytes"
 	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/h2non/filetype"
 	"go.uber.org/zap"
 	"net/http"
 	"srm/global"
@@ -32,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()})
@@ -44,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 {
@@ -54,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)
 	}
 }
 
@@ -277,7 +289,7 @@
 		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", "application/pdf")
+		c.Header("Content-Type", contract.FileType)
 
 		// 灏嗘枃浠朵綔涓哄搷搴斾綋鍙戦��
 		http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader)

--
Gitblit v1.8.0