fix: issues

This commit is contained in:
Rogee
2024-12-10 10:24:31 +08:00
parent 0c9cb498d5
commit cb1cdee87e
32 changed files with 226 additions and 61 deletions

View File

@@ -1,17 +1,20 @@
package medias
import (
"backend/common/consts"
"backend/common/errorx"
"backend/pkg/consts"
"backend/pkg/errorx"
"backend/pkg/pg"
"backend/providers/jwt"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
hashids "github.com/speps/go-hashids/v2"
)
// @provider
type Controller struct {
svc *Service
hashIds *hashids.HashID
svc *Service
}
// List
@@ -54,10 +57,60 @@ func (c *Controller) Show(ctx fiber.Ctx) error {
}
// Audio
func (c *Controller) Video(ctx fiber.Ctx) error {
// mediaId := ToInt64(ctx.Params("media"))
// tenantId := ToInt64(ctx.Locals("tenantId"))
// userId := ToInt64(ctx.Locals("userId"))
func (c *Controller) MediaIndex(ctx fiber.Ctx) error {
mediaType, err := pg.ParseMediaType(ctx.Params("type"))
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.RequestParseError)
}
return ctx.JSON(nil)
hash := ctx.Params("hash")
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash)
if err != nil {
log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaByHash")
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
}
bought, err := c.svc.HasUserBought(ctx.Context(), claim.TenantID, claim.UserID, model.ID)
if err != nil {
log.WithField("action", "medias.MediaIndex").WithError(err).Error("HasUserBought")
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
}
playlist, err := c.svc.GetM3U8(ctx.Context(), claim.TenantID, mediaType, model.Hash, bought)
if err != nil {
log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaPlaylist")
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
}
return ctx.SendString(playlist.String())
}
func (c *Controller) MediaSegment(ctx fiber.Ctx) error {
mediaType, err := pg.ParseMediaType(ctx.Params("type"))
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.RequestParseError)
}
segment := ctx.Params("segment")
segments, err := c.hashIds.DecodeInt64WithError(segment)
if err != nil {
log.WithField("action", "medias.MediaSegment").WithError(err).Error("DecodeInt64WithError")
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.RequestParseError)
}
hash := ctx.Params("hash")
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash)
if err != nil {
log.WithField("action", "medias.MediaSegment").WithError(err).Error("GetMediaByHash")
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
}
filepath := c.svc.GetSegmentPath(ctx.Context(), mediaType, model.Hash, segments[0])
return ctx.SendFile(filepath)
}