From dd2c694c124c4a0f5b1d9c5a876337bd424d697f Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期三, 19 六月 2024 17:46:08 +0800 Subject: [PATCH] download file by attach model --- docs/swagger.yaml | 22 +++++++ controllers/audio.go | 26 ++++++++ docs/docs.go | 39 +++++++++++++ docs/swagger.json | 39 +++++++++++++ response/audio.go | 4 + router/router.go | 1 6 files changed, 129 insertions(+), 2 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 8f5f108..9ce7cba 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -111,6 +111,9 @@ if err := models.NewAudioSearch().Create(audio); err != nil { util.ResponseFormat(c, code.SaveFail, "涓婁紶澶辫触") return + } else { + util.ResponseFormat(c, code.SaveFail, "涓婁紶鎴愬姛") + return } go func() { var trainInfoNames = []string{audio.LocomotiveNumber, audio.TrainNumber, audio.Station} @@ -290,13 +293,17 @@ filepath := "" if params.Filetype == 1 { filepath = audio.FilePath - c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 + c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷 + c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� } if params.Filetype == 2 { filepath = audio.TxtFilePath //璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷,閬垮厤涓枃涔辩爜 c.Header("Content-Type", "text/plain; charset=utf-8") c.Header("Content-Transfer-Encoding", "binary") + //鍘绘帀audio.Name涓�.涔嬪悗鐨勫唴瀹癸紝鍙繚鐣欐枃浠跺悕绉� + fileName := strings.Split(audio.Name, ".")[0] + c.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fmt.Sprintf("%s.txt", fileName))) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� } if filepath == "" { util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触") @@ -316,7 +323,6 @@ return } - c.Header("Content-Disposition", "inline; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� c.Header("Content-Length", fmt.Sprint(fileInfo.Size())) if _, err := io.Copy(c.Writer, file); err != nil { @@ -470,3 +476,19 @@ util.ResponseFormat(c, code.UpdateSuccess, resp) } + +// PreLoadPath +// @Tags 闊抽鑷姩鍔犺浇璺緞 +// @Summary 闊抽鑷姩鍔犺浇璺緞 +// @Produce application/json +// @Success 200 {object} util.Response{data=response.PreLoadPathResp} "鎴愬姛" +// @Router /api-sa/v1/audio/preLoadPath [get] +func (slf AudioCtl) PreLoadPath(c *gin.Context) { + //鑾峰彇PRELOAD_PATH鐜鍙橀噺 + preLoadPath := os.Getenv("PRELOAD_PATH") + if len(preLoadPath) == 0 { + preLoadPath = "./preloads" + } + resp := response.PreLoadPathResp{PreLoadPath: preLoadPath} + util.ResponseFormat(c, code.UpdateSuccess, resp) +} diff --git a/docs/docs.go b/docs/docs.go index d60e36c..41fc6df 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -360,6 +360,37 @@ } } }, + "/api-sa/v1/audio/preLoadPath": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "闊抽鑷姩鍔犺浇璺緞" + ], + "summary": "闊抽鑷姩鍔犺浇璺緞", + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.PreLoadPathResp" + } + } + } + ] + } + } + } + } + }, "/api-sa/v1/audio/process": { "post": { "produces": [ @@ -927,6 +958,14 @@ } } }, + "response.PreLoadPathResp": { + "type": "object", + "properties": { + "preLoadPath": { + "type": "string" + } + } + }, "util.Response": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 9f1dd40..e64e250 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -349,6 +349,37 @@ } } }, + "/api-sa/v1/audio/preLoadPath": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "闊抽鑷姩鍔犺浇璺緞" + ], + "summary": "闊抽鑷姩鍔犺浇璺緞", + "responses": { + "200": { + "description": "鎴愬姛", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/util.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.PreLoadPathResp" + } + } + } + ] + } + } + } + } + }, "/api-sa/v1/audio/process": { "post": { "produces": [ @@ -916,6 +947,14 @@ } } }, + "response.PreLoadPathResp": { + "type": "object", + "properties": { + "preLoadPath": { + "type": "string" + } + } + }, "util.Response": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 7bce983..791bf30 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -192,6 +192,11 @@ - $ref: '#/definitions/constvar.BoolType' description: 1 宸插叧娉� 2鏈叧娉� type: object + response.PreLoadPathResp: + properties: + preLoadPath: + type: string + type: object util.Response: properties: code: @@ -430,6 +435,23 @@ summary: 闊抽鍒嗘瀽妫�绱� tags: - 闊抽 + /api-sa/v1/audio/preLoadPath: + get: + produces: + - application/json + responses: + "200": + description: 鎴愬姛 + schema: + allOf: + - $ref: '#/definitions/util.Response' + - properties: + data: + $ref: '#/definitions/response.PreLoadPathResp' + type: object + summary: 闊抽鑷姩鍔犺浇璺緞 + tags: + - 闊抽鑷姩鍔犺浇璺緞 /api-sa/v1/audio/process: post: parameters: diff --git a/response/audio.go b/response/audio.go index 23f45c9..faa3f6f 100644 --- a/response/audio.go +++ b/response/audio.go @@ -5,3 +5,7 @@ type FollowResp struct { FollowStatus constvar.BoolType `json:"followStatus"` //1 宸插叧娉� 2鏈叧娉� } + +type PreLoadPathResp struct { + PreLoadPath string `json:"preLoadPath"` +} diff --git a/router/router.go b/router/router.go index 4cc3f67..0b32b31 100644 --- a/router/router.go +++ b/router/router.go @@ -35,6 +35,7 @@ audioAPi.DELETE("delete", audioCtl.Delete) // 闊抽鍒犻櫎 audioAPi.DELETE("batchDelete", audioCtl.BatchDelete) // 闊抽鎵归噺鍒犻櫎 audioAPi.POST("follow", audioCtl.Follow) // 鍏虫敞/鍙栨秷鍏虫敞 + audioAPi.GET("preLoadPath", audioCtl.PreLoadPath) // 鑾峰彇鑷姩鍔犺浇璺緞 } -- Gitblit v1.8.0