mirror of
https://github.com/gowvp/gb28181.git
synced 2026-04-22 15:07:10 +08:00
114 lines
3.2 KiB
Go
Executable File
114 lines
3.2 KiB
Go
Executable File
// Code generated by godddx, DO AVOID EDIT.
|
|
package api
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gowvp/owl/internal/conf"
|
|
"github.com/gowvp/owl/internal/core/event"
|
|
"github.com/gowvp/owl/internal/core/event/store/eventdb"
|
|
"github.com/ixugo/goddd/pkg/orm"
|
|
"github.com/ixugo/goddd/pkg/reason"
|
|
"github.com/ixugo/goddd/pkg/system"
|
|
"github.com/ixugo/goddd/pkg/web"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// EventAPI 为 http 提供业务方法
|
|
type EventAPI struct {
|
|
eventCore event.Core
|
|
conf *conf.Bootstrap
|
|
}
|
|
|
|
func NewEventCore(db *gorm.DB, conf *conf.Bootstrap) event.Core {
|
|
var store event.Storer
|
|
store = eventdb.NewDB(db).AutoMigrate(orm.GetEnabledAutoMigrate())
|
|
core := event.NewCore(store)
|
|
|
|
// 启动定时清理协程
|
|
days := conf.Server.AI.RetainDays
|
|
if days <= 0 {
|
|
days = 7
|
|
}
|
|
go core.StartCleanupWorker(days)
|
|
|
|
return core
|
|
}
|
|
|
|
func NewEventAPI(core event.Core, conf *conf.Bootstrap) EventAPI {
|
|
return EventAPI{eventCore: core, conf: conf}
|
|
}
|
|
|
|
func RegisterEvent(g gin.IRouter, api EventAPI, handler ...gin.HandlerFunc) {
|
|
{
|
|
group := g.Group("/events", handler...)
|
|
group.GET("", web.WrapH(api.findEvents))
|
|
group.GET("/:id", web.WrapH(api.getEvent))
|
|
group.PUT("/:id", web.WrapH(api.editEvent))
|
|
group.DELETE("/:id", web.WrapH(api.delEvent))
|
|
}
|
|
// 图片接口不需要认证中间件
|
|
// TODO: 待添加鉴权
|
|
g.GET("/events/image/*path", api.getEventImage)
|
|
}
|
|
|
|
// findEvents 分页查询事件列表
|
|
func (a EventAPI) findEvents(c *gin.Context, in *event.FindEventInput) (any, error) {
|
|
ctx := web.WithContext(c.Request)
|
|
items, total, err := a.eventCore.FindEvents(ctx, in)
|
|
return gin.H{"items": items, "total": total}, err
|
|
}
|
|
|
|
// getEvent 获取单个事件详情
|
|
func (a EventAPI) getEvent(c *gin.Context, _ *struct{}) (*event.Event, error) {
|
|
eventID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
return a.eventCore.GetEvent(c.Request.Context(), eventID)
|
|
}
|
|
|
|
// editEvent 更新事件信息
|
|
func (a EventAPI) editEvent(c *gin.Context, in *event.EditEventInput) (*event.Event, error) {
|
|
eventID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
return a.eventCore.EditEvent(c.Request.Context(), in, eventID)
|
|
}
|
|
|
|
// delEvent 删除事件
|
|
func (a EventAPI) delEvent(c *gin.Context, _ *struct{}) (*event.Event, error) {
|
|
eventID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
return a.eventCore.DelEvent(c.Request.Context(), eventID)
|
|
}
|
|
|
|
// getEventImage 获取事件快照图片
|
|
func (a EventAPI) getEventImage(c *gin.Context) {
|
|
imagePath := c.Param("path")
|
|
if imagePath == "" {
|
|
web.Fail(c, reason.ErrNotFound.SetMsg("image path is required"))
|
|
return
|
|
}
|
|
|
|
// 去除开头的斜杠
|
|
if len(imagePath) > 0 && imagePath[0] == '/' {
|
|
imagePath = imagePath[1:]
|
|
}
|
|
|
|
fullPath := filepath.Join(system.Getwd(), "configs", "events", imagePath)
|
|
|
|
// 安全检查:防止路径遍历攻击
|
|
eventsDir := filepath.Join(system.Getwd(), "configs", "events")
|
|
absPath, err := filepath.Abs(fullPath)
|
|
if err != nil || !filepath.HasPrefix(absPath, eventsDir) {
|
|
web.Fail(c, reason.ErrNotFound.SetMsg("invalid path"))
|
|
return
|
|
}
|
|
|
|
body, err := os.ReadFile(fullPath)
|
|
if err != nil {
|
|
web.Fail(c, reason.ErrNotFound.SetMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
c.Data(200, "image/jpeg", body)
|
|
}
|