fix: play segments

This commit is contained in:
Rogee
2024-12-28 16:05:25 +08:00
parent dae2941168
commit cee14cb3fd
5 changed files with 22 additions and 22 deletions

View File

@@ -83,6 +83,8 @@ func (c *Controller) MediaIndex(ctx fiber.Ctx) error {
return errorx.RequestUnAuthorized return errorx.RequestUnAuthorized
} }
// c.Locals(consts.CtxKeyJwt, token)
token := fiber.Locals[string](ctx, consts.CtxKeyJwt)
model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash) model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash)
if err != nil { if err != nil {
log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaByHash") log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaByHash")
@@ -95,7 +97,7 @@ func (c *Controller) MediaIndex(ctx fiber.Ctx) error {
return err return err
} }
playlist, err := c.svc.GetM3U8(ctx.Context(), claim.TenantID, mediaType, model.Hash, bought) playlist, err := c.svc.GetM3U8(ctx.Context(), claim.TenantID, mediaType, model.Hash, bought, token)
if err != nil { if err != nil {
log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaPlaylist") log.WithField("action", "medias.MediaIndex").WithError(err).Error("GetMediaPlaylist")
return err return err

View File

@@ -279,7 +279,7 @@ func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store
} }
// get video m3u8 // get video m3u8
func (svc *Service) GetM3U8(ctx context.Context, tenantId int64, types pg.MediaType, hash string, bought bool) (m3u8.Playlist, error) { func (svc *Service) GetM3U8(ctx context.Context, tenantId int64, types pg.MediaType, hash string, bought bool, token string) (m3u8.Playlist, error) {
log := svc.log.WithField("method", "GetM3U8") log := svc.log.WithField("method", "GetM3U8")
indexPath := filepath.Join(svc.storageConfig.Path, fmt.Sprintf("%d", tenantId), hash, types.String(), "index.m3u8") indexPath := filepath.Join(svc.storageConfig.Path, fmt.Sprintf("%d", tenantId), hash, types.String(), "index.m3u8")
log.Infof("m3u8 path: %s", indexPath) log.Infof("m3u8 path: %s", indexPath)
@@ -335,6 +335,9 @@ func (svc *Service) GetM3U8(ctx context.Context, tenantId int64, types pg.MediaT
return nil, errors.Wrap(err, "encode hash id") return nil, errors.Wrap(err, "encode hash id")
} }
seg.URI = fmt.Sprintf("%s/%s.%s", types, hashID, ext) seg.URI = fmt.Sprintf("%s/%s.%s", types, hashID, ext)
if token != "" {
seg.URI += fmt.Sprintf("?token=%s", token)
}
} }
return media, nil return media, nil

View File

@@ -45,15 +45,15 @@ func Test_DiscoverMedias(t *testing.T) {
func (t *ServiceTestSuite) Test_getM3U8() { func (t *ServiceTestSuite) Test_getM3U8() {
FocusConvey("Test_ffmpegVideoToM3U8", t.T(), func() { FocusConvey("Test_ffmpegVideoToM3U8", t.T(), func() {
Convey("Bought", func() { Convey("Bought", func() {
hash := "f464a6641a60e2722e4042db8fad2813" hash := "907f1c8fd92704233600ae54a1d75092"
media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, true) media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, true, "hello")
So(err, ShouldBeNil) So(err, ShouldBeNil)
t.T().Logf("%+v", media) t.T().Logf("%+v", media)
}) })
FocusConvey("Not Bought", func() { FocusConvey("Not Bought", func() {
hash := "f464a6641a60e2722e4042db8fad2813" hash := "907f1c8fd92704233600ae54a1d75092"
media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, false) media, err := t.Svc.GetM3U8(context.Background(), 1, pg.MediaTypeVideo, hash, false, "hello")
So(err, ShouldBeNil) So(err, ShouldBeNil)
t.T().Logf("%+v", media) t.T().Logf("%+v", media)
}) })

View File

@@ -13,7 +13,11 @@ import (
func (f *Middlewares) ParseJWT(c fiber.Ctx) error { func (f *Middlewares) ParseJWT(c fiber.Ctx) error {
tokens := c.GetReqHeaders()["Authorization"] tokens := c.GetReqHeaders()["Authorization"]
if len(tokens) == 0 { if len(tokens) == 0 {
return c.Next() queryToken := c.Query("token")
tokens = []string{queryToken}
if len(tokens) == 0 {
return c.Next()
}
} }
token := tokens[0] token := tokens[0]

View File

@@ -129,11 +129,11 @@ const loadMedia = (hash) => {
const play = (hash, type) => { const play = (hash, type) => {
const player = document.getElementById("video"); const player = document.getElementById("video");
const source = `/v1/medias/${hash}/${type}`; const source = `/v1/medias/${hash}/${type}?token=${__GA}`;
if (Hls.isSupported()) { if (Hls.isSupported()) {
var hls = new Hls({ var hls = new Hls({
xhrSetup: function (xhr, url) { xhrSetup: function (xhr, url) {
xhr.setRequestHeader("Authorization", "Bearer " + __GA); xhr.withCredentials = true;
}, },
}); });
hls.loadSource(source); hls.loadSource(source);
@@ -142,19 +142,10 @@ const play = (hash, type) => {
player.play(); player.play();
}); });
} else if (player.canPlayType("application/vnd.apple.mpegurl")) { } else if (player.canPlayType("application/vnd.apple.mpegurl")) {
var xhr = new XMLHttpRequest(); player.src = source;
xhr.open('GET', source, true); player.addEventListener("loadedmetadata", function () {
xhr.setRequestHeader("Authorization", "Bearer " + __GA); player.play();
xhr.responseType = 'blob'; });
xhr.onload = function () {
if (xhr.status === 200) {
player.src = URL.createObjectURL(xhr.response);
player.addEventListener("loadedmetadata", function () {
player.play();
});
}
};
xhr.send();
} }
}; };
</script> </script>