feat: add clean up media files task

This commit is contained in:
Rogee
2024-12-04 20:17:12 +08:00
parent 975eba4183
commit 3b640be9d6
2 changed files with 54 additions and 9 deletions

View File

@@ -11,13 +11,19 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
)
// @provider
type DiscoverMedias struct {
mediasSvc *medias.Service
log *log.Entry
log *log.Entry `inject:"false"`
}
type MediaUuidMap struct {
UUID string
Name string
}
// Prepare
@@ -47,16 +53,14 @@ func (d *DiscoverMedias) RunE(from, to string) error {
return errors.Wrapf(err, "read file: %s", mapFile)
}
type mediaUuidMap struct {
UUID string
Name string
}
var store []mediaUuidMap
var store []MediaUuidMap
err = json.Unmarshal(b, &store)
if err != nil {
return errors.Wrapf(err, "unmarshal json: %s", mapFile)
}
defer func() {
d.runCleanup(to, store)
}()
storeMap := make(map[string]string)
for _, item := range store {
@@ -99,7 +103,7 @@ func (d *DiscoverMedias) RunE(from, to string) error {
}
}
store = append(store, mediaUuidMap{
store = append(store, MediaUuidMap{
UUID: uuid,
Name: dir,
})
@@ -242,3 +246,24 @@ func (d *DiscoverMedias) ffmpegAudioToM3U8(input string, output string) error {
d.log.Infof("ffmpeg audio to m3u8: %s", logs)
return nil
}
func (d *DiscoverMedias) runCleanup(to string, store []MediaUuidMap) {
uuids := lo.Map(store, func(item MediaUuidMap, _ int) string {
return item.UUID
})
dirs, err := d.getSubDirs(to)
if err != nil {
d.log.Errorf("get sub dirs: %s", to)
return
}
for _, dir := range dirs {
if !lo.Contains(uuids, dir) {
d.log.Infof("Remove dir: %s", dir)
if err := os.RemoveAll(filepath.Join(to, dir)); err != nil {
d.log.Errorf("Remove dir: %s", dir)
}
}
}
}

View File

@@ -5,6 +5,8 @@ import (
"testing"
"backend/common/service/testx"
"backend/modules/medias"
"backend/providers/postgres"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite"
@@ -22,8 +24,11 @@ type DiscoverMediasTestSuite struct {
}
func Test_DiscoverMedias(t *testing.T) {
providers := testx.Default().With(
providers := testx.Default(
postgres.DefaultProvider(),
).With(
Provide,
medias.Provide,
)
testx.Serve(providers, t, func(params DiscoverMediasInjectParams) {
@@ -63,3 +68,18 @@ func (t *DiscoverMediasTestSuite) Test_ffmpegVideoToM3U8() {
So(err, ShouldBeNil)
})
}
func (t *DiscoverMediasTestSuite) Test_item() {
Convey("Test_item", t.T(), func() {
items := []int{}
defer func() {
for _, item := range items {
t.T().Logf("Recovered in f %d", item)
}
}()
for i := 0; i < 4; i++ {
items = append(items, i)
}
})
}