videoplayer/handler/tool.go

132 lines
3.9 KiB
Go

package handler
import (
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/dao"
"videoplayer/proto"
"videoplayer/service"
)
type SetRedisReq struct {
Option string `json:"option" form:"option"`
Key string `json:"key" form:"key"`
Value string `json:"value" form:"value"`
Expire int `json:"expire" form:"expire"`
}
func SetUpToolGroup(router *gin.Engine) {
toolGroup := router.Group("/tool")
toolGroup.POST("/set_redis", SetRedis)
toolGroup.POST("/get_redis", GetRedis)
//文件上传、下载
toolGroup.POST("/upload", UploadFile)
toolGroup.GET("/download/:filename", DownloadFile)
}
func UploadFile(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
//从请求头获取upload_type
uploadType := c.PostForm("upload_type")
if uploadType == "" {
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
return
}
user := dao.FindUserByUserID(id1)
if user.Upload == false {
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
return
}
//上传文件
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": "upload file failed", "code": proto.UploadFileFailed, "message": "failed"})
return
}
//保存文件
filePath, fileStoreName, err := service.SaveFile(file, uploadType)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": "save file failed", "code": proto.SaveFileFailed, "message": "failed"})
return
}
//保存文件信息
fileSize := int(file.Size)
fileName := file.Filename
fileType := file.Header.Get("file_type")
fileID := dao.CreateFile(fileStoreName, fileName, fileType, filePath, fileSize, id1)
if fileID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
}
func DownloadFile(c *gin.Context) {
//参数
filename := c.Param("filename")
//file_id, _ := strconv.Atoi(c.Query("id"))
id, _ := c.Get("id")
//查询文件信息
file := dao.FindFileByNames(filename, int(id.(float64)))
if file.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return
}
//下载文件
c.File(file.FilePath + file.FileStoreName)
}
func SetRedis(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
user := dao.FindUserByUserID(id1)
if user.Redis == false {
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
return
}
//解析请求参数
var req SetRedisReq
if err := c.ShouldBind(&req); err == nil {
var code int
var message string
if req.Option == "list" {
code, message = service.SetToolRedisList(req.Key, req.Value, req.Expire)
} else if req.Option == "set" {
code, message = service.SetToolRedisSet(req.Key, req.Value, req.Expire)
} else if req.Option == "kv" {
code, message = service.SetToolRedisKV(req.Key, req.Value, req.Expire)
}
c.JSON(http.StatusOK, gin.H{"code": code, "message": message})
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
}
}
func GetRedis(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
user := dao.FindUserByUserID(id1)
if user.Redis == false {
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
return
}
//解析请求参数
var req SetRedisReq
if err := c.ShouldBind(&req); err == nil {
code, message := service.GetToolRedis(req.Key)
req.Value = message
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
}
}