Browse Source

Merge branch 'hotfix/jinbo_test_0329' into debug

hsun 1 year ago
parent
commit
d14ce51939
1 changed files with 35 additions and 10 deletions
  1. 35 10
      controllers/resource.go

+ 35 - 10
controllers/resource.go

@@ -944,12 +944,12 @@ func (this *ResourceAuthController) FileDownload() {
 		br.Ret = 408
 		return
 	}
-	fileName := this.GetString("FileName")
-	fileName = strings.TrimSpace(fileName)
-	if fileName == "" {
-		br.Msg = "参数有误"
-		return
-	}
+	//fileName := this.GetString("FileName")
+	//fileName = strings.TrimSpace(fileName)
+	//if fileName == "" {
+	//	br.Msg = "参数有误"
+	//	return
+	//}
 	fileEncode := this.GetString("FileUrl")
 	fileEncode = strings.TrimSpace(fileEncode)
 	if fileEncode == "" {
@@ -963,6 +963,13 @@ func (this *ResourceAuthController) FileDownload() {
 		return
 	}
 	fileUrl := string(fileByte)
+	fileArr := strings.Split(fileUrl, "/")
+	if len(fileArr) == 0 {
+		br.Msg = "文件地址有误"
+		return
+	}
+	fileName := fileArr[len(fileArr)-1]
+	//fmt.Println(fileName)
 
 	// 获取文件
 	down, e := http.Get(fileUrl)
@@ -978,19 +985,37 @@ func (this *ResourceAuthController) FileDownload() {
 		return
 	}
 
+	// 生成本地文件
+	localFilePath := fmt.Sprintf("%s%s", utils.GetRandStringNoSpecialChar(6), fileName)
+	localFile, e := os.Create(localFilePath)
+	if e != nil {
+		br.Msg = "下载失败"
+		br.ErrMsg = "生成本地文件失败, Err: " + e.Error()
+		return
+	}
+	defer func() {
+		if e = localFile.Close(); e != nil {
+			fmt.Println("local file close err: ", e.Error())
+		}
+		if e = os.Remove(localFilePath); e != nil {
+			fmt.Println("local file remove err: ", e.Error())
+		}
+	}()
+
 	// 写入响应流
-	_, e = io.Copy(this.Ctx.ResponseWriter, down.Body)
+	//_, e = io.Copy(this.Ctx.ResponseWriter, down.Body)
+	_, e = io.Copy(localFile, down.Body)
 	if e != nil {
 		br.Msg = "下载失败"
 		br.ErrMsg = "复制文件资源失败, Err: " + e.Error()
 		return
 	}
-
 	// 设置响应头
-	this.Ctx.ResponseWriter.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
-	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
+	//this.Ctx.ResponseWriter.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
+	//this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
 
 	br.Ret = 200
 	br.Msg = "下载成功"
 	br.Success = true
+	this.Ctx.Output.Download(localFilePath, fileName)
 }