videoplayer/handler/tool.go

272 lines
7.6 KiB
Go
Raw Normal View History

2024-07-19 09:43:17 +08:00
package handler
import (
2024-08-30 15:40:45 +08:00
"fmt"
2024-07-19 09:43:17 +08:00
"github.com/gin-gonic/gin"
2024-08-30 15:40:45 +08:00
"io"
2024-07-19 09:43:17 +08:00
"net/http"
2024-08-30 15:40:45 +08:00
"os"
2024-08-30 15:26:39 +08:00
"strconv"
"time"
2024-07-19 09:43:17 +08:00
"videoplayer/dao"
"videoplayer/proto"
"videoplayer/service"
"videoplayer/worker"
2024-07-19 09:43:17 +08:00
)
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"`
}
type SetDeviceStatusReq struct {
ID string `json:"id" form:"id"` //设备编码
Status string `json:"status" form:"status"` //设备状态
}
2024-07-19 09:43:17 +08:00
func SetUpToolGroup(router *gin.Engine) {
toolGroup := router.Group("/tool")
toolGroup.POST("/set_redis", SetRedis)
toolGroup.POST("/get_redis", GetRedis)
2024-08-30 11:28:27 +08:00
//文件上传、下载
toolGroup.POST("/upload", UploadFile)
2024-08-30 15:41:51 +08:00
toolGroup.GET("/download", DownloadFile)
2024-08-30 21:46:28 +08:00
//文件管理
toolGroup.POST("/file_del", DelFile)
//服务器、设备状态接口
toolGroup.POST("/monitor", SetDeviceStatusV2)
}
func SetDeviceStatusV2(c *gin.Context) {
// TODO
var req SetDeviceStatusReq
if err := c.ShouldBind(&req); err != nil {
c.JSON(200, gin.H{"code": 400, "message": "参数错误"})
return
} else {
token, _ := c.Get("token")
if token == nil {
c.JSON(200, gin.H{"code": 401, "message": "token为空"})
return
}
devices := worker.GetRedisSetMembers(token.(string))
if len(devices) == 0 {
c.JSON(200, gin.H{"code": 402, "message": "设备为空"})
return
}
for _, v := range devices {
if v == req.ID {
// 继续处理请求
worker.SetRedisWithExpire(req.ID, "1", 300)
c.JSON(200, gin.H{"code": 0, "message": "success"})
return
}
}
c.JSON(200, gin.H{"code": 402, "message": "设备不存在"})
}
2024-08-30 21:46:28 +08:00
}
func DelFile(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
file_id, _ := strconv.Atoi(c.PostForm("id"))
file_ := dao.FindFileByID(file_id, id1)
if file_.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return
}
//删除文件
err := os.Remove(file_.FilePath + "/" + file_.FileStoreName)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": "delete file failed", "code": proto.DeleteFileFailed, "message": "failed"})
return
}
//删除文件信息
if res := dao.DeleteFileById(file_id); !res {
c.JSON(http.StatusOK, gin.H{"error": "delete file info failed", "code": proto.DeleteFileInfoFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
2024-08-30 11:28:27 +08:00
}
func UploadFile(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
//从请求头获取upload_type
2024-08-30 15:13:23 +08:00
uploadType := c.PostForm("upload_type")
if uploadType == "" {
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
return
}
2024-08-30 11:28:27 +08:00
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
}
//保存文件
2024-08-30 21:46:28 +08:00
filePath, fileStoreName, err := service.SaveFile(c, file, uploadType)
2024-08-30 11:28:27 +08:00
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
}
2024-08-30 22:31:08 +08:00
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileID})
2024-08-30 11:28:27 +08:00
}
func DownloadFile(c *gin.Context) {
//参数
2024-08-30 15:26:39 +08:00
//filename := c.Param("filename")
file_id, _ := strconv.Atoi(c.Query("id"))
2024-08-30 11:28:27 +08:00
id, _ := c.Get("id")
//查询文件信息
2024-08-30 15:26:39 +08:00
//file := dao.FindFileByNames(file_id, int(id.(float64)))
2024-08-30 15:40:45 +08:00
file_ := dao.FindFileByID(file_id, int(id.(float64)))
if file_.ID == 0 {
2024-08-30 11:28:27 +08:00
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
return
}
//下载文件
2024-08-30 15:40:45 +08:00
// 打开文件
file, err := os.Open(file_.FilePath + "/" + file_.FileStoreName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to open file"})
return
}
defer file.Close()
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file_.FileName))
// 发送文件内容
_, err = io.Copy(c.Writer, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to send file"})
return
}
2024-08-30 15:47:57 +08:00
c.Status(http.StatusOK)
2024-07-19 09:43:17 +08:00
}
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
}
}
2024-07-19 09:43:17 +08:00
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})
2024-07-19 09:43:17 +08:00
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
}
}
// 服务器、设备状态扫描
func ScanDeviceStatus() {
// TODO
// 检查设备状态
// 如果设备状态异常, 则发送邮件通知
devices := worker.GetRedisSetMembers("627gyf3488h")
offline := ""
for _, v := range devices {
res := worker.GetRedis(v)
if res == "" {
c := worker.IsContainKey("monitor_" + v)
if c == false {
worker.SetRedisWithExpire("monitor_"+v, "1", time.Hour*12)
offline += v + ","
}
}
}
if offline != "" {
title := "设备状态异常"
content := "设备状态异常\n设备: " + offline + "\t时间" + time.Now().String()
go SendMail(title, content)
}
}
func SendMail(title, content string) {
// TODO
// 发送邮件
// 邮件内容
// 邮件标题
// 收件人
// 发送邮件
// 发送邮件通知
// 发送邮件通知
var em worker.MyEmail
em.SmtpPassword = "nihzazdkmucnbhid"
em.SmtpHost = "pop.qq.com:587"
em.SmtpUserName = "354425203@qq.com"
em.SmtpPort = 587
em.ImapPort = 993
err := em.Send(title, content, []string{"3236990479@qq.com", "lijun@ljsea.top"})
if err != nil {
fmt.Println(err)
}
}