rework exclude repository
This commit is contained in:
parent
0332206560
commit
cd6cf8dd74
4 changed files with 22 additions and 29 deletions
|
@ -73,10 +73,7 @@ func run(configPath string) error {
|
||||||
log.Err(err).Msg("set priority failed")
|
log.Err(err).Msg("set priority failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(filepath.Join(conf.TorrentClient.MetadataFolder, "meta"), 0744); err != nil {
|
rep, err := repository.NewTorrentMetaRepository(conf.TorrentClient.MetadataFolder)
|
||||||
return fmt.Errorf("error creating metadata folder: %w", err)
|
|
||||||
}
|
|
||||||
rep, err := repository.NewTorrentMetaRepository(filepath.Join(conf.TorrentClient.MetadataFolder, "meta"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/anacrolix/torrent/metainfo"
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
|
@ -10,22 +11,23 @@ import (
|
||||||
"github.com/philippgille/gokv/encoding"
|
"github.com/philippgille/gokv/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TorrentMetaRepository interface {
|
type TorrentsRepository interface {
|
||||||
ExcludeFile(hash metainfo.Hash, file ...string) error
|
ExcludeFile(hash metainfo.Hash, file ...string) error
|
||||||
ExcludedFiles(hash metainfo.Hash) ([]string, error)
|
ExcludedFiles(hash metainfo.Hash) ([]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTorrentMetaRepository(dir string) (TorrentMetaRepository, error) {
|
func NewTorrentMetaRepository(dir string) (TorrentsRepository, error) {
|
||||||
store, err := badgerdb.NewStore(badgerdb.Options{
|
excludedFilesStore, err := badgerdb.NewStore(badgerdb.Options{
|
||||||
Dir: dir,
|
Dir: filepath.Join(dir, "excluded-files"),
|
||||||
Codec: encoding.JSON,
|
Codec: encoding.JSON,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := &torrentRepositoryImpl{
|
r := &torrentRepositoryImpl{
|
||||||
store: store,
|
excludedFiles: excludedFilesStore,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
|
@ -33,11 +35,7 @@ func NewTorrentMetaRepository(dir string) (TorrentMetaRepository, error) {
|
||||||
|
|
||||||
type torrentRepositoryImpl struct {
|
type torrentRepositoryImpl struct {
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
store gokv.Store
|
excludedFiles gokv.Store
|
||||||
}
|
|
||||||
|
|
||||||
type torrentMeta struct {
|
|
||||||
ExludedFiles []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNotFound = errors.New("not found")
|
var ErrNotFound = errors.New("not found")
|
||||||
|
@ -46,27 +44,25 @@ func (r *torrentRepositoryImpl) ExcludeFile(hash metainfo.Hash, file ...string)
|
||||||
r.m.Lock()
|
r.m.Lock()
|
||||||
defer r.m.Unlock()
|
defer r.m.Unlock()
|
||||||
|
|
||||||
var meta torrentMeta
|
var excludedFiles []string
|
||||||
found, err := r.store.Get(hash.AsString(), &meta)
|
found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
meta = torrentMeta{
|
excludedFiles = []string{}
|
||||||
ExludedFiles: file,
|
|
||||||
}
|
}
|
||||||
}
|
excludedFiles = unique(append(excludedFiles, file...))
|
||||||
meta.ExludedFiles = unique(append(meta.ExludedFiles, file...))
|
|
||||||
|
|
||||||
return r.store.Set(hash.AsString(), meta)
|
return r.excludedFiles.Set(hash.AsString(), excludedFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, error) {
|
func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, error) {
|
||||||
r.m.Lock()
|
r.m.Lock()
|
||||||
defer r.m.Unlock()
|
defer r.m.Unlock()
|
||||||
|
|
||||||
var meta torrentMeta
|
var excludedFiles []string
|
||||||
found, err := r.store.Get(hash.AsString(), &meta)
|
found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -74,7 +70,7 @@ func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, err
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return meta.ExludedFiles, nil
|
return excludedFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func unique[C comparable](intSlice []C) []C {
|
func unique[C comparable](intSlice []C) []C {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
c *torrent.Client
|
c *torrent.Client
|
||||||
rep repository.TorrentMetaRepository
|
rep repository.TorrentsRepository
|
||||||
|
|
||||||
// stats *Stats
|
// stats *Stats
|
||||||
DefaultPriority types.PiecePriority
|
DefaultPriority types.PiecePriority
|
||||||
|
@ -24,7 +24,7 @@ type Service struct {
|
||||||
addTimeout, readTimeout int
|
addTimeout, readTimeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(c *torrent.Client, rep repository.TorrentMetaRepository, addTimeout, readTimeout int) *Service {
|
func NewService(c *torrent.Client, rep repository.TorrentsRepository, addTimeout, readTimeout int) *Service {
|
||||||
l := slog.With("component", "torrent-service")
|
l := slog.With("component", "torrent-service")
|
||||||
return &Service{
|
return &Service{
|
||||||
log: l,
|
log: l,
|
||||||
|
|
|
@ -21,7 +21,7 @@ var _ Filesystem = &TorrentFs{}
|
||||||
type TorrentFs struct {
|
type TorrentFs struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
t *torrent.Torrent
|
t *torrent.Torrent
|
||||||
rep repository.TorrentMetaRepository
|
rep repository.TorrentsRepository
|
||||||
|
|
||||||
readTimeout int
|
readTimeout int
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ type TorrentFs struct {
|
||||||
resolver *resolver
|
resolver *resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentMetaRepository, readTimeout int) *TorrentFs {
|
func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentsRepository, readTimeout int) *TorrentFs {
|
||||||
return &TorrentFs{
|
return &TorrentFs{
|
||||||
t: t,
|
t: t,
|
||||||
rep: rep,
|
rep: rep,
|
||||||
|
|
Loading…
Reference in a new issue