42 lines
812 B
Go
42 lines
812 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"backend/modules/medias"
|
|
"backend/pkg/media_store"
|
|
|
|
"github.com/pkg/errors"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type StoreMedias struct {
|
|
mediasSvc *medias.Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
// Prepare
|
|
func (d *StoreMedias) Prepare() error {
|
|
d.log = log.WithField("module", "StoreMedias")
|
|
return nil
|
|
}
|
|
|
|
func (d *StoreMedias) RunE(targetPath string) error {
|
|
d.log.Infof("Store medias from: %s ", targetPath)
|
|
|
|
store, err := media_store.NewStore(targetPath)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "new store: %s", targetPath)
|
|
}
|
|
|
|
for _, item := range store {
|
|
err := d.mediasSvc.Upsert(context.Background(), 1, item)
|
|
if err != nil {
|
|
d.log.WithError(err).Errorf("upsert media: %s - %s", item.Hash, item.Name)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|