videoplayer/handler/file.go

195 lines
6.4 KiB
Go

package handler
import (
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/dao"
"videoplayer/proto"
"videoplayer/service"
)
func SetUpFileGroup(router *gin.Engine) {
fileGroup := router.Group("/file")
fileGroup.POST("/config_add", AddConfigFile)
fileGroup.POST("/config_delete", DeleteConfigFile)
fileGroup.POST("/config_update", UpdateConfigFile)
fileGroup.POST("/config_search", SearchConfigFile)
fileGroup.POST("/upload", UploadFileV2)
fileGroup.GET("/general/:filename", GetFile)
}
func AddConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.AddConfigFileReq
if err := c.ShouldBind(&req); err == nil {
err2 := service.CreateConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "add config file failed:" + err2.Error(), "code": proto.AddConfigFileFailed, "message": "failed"})
return
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
return
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
}
func DeleteConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
err2 := service.DeleteConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "delete config file failed:" + err2.Error(), "code": proto.DeleteConfigFailed, "message": "failed"})
return
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
return
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
}
func UpdateConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
var configFileService service.ConfigFileService
err2 := configFileService.UpdateConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "update config file failed:" + err2.Error(), "code": proto.UpdateConfigFailed, "message": "failed"})
return
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
return
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
}
func SearchConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
var configFileService service.ConfigFileService
if req.Type == "one" {
//有文件内容
configFile, err2 := configFileService.SearchOneConfigFile(&req, user_id)
if err2 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": configFile})
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SearchConfigFileFailed, "msg": "info:" + err2.Error(), "data": configFile})
}
} else if req.Type == "all" {
configFileList, err3 := configFileService.SearchAllConfigFile(user_id)
if err3 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": configFileList})
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SearchConfigFileFailed, "msg": "info:" + err3.Error(), "data": configFileList})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "search config file type error", "code": proto.ParameterError, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
}
func UploadFileV2(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
var req proto.FileUploadReq
//获取post form参数
if err := c.ShouldBind(&req); err == nil {
//检查参数
if err2 := service.CheckUploadRequestParameters(&req); err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter check error:" + err2.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
//
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
//从请求头获取upload_type
uploadType := c.PostForm("upload_type")
authType := c.PostForm("auth_type")
md5_ := c.PostForm("md5")
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 <= 0 {
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
}
//计算文件md5值
if md5_ == "" {
file_, _ := file.Open()
md5_ = service.CalculateFileMd5(file_)
if md5_ == "" {
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
return
}
}
//查询文件是否已存在
fileExist := dao.FindFileByMd5(md5_)
if fileExist.ID != 0 {
fileExist.FilePath = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
return
}
//保存文件
filePath, fileStoreName, err := service.SaveFile(c, 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")
var auth_type_ bool
if authType == "public" || authType == "" {
auth_type_ = false
} else if authType == "private" {
auth_type_ = true
}
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, md5_, fileSize, id1, auth_type_)
if file_record.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
file_record.FilePath = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": file_record})
}