From f22238066f4901370059bb6aaa94742709c4e8bd Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期一, 28 八月 2023 19:18:17 +0800 Subject: [PATCH] fix --- go.sum | 13 +----- go.mod | 3 + docs/swagger.yaml | 14 +++++++ api/v1/test/contract.go | 18 +++++++- docs/docs.go | 23 +++++++++++ docs/swagger.json | 23 +++++++++++ 6 files changed, 79 insertions(+), 15 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) diff --git a/docs/docs.go b/docs/docs.go index 1ab4138..5433565 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1938,6 +1938,11 @@ "in": "query" }, { + "type": "string", + "name": "fileType", + "in": "query" + }, + { "type": "integer", "description": "涓婚敭ID", "name": "id", @@ -1995,6 +2000,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -2060,6 +2070,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -2143,6 +2158,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -9709,6 +9729,9 @@ "fileName": { "type": "string" }, + "fileType": { + "type": "string" + }, "id": { "description": "涓婚敭ID", "type": "integer" diff --git a/docs/swagger.json b/docs/swagger.json index 4f3c6ad..03b7140 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1929,6 +1929,11 @@ "in": "query" }, { + "type": "string", + "name": "fileType", + "in": "query" + }, + { "type": "integer", "description": "涓婚敭ID", "name": "id", @@ -1986,6 +1991,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -2051,6 +2061,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -2134,6 +2149,11 @@ { "type": "string", "name": "fileName", + "in": "query" + }, + { + "type": "string", + "name": "fileType", "in": "query" }, { @@ -9700,6 +9720,9 @@ "fileName": { "type": "string" }, + "fileType": { + "type": "string" + }, "id": { "description": "涓婚敭ID", "type": "integer" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 667af04..f2e40d0 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1634,6 +1634,8 @@ type: array fileName: type: string + fileType: + type: string id: description: 涓婚敭ID type: integer @@ -2846,6 +2848,9 @@ - in: query name: fileName type: string + - in: query + name: fileType + type: string - description: 涓婚敭ID in: query name: id @@ -2882,6 +2887,9 @@ type: array - in: query name: fileName + type: string + - in: query + name: fileType type: string - description: 涓婚敭ID in: query @@ -2922,6 +2930,9 @@ type: array - in: query name: fileName + type: string + - in: query + name: fileType type: string - description: 涓婚敭ID in: query @@ -2975,6 +2986,9 @@ - in: query name: fileName type: string + - in: query + name: fileType + type: string - description: 涓婚敭ID in: query name: id diff --git a/go.mod b/go.mod index 7f42f24..8e27a53 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ github.com/gofrs/uuid/v5 v5.0.0 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/gookit/color v1.5.4 + github.com/h2non/filetype v1.1.3 github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible @@ -27,6 +28,7 @@ github.com/robfig/cron/v3 v3.0.1 github.com/sashabaranov/go-openai v1.14.1 github.com/shirou/gopsutil/v3 v3.23.6 + github.com/shopspring/decimal v1.3.1 github.com/songzhibin97/gkit v1.2.11 github.com/spf13/cast v1.5.1 github.com/spf13/viper v1.16.0 @@ -105,7 +107,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect - github.com/shopspring/decimal v1.3.1 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index 49b0577..19a090a 100644 --- a/go.sum +++ b/go.sum @@ -47,7 +47,6 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= -github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= @@ -57,7 +56,6 @@ github.com/aws/aws-sdk-go v1.44.307 h1:2R0/EPgpZcFSUwZhYImq/srjaOrOfLv5MNRzrFyAM38= github.com/aws/aws-sdk-go v1.44.307/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= @@ -161,7 +159,6 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= @@ -256,6 +253,8 @@ github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -266,16 +265,10 @@ github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible/go.mod h1:l7VUhRbTKCzdOacdT4oWCwATKyvZqUOlOqr0Ous3k4s= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= -github.com/jackc/pgconn v1.13.0/go.mod h1:AnowpAqO4CMIIJNZl2VJp+KrkAZciAkhEl0W0JIobpI= -github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= -github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgtype v1.12.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= -github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw= github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= @@ -332,7 +325,6 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ= github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= -github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= @@ -486,7 +478,6 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= -- Gitblit v1.8.0