fix: issues
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -8,14 +8,17 @@ import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
hashids "github.com/speps/go-hashids/v2"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
hashIds *hashids.HashID,
|
||||
svc *Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
svc: svc,
|
||||
hashIds: hashIds,
|
||||
svc: svc,
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
@@ -38,10 +41,12 @@ func Provide(opts ...opt.Option) error {
|
||||
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
hashIds *hashids.HashID,
|
||||
storageConfig *storage.Config,
|
||||
) (*Service, error) {
|
||||
obj := &Service{
|
||||
db: db,
|
||||
hashIds: hashIds,
|
||||
storageConfig: storageConfig,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
|
||||
@@ -27,4 +27,6 @@ func (r *Router) Register(router fiber.Router) {
|
||||
group := router.Group(r.Name())
|
||||
group.Post("", r.controller.List)
|
||||
group.Get(":hash", r.controller.Show)
|
||||
group.Get(":hash/:type<regex([video|audio])>", r.controller.MediaIndex)
|
||||
group.Get(":hash/:type<regex([video|audio])>/:segment.ts", r.controller.MediaSegment)
|
||||
}
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"backend/common/media_store"
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/pkg/media_store"
|
||||
"backend/pkg/path"
|
||||
"backend/pkg/pg"
|
||||
"backend/providers/storage"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/grafov/m3u8"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
hashids "github.com/speps/go-hashids/v2"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
hashIds *hashids.HashID
|
||||
storageConfig *storage.Config
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
@@ -244,3 +251,66 @@ func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// get video m3u8
|
||||
func (svc *Service) GetM3U8(ctx context.Context, tenantId int64, types pg.MediaType, hash string, bought bool) (m3u8.Playlist, error) {
|
||||
log := svc.log.WithField("method", "GetM3U8")
|
||||
indexPath := filepath.Join(svc.storageConfig.Path, hash, types.String(), "index.m3u8")
|
||||
log.Infof("m3u8 path: %s", indexPath)
|
||||
|
||||
f, err := os.Open(indexPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "open index file")
|
||||
}
|
||||
|
||||
p, listType, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "decode index file")
|
||||
}
|
||||
|
||||
if listType != m3u8.MEDIA {
|
||||
return nil, errors.New("Invalid media file")
|
||||
}
|
||||
|
||||
media, ok := p.(*m3u8.MediaPlaylist)
|
||||
if !ok {
|
||||
return nil, errors.New("Invalid media playlist")
|
||||
}
|
||||
media.Segments = lo.Filter(media.Segments, func(seg *m3u8.MediaSegment, _ int) bool {
|
||||
return seg != nil
|
||||
})
|
||||
|
||||
if !bought {
|
||||
duration := 0
|
||||
for i, seg := range media.Segments {
|
||||
duration += int(seg.Duration)
|
||||
if duration >= 55 {
|
||||
media.Segments = media.Segments[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, seg := range media.Segments {
|
||||
// remove seg.URI ext, only keep the name
|
||||
name, ext := path.SplitNameExt(seg.URI)
|
||||
nameId, err := cast.ToInt64E(name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cast index to int64")
|
||||
}
|
||||
|
||||
// get video info
|
||||
hashID, err := svc.hashIds.EncodeInt64([]int64{nameId})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "encode hash id")
|
||||
}
|
||||
seg.URI = fmt.Sprintf("%s/%s.%s", types, hashID, ext)
|
||||
}
|
||||
|
||||
return media, nil
|
||||
}
|
||||
|
||||
// GetSegmentPath
|
||||
func (svc *Service) GetSegmentPath(ctx context.Context, t pg.MediaType, hash string, segment int64) string {
|
||||
return filepath.Join(svc.storageConfig.Path, hash, t.String(), fmt.Sprintf("%d.ts", segment))
|
||||
}
|
||||
|
||||
@@ -4,48 +4,65 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/fixtures"
|
||||
dbUtil "backend/pkg/db"
|
||||
"backend/pkg/path"
|
||||
"backend/pkg/pg"
|
||||
"backend/pkg/service/testx"
|
||||
"backend/providers/hashids"
|
||||
"backend/providers/postgres"
|
||||
"backend/providers/storage"
|
||||
|
||||
"github.com/samber/lo"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func TestService_GetUserBoughtMedias(t *testing.T) {
|
||||
Convey("TestService_GetUserBoughtMedias", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
type ServiceInjectParams struct {
|
||||
dig.In
|
||||
Svc *Service
|
||||
}
|
||||
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "user_medias"), ShouldBeNil)
|
||||
type ServiceTestSuite struct {
|
||||
suite.Suite
|
||||
ServiceInjectParams
|
||||
}
|
||||
|
||||
Convey("insert some data", func() {
|
||||
items := []model.UserMedias{
|
||||
{UserID: 1, TenantID: 1, MediaID: 1, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 2, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 3, Price: 10},
|
||||
}
|
||||
func Test_DiscoverMedias(t *testing.T) {
|
||||
providers := testx.Default(
|
||||
postgres.DefaultProvider(),
|
||||
storage.DefaultProvider(),
|
||||
hashids.DefaultProvider(),
|
||||
).With(
|
||||
Provide,
|
||||
)
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.INSERT(tbl.UserID, tbl.TenantID, tbl.MediaID, tbl.Price).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
testx.Serve(providers, t, func(params ServiceInjectParams) {
|
||||
suite.Run(t, &ServiceTestSuite{ServiceInjectParams: params})
|
||||
})
|
||||
}
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
func (t *ServiceTestSuite) Test_getM3U8() {
|
||||
FocusConvey("Test_ffmpegVideoToM3U8", t.T(), func() {
|
||||
Convey("Bought", func() {
|
||||
hash := "f464a6641a60e2722e4042db8fad2813"
|
||||
media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, true)
|
||||
So(err, ShouldBeNil)
|
||||
t.T().Logf("%+v", media)
|
||||
})
|
||||
|
||||
Convey("get user bought medias", func() {
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
ids, err := svc.GetUserBoughtMedias(context.TODO(), 1, 1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
for _, id := range ids {
|
||||
So(lo.Contains([]int64{1, 2, 3}, id), ShouldBeTrue)
|
||||
}
|
||||
})
|
||||
FocusConvey("Not Bought", func() {
|
||||
hash := "f464a6641a60e2722e4042db8fad2813"
|
||||
media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, false)
|
||||
So(err, ShouldBeNil)
|
||||
t.T().Logf("%+v", media)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *ServiceTestSuite) Test_getMediaByHash() {
|
||||
Convey("Test_getMediaByHash", t.T(), func() {
|
||||
name, ext := path.SplitNameExt("0.ts")
|
||||
|
||||
So(name, ShouldEqual, "0")
|
||||
So(ext, ShouldEqual, "ts")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user