package handler import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" "net/http" "os" "strconv" "time" "videoplayer/proto" "videoplayer/service" "videoplayer/worker" ) // video获取视频列表请求 type gvlReq struct { StartTime int64 `json:"begin" form:"begin"` EndTime int64 `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) videoGroup.POST("/quash_option", QuashOption) } func GetWillDelVideoList(c *gin.Context) { id, _ := c.Get("id") videos := service.GetWillDelVideoList(int(id.(float64))) c.JSON(http.StatusOK, gin.H{"videos": videos, "code": proto.SuccessCode, "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": proto.OperationFailed, "message": "failed"}) return } c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": "update success"}) } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "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": proto.DataNotFound, "message": "failed"}) return } c.JSON(http.StatusOK, gin.H{"id": id, "code": proto.SuccessCode, "message": "success"}) } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } func GetVideoList(c *gin.Context) { var gvl_req gvlReq gvl_req.Hour = "33" id, _ := c.Get("id") if err := c.ShouldBind(&gvl_req); err == nil { const layout = "2006-01-02 15:04:05" tm1 := time.Unix(gvl_req.StartTime, 0).Format(layout) tm2 := time.Unix(gvl_req.EndTime, 0).Format(layout) videos := service.GetVideoList(int(id.(float64)), tm1, tm2, gvl_req.Hour) c.JSON(http.StatusOK, gin.H{"data": videos, "code": proto.SuccessCode, "message": "success"}) } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } func DelayVideo(c *gin.Context) { var delay_req delayReq id, _ := c.Get("id") user, _ := c.Get("username") 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": proto.VideoDelayOperationFailed, "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": proto.OperationFailed, "data": "延迟失败数据", "message": "failed,cnt:" + strconv.Itoa(cnt)}) } else { data := map[string]interface{}{"auth_id": int(id.(float64)), "method": "delay", "delay_time": time.Now().Format("2006-01-02 15:04:05"), "delay_day": delay_req.Day, "option": delay_req.Option, "id": delay_req.ID} str, _ := json.Marshal(data) worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", string(str)) c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": "延迟成功,影响记录:" + strconv.Itoa(cnt), "message": "success cnt:" + strconv.Itoa(cnt)}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } func DeleteVideo(c *gin.Context) { var video_req VideoDelReq id, _ := c.Get("id") user, _ := c.Get("username") if err := c.ShouldBind(&video_req); err == nil { res := service.DeleteVideo(video_req.ID, int(id.(float64))) if res != 0 { data := map[string]interface{}{"id": video_req.ID, "auth_id": int(id.(float64)), "delete_time": time.Now().Format("2006-01-02 15:04:05"), "method": "delete"} str, _ := json.Marshal(data) worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", string(str)) c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"}) } else { c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "failed"}) } } else { c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) } } func QuashOption(c *gin.Context) { id, _ := c.Get("id") user, _ := c.Get("username") res := worker.PopRedisList(user.(string) + "-" + strconv.Itoa(int(id.(float64))) + "-option") if res != "" { var retrievedData map[string]interface{} err2 := json.Unmarshal([]byte(res), &retrievedData) if err2 == nil { code, msg := service.QuashVideo(int(id.(float64)), retrievedData) c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg}) } else { worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", res) //未操作成功重新添加到队列 c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": err2, "data": "json解析错误"}) return } } else { c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "redis data not found", "data": "error"}) } }