feat: add media sources
This commit is contained in:
@@ -28,3 +28,30 @@ func (c *Controller) List(ctx fiber.Ctx) error {
|
||||
|
||||
return ctx.JSON(items)
|
||||
}
|
||||
|
||||
// Show
|
||||
func (c *Controller) Show(ctx fiber.Ctx) error {
|
||||
id := ToInt64(ctx.Params("id"))
|
||||
tenantId, userId := ToInt64(ctx.Locals("tenantId")), ToInt64(ctx.Locals("userId"))
|
||||
|
||||
item, err := c.svc.GetByID(ctx.Context(), tenantId, userId, id)
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
||||
}
|
||||
|
||||
return ctx.JSON(item)
|
||||
}
|
||||
|
||||
// Audio
|
||||
func (c *Controller) Video(ctx fiber.Ctx) error {
|
||||
mediaId := ToInt64(ctx.Params("media"))
|
||||
tenantId := ToInt64(ctx.Locals("tenantId"))
|
||||
userId := ToInt64(ctx.Locals("userId"))
|
||||
|
||||
item, err := c.svc.GetVideo(ctx.Context(), tenantId, userId, mediaId)
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
||||
}
|
||||
|
||||
return ctx.JSON(item)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func (r *Router) Register() error {
|
||||
group := r.http.Engine.Group("medias")
|
||||
log.Infof("register route group: %s", group.(*fiber.Group).Prefix)
|
||||
group.Get("", r.controller.List)
|
||||
group.Get("{id}", r.controller.List)
|
||||
group.Get("{id}", r.controller.Show)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/pkg/pg"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/pkg/errors"
|
||||
@@ -25,11 +26,15 @@ func (svc *Service) Prepare() error {
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (svc *Service) GetByID(ctx context.Context, id, userId int64) (*ListItem, error) {
|
||||
func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*ListItem, error) {
|
||||
log := svc.log.WithField("method", "GetByID")
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int(id)))
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(
|
||||
tbl.ID.EQ(Int(id)).AND(
|
||||
tbl.TenantID.EQ(Int(tenantId)),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.Sql())
|
||||
|
||||
var media ListItem
|
||||
@@ -59,7 +64,7 @@ func (svc *Service) GetByID(ctx context.Context, id, userId int64) (*ListItem, e
|
||||
media.MediaResources = resources
|
||||
|
||||
var err error
|
||||
media.Bought, err = svc.HasUserBought(ctx, media.TenantID, userId, media.ID)
|
||||
media.Bought, err = svc.HasUserBought(ctx, tenantId, userId, media.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "check user bought")
|
||||
}
|
||||
@@ -220,3 +225,58 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
|
||||
|
||||
return mediaID > 0, nil
|
||||
}
|
||||
|
||||
// GetVideo
|
||||
func (svc *Service) GetVideo(ctx context.Context, tenantId, userId, mediaId int64) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetVideo")
|
||||
|
||||
ok, err := svc.HasUserBought(ctx, tenantId, userId, mediaId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "check user bought")
|
||||
}
|
||||
log.Debugf("user bought media: %d of tennatID: %d", mediaId, tenantId)
|
||||
|
||||
source, err := svc.GetResourceOfType(ctx, mediaId, pg.MediaTypeVideo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get video resource")
|
||||
}
|
||||
|
||||
if ok {
|
||||
return source, nil
|
||||
}
|
||||
// cut 1 min video
|
||||
maxDuration := int64(60)
|
||||
duration := int64(0)
|
||||
|
||||
for i, item := range source.Items {
|
||||
duration += item.Duration
|
||||
if duration >= maxDuration {
|
||||
source.Items = source.Items[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return source, nil
|
||||
}
|
||||
|
||||
// GetResourceOfType
|
||||
func (svc *Service) GetResourceOfType(ctx context.Context, mediaId int64, resourceType pg.MediaType) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetResourceOfType")
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.
|
||||
SELECT(tbl.Source).
|
||||
WHERE(
|
||||
tbl.MediaID.EQ(Int(mediaId)).AND(
|
||||
tbl.Type.EQ(String(resourceType.String())),
|
||||
),
|
||||
)
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var source pg.MediaSource
|
||||
if err := stmt.QueryContext(ctx, svc.db, &source); err != nil {
|
||||
return nil, errors.Wrap(err, "query media source")
|
||||
}
|
||||
|
||||
return &source, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user