Compare commits

..

3 Commits

Author SHA1 Message Date
junleea c943ced9f6 upload type 2024-08-30 15:26:39 +08:00
junleea db726b078e upload type 2024-08-30 15:13:23 +08:00
junleea 0b503558ef upload type 2024-08-30 15:03:33 +08:00
2 changed files with 13 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"strconv"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
"videoplayer/service" "videoplayer/service"
@ -22,7 +23,7 @@ func SetUpToolGroup(router *gin.Engine) {
//文件上传、下载 //文件上传、下载
toolGroup.POST("/upload", UploadFile) toolGroup.POST("/upload", UploadFile)
toolGroup.GET("/download/:filename", DownloadFile) toolGroup.GET("/download", DownloadFile)
} }
func UploadFile(c *gin.Context) { func UploadFile(c *gin.Context) {
@ -30,7 +31,7 @@ func UploadFile(c *gin.Context) {
id, _ := c.Get("id") id, _ := c.Get("id")
id1 := int(id.(float64)) id1 := int(id.(float64))
//从请求头获取upload_type //从请求头获取upload_type
uploadType := c.Request.Header.Get("upload_type") uploadType := c.PostForm("upload_type")
if uploadType == "" { if uploadType == "" {
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
return return
@ -63,16 +64,18 @@ func UploadFile(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return return
} }
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
} }
func DownloadFile(c *gin.Context) { func DownloadFile(c *gin.Context) {
//参数 //参数
filename := c.Param("filename") //filename := c.Param("filename")
//file_id, _ := strconv.Atoi(c.Query("id")) file_id, _ := strconv.Atoi(c.Query("id"))
id, _ := c.Get("id") id, _ := c.Get("id")
//查询文件信息 //查询文件信息
file := dao.FindFileByNames(filename, int(id.(float64))) //file := dao.FindFileByNames(file_id, int(id.(float64)))
file := dao.FindFileByID(file_id, int(id.(float64)))
if file.ID == 0 { if file.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return return

View File

@ -50,10 +50,11 @@ func writeLogger(c *gin.Context) {
method := c.Request.Method method := c.Request.Method
path := c.Request.URL.Path path := c.Request.URL.Path
params := "" params := ""
if method == "GET" { if method == "GET" {
params = c.Request.URL.RawQuery params = c.Request.URL.RawQuery
} }
if method == "POST" { if method == "POST" && !strings.Contains(c.Request.URL.Path, "/upload") {
params = c.Request.PostForm.Encode() params = c.Request.PostForm.Encode()
if params == "" { if params == "" {
// 请求体 // 请求体
@ -62,6 +63,9 @@ func writeLogger(c *gin.Context) {
params = string(bodyBytes) params = string(bodyBytes)
} }
} }
if strings.Contains(c.Request.URL.Path, "/upload") {
params = "upload file"
}
go dao.InsertLogToDB(path, ip, method, params) go dao.InsertLogToDB(path, ip, method, params)
} }