Compare commits

..

No commits in common. "bdd4d8dc32d6e2a0e526540f2c73c302a1a668b7" and "2b11a58a2daf80c31e4c3d70d525c95a0325e9d1" have entirely different histories.

1 changed files with 3 additions and 28 deletions

View File

@ -1,11 +1,8 @@
package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"strconv"
"videoplayer/dao"
"videoplayer/proto"
@ -78,35 +75,13 @@ func DownloadFile(c *gin.Context) {
id, _ := c.Get("id")
//查询文件信息
//file := dao.FindFileByNames(file_id, int(id.(float64)))
file_ := dao.FindFileByID(file_id, int(id.(float64)))
if file_.ID == 0 {
file := dao.FindFileByID(file_id, int(id.(float64)))
if file.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return
}
//下载文件
// 打开文件
file, err := os.Open(file_.FilePath + "/" + file_.FileStoreName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to open file"})
return
}
defer file.Close()
// 设置响应头
c.Writer.Header().Set("Content-Description", "File Transfer")
c.Writer.Header().Set("Content-Transfer-Encoding", "binary")
c.Writer.Header().Set("Expires", "0")
c.Writer.Header().Set("Cache-Control", "must-revalidate")
c.Writer.Header().Set("Pragma", "public")
c.Writer.Header().Set("Content-Type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file_.FileName))
// 发送文件内容
_, err = io.Copy(c.Writer, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to send file"})
return
}
c.File(file.FilePath + file.FileStoreName)
}
func SetRedis(c *gin.Context) {