fix: issues
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user