2024-05-18 17:12:24 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2024-06-24 17:58:22 +08:00
|
|
|
"encoding/json"
|
2024-05-21 11:10:21 +08:00
|
|
|
"fmt"
|
2024-05-18 17:12:24 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2024-05-29 17:27:46 +08:00
|
|
|
"strconv"
|
2024-05-18 17:12:24 +08:00
|
|
|
"time"
|
2024-06-25 09:38:21 +08:00
|
|
|
"videoplayer/proto"
|
2024-05-18 17:12:24 +08:00
|
|
|
"videoplayer/service"
|
2024-06-24 17:58:22 +08:00
|
|
|
"videoplayer/worker"
|
2024-05-18 17:12:24 +08:00
|
|
|
)
|
|
|
|
|
|
2024-05-20 11:02:55 +08:00
|
|
|
// video获取视频列表请求
|
2024-05-18 17:12:24 +08:00
|
|
|
type gvlReq struct {
|
2024-12-17 17:16:22 +08:00
|
|
|
ID int `json:"id" form:"id"`
|
2024-10-25 18:04:08 +08:00
|
|
|
StartTime int64 `json:"begin" form:"begin"`
|
|
|
|
|
EndTime int64 `json:"end" form:"end"`
|
2024-05-28 17:27:46 +08:00
|
|
|
IP string `json:"ip" form:"ip"`
|
|
|
|
|
Token string `json:"token" form:"token"`
|
|
|
|
|
Hour string `json:"hour" form:"hour"`
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
|
|
|
|
|
// video添加视频记录请求
|
2024-05-18 17:12:24 +08:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 14:57:45 +08:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 11:02:55 +08:00
|
|
|
// video播放视频请求
|
2024-05-18 17:12:24 +08:00
|
|
|
type videoPReq struct {
|
2024-05-28 17:27:46 +08:00
|
|
|
ID int `form:"id"`
|
|
|
|
|
Token string `form:"token"`
|
|
|
|
|
AuthId string `form:"userId"`
|
|
|
|
|
IP string `form:"ip"`
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 17:30:39 +08:00
|
|
|
type VideoDelReq struct {
|
|
|
|
|
ID int `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 11:02:55 +08:00
|
|
|
// video延迟视频请求
|
|
|
|
|
type delayReq struct {
|
2024-05-29 16:57:26 +08:00
|
|
|
ID int `form:"id"`
|
2024-05-28 17:27:46 +08:00
|
|
|
Option string `form:"option"`
|
2024-05-29 16:57:26 +08:00
|
|
|
AuthId int `form:"userId"`
|
2024-05-28 17:27:46 +08:00
|
|
|
IP string `form:"ip"`
|
2024-05-29 16:57:26 +08:00
|
|
|
Day int `form:"delay"`
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-18 17:12:24 +08:00
|
|
|
func SetUpVideoGroup(router *gin.Engine) {
|
|
|
|
|
videoGroup := router.Group("/video")
|
|
|
|
|
videoGroup.POST("/get_video_list", GetVideoList)
|
2024-05-22 11:31:03 +08:00
|
|
|
videoGroup.GET("/will_del_video_list", GetWillDelVideoList)
|
2024-05-18 17:12:24 +08:00
|
|
|
videoGroup.POST("/create", CreateVideo)
|
|
|
|
|
videoGroup.GET("/mp4", GetVideo)
|
2024-05-20 11:02:55 +08:00
|
|
|
videoGroup.POST("/delay", DelayVideo)
|
|
|
|
|
videoGroup.POST("/delete", DeleteVideo)
|
2024-05-21 14:57:45 +08:00
|
|
|
videoGroup.POST("/update", UpdateVideo)
|
2024-06-24 17:58:22 +08:00
|
|
|
videoGroup.POST("/quash_option", QuashOption)
|
2024-05-21 14:57:45 +08:00
|
|
|
}
|
2024-05-21 15:48:20 +08:00
|
|
|
|
|
|
|
|
func GetWillDelVideoList(c *gin.Context) {
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
videos := service.GetWillDelVideoList(int(id.(float64)))
|
2024-06-25 10:43:17 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"videos": videos, "code": proto.SuccessCode, "message": "success"})
|
2024-05-21 15:48:20 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 14:57:45 +08:00
|
|
|
func UpdateVideo(c *gin.Context) {
|
|
|
|
|
var video_req videoUpdateReq
|
|
|
|
|
user_id, _ := c.Get("id")
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&video_req); err == nil {
|
2024-05-21 14:57:45 +08:00
|
|
|
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 {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "update video failed", "code": proto.OperationFailed, "message": "failed"})
|
2024-05-21 14:57:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-25 10:43:17 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": "update success"})
|
2024-05-21 14:57:45 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
2024-05-21 14:57:45 +08:00
|
|
|
}
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetVideo(c *gin.Context) {
|
|
|
|
|
var vp_req videoPReq
|
2024-05-29 16:57:26 +08:00
|
|
|
user_id, _ := c.Get("id")
|
2024-05-18 17:12:24 +08:00
|
|
|
if err := c.ShouldBindQuery(&vp_req); err == nil {
|
2024-05-28 17:27:46 +08:00
|
|
|
fmt.Println(vp_req)
|
|
|
|
|
video := service.GetVideo(vp_req.ID, int(user_id.(float64)))
|
2024-05-18 17:12:24 +08:00
|
|
|
name := video.VideoName
|
|
|
|
|
path := video.VideoPath
|
|
|
|
|
if video.IsDelete == 0 {
|
|
|
|
|
// 打开文件
|
|
|
|
|
file, err := os.Open(path + name)
|
|
|
|
|
if err != nil {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
2024-05-18 17:12:24 +08:00
|
|
|
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 {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "video is deleted"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func CreateVideo(c *gin.Context) {
|
|
|
|
|
var video_req videoReq
|
2024-05-20 17:30:39 +08:00
|
|
|
user_id, _ := c.Get("id")
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&video_req); err == nil {
|
2024-05-21 11:10:21 +08:00
|
|
|
fmt.Println(video_req)
|
2024-06-05 17:06:29 +08:00
|
|
|
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)
|
2024-05-20 17:30:39 +08:00
|
|
|
if id == 0 {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": proto.DataNotFound, "message": "failed"})
|
2024-05-20 17:30:39 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-25 10:43:17 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"id": id, "code": proto.SuccessCode, "message": "success"})
|
2024-05-18 17:12:24 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetVideoList(c *gin.Context) {
|
|
|
|
|
var gvl_req gvlReq
|
2024-06-14 17:59:53 +08:00
|
|
|
gvl_req.Hour = "33"
|
2024-05-20 16:01:04 +08:00
|
|
|
id, _ := c.Get("id")
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&gvl_req); err == nil {
|
2024-10-25 18:04:08 +08:00
|
|
|
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)
|
2024-10-25 18:45:04 +08:00
|
|
|
if gvl_req.StartTime == 0 || gvl_req.EndTime == 0 {
|
|
|
|
|
tm1 = ""
|
|
|
|
|
tm2 = ""
|
|
|
|
|
}
|
2024-12-17 17:16:22 +08:00
|
|
|
videos := service.GetVideoList(int(id.(float64)), gvl_req.ID, tm1, tm2, gvl_req.Hour)
|
2024-06-25 10:43:17 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"data": videos, "code": proto.SuccessCode, "message": "success"})
|
2024-05-18 17:12:24 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
|
|
|
|
|
func DelayVideo(c *gin.Context) {
|
|
|
|
|
var delay_req delayReq
|
2024-05-20 16:01:04 +08:00
|
|
|
id, _ := c.Get("id")
|
2024-06-24 17:58:22 +08:00
|
|
|
user, _ := c.Get("username")
|
2024-05-29 16:57:26 +08:00
|
|
|
cnt := 0
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&delay_req); err == nil {
|
2024-05-29 16:57:26 +08:00
|
|
|
if delay_req.Day > 30 || delay_req.Day < 1 {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.VideoDelayOperationFailed, "data": "延迟天数过大或过小", "message": "failed"})
|
2024-05-28 17:27:46 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-05-29 16:57:26 +08:00
|
|
|
if delay_req.Option == "all" {
|
|
|
|
|
cnt = service.DelayAllVideo(int(id.(float64)), delay_req.Day)
|
2024-05-28 17:27:46 +08:00
|
|
|
}
|
2024-05-29 16:57:26 +08:00
|
|
|
if delay_req.Option == "one" {
|
|
|
|
|
cnt = service.DelayVideo(delay_req.ID, int(id.(float64)), delay_req.Day)
|
|
|
|
|
}
|
|
|
|
|
if cnt == 0 {
|
2024-06-25 10:43:17 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.OperationFailed, "data": "延迟失败数据", "message": "failed,cnt:" + strconv.Itoa(cnt)})
|
2024-05-29 16:57:26 +08:00
|
|
|
} else {
|
2024-06-25 16:32:38 +08:00
|
|
|
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}
|
2024-06-24 17:58:22 +08:00
|
|
|
str, _ := json.Marshal(data)
|
2024-06-25 10:43:17 +08:00
|
|
|
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)})
|
2024-05-28 17:27:46 +08:00
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteVideo(c *gin.Context) {
|
2024-05-20 17:30:39 +08:00
|
|
|
var video_req VideoDelReq
|
2024-05-20 16:01:04 +08:00
|
|
|
id, _ := c.Get("id")
|
2024-06-24 17:58:22 +08:00
|
|
|
user, _ := c.Get("username")
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&video_req); err == nil {
|
2024-06-24 17:58:22 +08:00
|
|
|
res := service.DeleteVideo(video_req.ID, int(id.(float64)))
|
|
|
|
|
if res != 0 {
|
2024-06-25 16:32:38 +08:00
|
|
|
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"}
|
2024-06-24 17:58:22 +08:00
|
|
|
str, _ := json.Marshal(data)
|
2024-06-25 10:43:17 +08:00
|
|
|
worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", string(str))
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
2024-06-24 17:58:22 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "failed"})
|
2024-06-24 17:58:22 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
2024-06-24 17:58:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func QuashOption(c *gin.Context) {
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
user, _ := c.Get("username")
|
2024-06-25 16:32:38 +08:00
|
|
|
res := worker.PopRedisList(user.(string) + "-" + strconv.Itoa(int(id.(float64))) + "-option")
|
|
|
|
|
if res != "" {
|
|
|
|
|
var retrievedData map[string]interface{}
|
|
|
|
|
err2 := json.Unmarshal([]byte(res), &retrievedData)
|
2024-06-25 16:47:06 +08:00
|
|
|
if err2 == nil {
|
2024-06-25 16:32:38 +08:00
|
|
|
code, msg := service.QuashVideo(int(id.(float64)), retrievedData)
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg})
|
2024-06-24 17:58:22 +08:00
|
|
|
} else {
|
2024-06-25 16:47:06 +08:00
|
|
|
worker.PushRedisList(user.(string)+"-"+strconv.Itoa(int(id.(float64)))+"-option", res) //未操作成功重新添加到队列
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": err2, "data": "json解析错误"})
|
2024-06-25 16:32:38 +08:00
|
|
|
return
|
2024-06-24 17:58:22 +08:00
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
} else {
|
2024-06-25 16:32:38 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "redis data not found", "data": "error"})
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
}
|