feat: remove v1
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"backend/providers/http"
|
||||
"database/sql"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"backend/modules/users"
|
||||
"backend/providers/jwt"
|
||||
"backend/providers/wechat"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
@@ -10,9 +12,13 @@ import (
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
client *wechat.Client,
|
||||
jwt *jwt.JWT,
|
||||
userSvc *users.Service,
|
||||
) (*Middlewares, error) {
|
||||
obj := &Middlewares{
|
||||
client: client,
|
||||
client: client,
|
||||
jwt: jwt,
|
||||
userSvc: userSvc,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
|
||||
140
backend/modules/tasks/discover_medias.go
Normal file
140
backend/modules/tasks/discover_medias.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"backend/modules/medias"
|
||||
|
||||
"github.com/etherlabsio/go-m3u8/m3u8"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
hashids "github.com/speps/go-hashids/v2"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type DiscoverMedias struct {
|
||||
hashId *hashids.HashID
|
||||
mediasSvc *medias.Service
|
||||
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) Prepare() error {
|
||||
d.log = log.WithField("module", "DiscoverMedias")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) getHashID(ids ...int64) (string, error) {
|
||||
return d.hashId.EncodeInt64(ids)
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) getSubDirs(root string) ([]string, error) {
|
||||
fd, err := os.Open(root)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "open root directory: %s", root)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
entries, err := fd.Readdir(-1)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "read root directory: %s", root)
|
||||
}
|
||||
|
||||
var paths []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
paths = append(paths, entry.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) globVideo(root string) []string {
|
||||
files := []string{}
|
||||
|
||||
exts := []string{"*.mp4", "*.MP4", "*.mov", "*.MOV"}
|
||||
|
||||
for _, ext := range exts {
|
||||
fs, _ := filepath.Glob(filepath.Join(root, ext))
|
||||
files = append(files, fs...)
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) globAudio(root string) []string {
|
||||
files := []string{}
|
||||
|
||||
exts := []string{"*.mp3", "*.MP3", "*.wav", "*.WAV"}
|
||||
|
||||
for _, ext := range exts {
|
||||
fs, _ := filepath.Glob(filepath.Join(root, ext))
|
||||
files = append(files, fs...)
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
||||
// ensureDirectory ensure the directory exists
|
||||
func (d *DiscoverMedias) ensureDirectory(dir string) error {
|
||||
st, err := os.Stat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return errors.Wrapf(err, "mkdir: %s", dir)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if !st.IsDir() {
|
||||
os.RemoveAll(dir)
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return errors.Wrapf(err, "mkdir: %s", dir)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiscoverMedias) ffmpegVideoToM3U8(input string, output string) error {
|
||||
output = filepath.Join(output, "video")
|
||||
if err := d.ensureDirectory(output); err != nil {
|
||||
return errors.Wrapf(err, "ensure directory: %s", output)
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-i", input,
|
||||
"-c:v", "libx264",
|
||||
"-c:a", "aac",
|
||||
"-strict",
|
||||
"-2",
|
||||
"-f", "hls",
|
||||
"-hls_time", "10",
|
||||
"-hls_list_size", "0",
|
||||
"-hls_segment_filename", filepath.Join(output, "%d.ts"),
|
||||
filepath.Join(output, "index.m3u8"),
|
||||
}
|
||||
|
||||
log.Infof("cmd: ffmpeg %s", strings.Join(args, " "))
|
||||
logs, err := exec.Command("ffmpeg", args...).Output()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "ffmpeg video to m3u8: %s", input)
|
||||
}
|
||||
|
||||
d.log.Infof("ffmpeg video to m3u8: %s", logs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseM3u8File parse the m3u8 file
|
||||
func (d *DiscoverMedias) parseM3u8File(file string) (*m3u8.Playlist, error) {
|
||||
platList, err := m3u8.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "read m3u8 file:%s", file)
|
||||
}
|
||||
|
||||
return platList, nil
|
||||
}
|
||||
84
backend/modules/tasks/discover_medias_test.go
Normal file
84
backend/modules/tasks/discover_medias_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"backend/common/service/testx"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type DiscoverMediasInjectParams struct {
|
||||
dig.In
|
||||
Svc *DiscoverMedias
|
||||
}
|
||||
|
||||
type DiscoverMediasTestSuite struct {
|
||||
suite.Suite
|
||||
DiscoverMediasInjectParams
|
||||
}
|
||||
|
||||
func Test_DiscoverMedias(t *testing.T) {
|
||||
providers := testx.Default().With(
|
||||
Provide,
|
||||
)
|
||||
|
||||
testx.Serve(providers, t, func(params DiscoverMediasInjectParams) {
|
||||
suite.Run(t, &DiscoverMediasTestSuite{DiscoverMediasInjectParams: params})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_getHashID() {
|
||||
Convey("TestDiscoverMedias_getHashID", t.T(), func() {
|
||||
hashId, err := t.Svc.getHashID(10, 11, 12)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.T().Logf("HashID: %s", hashId)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_getSubDirs() {
|
||||
Convey("Test_getSubDirs", t.T(), func() {
|
||||
dirs, err := t.Svc.getSubDirs("/mnt/yangpingliang/动态曲谱")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.T().Logf("Dirs: %+v", dirs)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_getVideo() {
|
||||
Convey("Test_getVideo", t.T(), func() {
|
||||
dirs := t.Svc.globVideo("/mnt/yangpingliang/动态曲谱/河北梆子伴奏《李慧娘》依然还我生前摸样")
|
||||
t.T().Logf("Dirs: %+v", dirs)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_getAudio() {
|
||||
Convey("Test_getAudio", t.T(), func() {
|
||||
dirs := t.Svc.globAudio("/mnt/yangpingliang/动态曲谱/河北梆子伴奏《李慧娘》依然还我生前摸样")
|
||||
t.T().Logf("Dirs: %+v", dirs)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_ffmpegVideoToM3U8() {
|
||||
Convey("Test_ffmpegVideoToM3U8", t.T(), func() {
|
||||
output := "/projects/mp-qvyun/backend/fixtures/medias/abc"
|
||||
os.RemoveAll(output)
|
||||
|
||||
err := t.Svc.ffmpegVideoToM3U8("/projects/mp-qvyun/backend/fixtures/medias/video.mp4", output)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DiscoverMediasTestSuite) Test_parseM3u8File() {
|
||||
Convey("TestDiscoverMedias_parseM3u8File", t.T(), func() {
|
||||
file := "/projects/mp-qvyun/backend/fixtures/medias/abc/video/index.m3u8"
|
||||
playlist, err := t.Svc.parseM3u8File(file)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.T().Logf("ListType: %+v", playlist)
|
||||
})
|
||||
}
|
||||
22
backend/modules/tasks/provider.gen.go
Executable file
22
backend/modules/tasks/provider.gen.go
Executable file
@@ -0,0 +1,22 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"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(
|
||||
hashId *hashids.HashID,
|
||||
) (*DiscoverMedias, error) {
|
||||
obj := &DiscoverMedias{
|
||||
hashId: hashId,
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user