Преглед на файлове

download file by attach model

yinbangzhong преди 2 години
родител
ревизия
dd2c694c12
променени са 6 файла, в които са добавени 129 реда и са изтрити 2 реда
  1. 24 2
      controllers/audio.go
  2. 39 0
      docs/docs.go
  3. 39 0
      docs/swagger.json
  4. 22 0
      docs/swagger.yaml
  5. 4 0
      response/audio.go
  6. 1 0
      router/router.go

+ 24 - 2
controllers/audio.go

@@ -111,6 +111,9 @@ func (slf AudioCtl) Upload(c *gin.Context) {
 	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 @@ func (slf AudioCtl) AudioDownload(c *gin.Context) {
 	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为txt文件类型,避免中文乱码
 		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 @@ func (slf AudioCtl) AudioDownload(c *gin.Context) {
 		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 @@ func (slf AudioCtl) Follow(c *gin.Context) {
 
 	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)
+}

+ 39 - 0
docs/docs.go

@@ -360,6 +360,37 @@ const docTemplate = `{
                 }
             }
         },
+        "/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 @@ const docTemplate = `{
                 }
             }
         },
+        "response.PreLoadPathResp": {
+            "type": "object",
+            "properties": {
+                "preLoadPath": {
+                    "type": "string"
+                }
+            }
+        },
         "util.Response": {
             "type": "object",
             "properties": {

+ 39 - 0
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": {

+ 22 - 0
docs/swagger.yaml

@@ -192,6 +192,11 @@ definitions:
         - $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 @@ paths:
       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:

+ 4 - 0
response/audio.go

@@ -5,3 +5,7 @@ import "speechAnalysis/constvar"
 type FollowResp struct {
 	FollowStatus constvar.BoolType `json:"followStatus"` //1 已关注 2未关注
 }
+
+type PreLoadPathResp struct {
+	PreLoadPath string `json:"preLoadPath"`
+}

+ 1 - 0
router/router.go

@@ -35,6 +35,7 @@ func NewRouter() *gin.Engine {
 		audioAPi.DELETE("delete", audioCtl.Delete)            // 音频删除
 		audioAPi.DELETE("batchDelete", audioCtl.BatchDelete)  // 音频批量删除
 		audioAPi.POST("follow", audioCtl.Follow)              // 关注/取消关注
+		audioAPi.GET("preLoadPath", audioCtl.PreLoadPath)     // 获取自动加载路径
 
 	}