videoplayer/handler/video.go

197 lines
6.1 KiB
Go

package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"os"
"strconv"
"time"
"videoplayer/service"
)
// video获取视频列表请求
type gvlReq struct {
StartTime string `json:"begin" form:"begin"`
EndTime string `json:"end" form:"end"`
IP string `json:"ip" form:"ip"`
Token string `json:"token" form:"token"`
Hour string `json:"hour" form:"hour"`
}
// video添加视频记录请求
type videoReq struct {
CameraID int `json:"camera_id"`
VideoPath string `json:"video_path"`
VideoName string `json:"video_name"`
AuthId int `json:"auth_id"`
Human int `json:"human"`
IsDelete int `json:"is_delete"`
CreateTime string `json:"create_time"`
EndTime string `json:"end_time"`
DeleteTime string `json:"delete_time"`
FileSize int `json:"file_size"`
}
type videoUpdateReq struct {
ID int `json:"id"`
CameraID int `json:"camera_id"`
VideoPath string `json:"video_path"`
VideoName string `json:"video_name"`
AuthId int `json:"auth_id"`
Human int `json:"human"`
IsDelete int `json:"is_delete"`
CreateTime string `json:"create_time"`
EndTime string `json:"end_time"`
DeleteTime string `json:"delete_time"`
FileSize int `json:"file_size"`
}
// video播放视频请求
type videoPReq struct {
ID int `form:"id"`
Token string `form:"token"`
AuthId string `form:"userId"`
IP string `form:"ip"`
}
type VideoDelReq struct {
ID int `json:"id"`
}
// video延迟视频请求
type delayReq struct {
ID int `form:"id"`
Option string `form:"option"`
AuthId int `form:"userId"`
IP string `form:"ip"`
Day int `form:"delay"`
}
func SetUpVideoGroup(router *gin.Engine) {
videoGroup := router.Group("/video")
videoGroup.POST("/get_video_list", GetVideoList)
videoGroup.GET("/will_del_video_list", GetWillDelVideoList)
videoGroup.POST("/create", CreateVideo)
videoGroup.GET("/mp4", GetVideo)
videoGroup.POST("/delay", DelayVideo)
videoGroup.POST("/delete", DeleteVideo)
videoGroup.POST("/update", UpdateVideo)
}
func GetWillDelVideoList(c *gin.Context) {
id, _ := c.Get("id")
videos := service.GetWillDelVideoList(int(id.(float64)))
c.JSON(http.StatusOK, gin.H{"videos": videos, "code": 0, "message": "success"})
}
func UpdateVideo(c *gin.Context) {
var video_req videoUpdateReq
user_id, _ := c.Get("id")
if err := c.ShouldBind(&video_req); err == nil {
res := service.UpdateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, video_req.ID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.FileSize)
if !res {
c.JSON(http.StatusOK, gin.H{"error": "update video failed", "code": 1, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}
func GetVideo(c *gin.Context) {
var vp_req videoPReq
user_id, _ := c.Get("id")
if err := c.ShouldBindQuery(&vp_req); err == nil {
fmt.Println(vp_req)
video := service.GetVideo(vp_req.ID, int(user_id.(float64)))
name := video.VideoName
path := video.VideoPath
if video.IsDelete == 0 {
// 打开文件
file, err := os.Open(path + name)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
defer file.Close()
// 设置响应头
c.Header("Content-Type", "video/mp4")
// 开始传输文件
http.ServeContent(c.Writer, c.Request, name, time.Now(), file) // 传输文件, 传输文件的内容类型, 文件名, 文件最后修改时间, 文件
// 返回文件
c.JSON(http.StatusOK, gin.H{"message": "success", "code": 200})
} else {
c.JSON(http.StatusOK, gin.H{"error": "video is deleted"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}
}
func CreateVideo(c *gin.Context) {
var video_req videoReq
user_id, _ := c.Get("id")
if err := c.ShouldBind(&video_req); err == nil {
fmt.Println(video_req)
id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.DeleteTime, video_req.FileSize)
if id == 0 {
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": 1, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"id": id, "code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}
func GetVideoList(c *gin.Context) {
var gvl_req gvlReq
id, _ := c.Get("id")
if err := c.ShouldBind(&gvl_req); err == nil {
videos := service.GetVideoList(int(id.(float64)), gvl_req.StartTime, gvl_req.EndTime)
c.JSON(http.StatusOK, gin.H{"data": videos, "code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}
func DelayVideo(c *gin.Context) {
var delay_req delayReq
id, _ := c.Get("id")
cnt := 0
if err := c.ShouldBind(&delay_req); err == nil {
if delay_req.Day > 30 || delay_req.Day < 1 {
c.JSON(http.StatusOK, gin.H{"code": 1, "data": "延迟天数过大或过小", "message": "failed"})
return
}
if delay_req.Option == "all" {
cnt = service.DelayAllVideo(int(id.(float64)), delay_req.Day)
}
if delay_req.Option == "one" {
cnt = service.DelayVideo(delay_req.ID, int(id.(float64)), delay_req.Day)
}
if cnt == 0 {
c.JSON(http.StatusOK, gin.H{"code": 1, "data": "延迟失败影响数据", "message": "failed,cnt:" + strconv.Itoa(cnt)})
} else {
c.JSON(http.StatusOK, gin.H{"code": 0, "data": "延迟成功,影响记录:" + strconv.Itoa(cnt), "message": "success cnt:" + strconv.Itoa(cnt)})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}
func DeleteVideo(c *gin.Context) {
var video_req VideoDelReq
id, _ := c.Get("id")
if err := c.ShouldBind(&video_req); err == nil {
service.DeleteVideo(video_req.ID, int(id.(float64)))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}