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)
}
}
}
}