From 3b640be9d649333cecc86e1cf060d0772b888d0e Mon Sep 17 00:00:00 2001 From: Rogee Date: Wed, 4 Dec 2024 20:17:12 +0800 Subject: [PATCH] feat: add clean up media files task --- backend/modules/tasks/discover_medias.go | 41 +++++++++++++++---- backend/modules/tasks/discover_medias_test.go | 22 +++++++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/backend/modules/tasks/discover_medias.go b/backend/modules/tasks/discover_medias.go index efebc90..f435fb9 100644 --- a/backend/modules/tasks/discover_medias.go +++ b/backend/modules/tasks/discover_medias.go @@ -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) + } + } + } +} diff --git a/backend/modules/tasks/discover_medias_test.go b/backend/modules/tasks/discover_medias_test.go index e094d70..dd7455b 100644 --- a/backend/modules/tasks/discover_medias_test.go +++ b/backend/modules/tasks/discover_medias_test.go @@ -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) + } + }) +}