refactor torrent

This commit is contained in:
royalcat 2024-05-20 00:24:09 +03:00
parent 99cdd5471e
commit 991c15fdef
33 changed files with 223 additions and 275 deletions
src/host/store

View file

@ -1,61 +0,0 @@
package store
import (
"context"
"errors"
"path/filepath"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/types/infohash"
"github.com/royalcat/kv"
)
func NewFileMappings(metaDir string, storage TorrentFileDeleter) (*FilesMappings, error) {
str, err := kv.NewBadgerKVBytes[string, string](filepath.Join(metaDir, "file-mappings"))
if err != nil {
return nil, err
}
r := &FilesMappings{
mappings: str,
storage: storage,
}
return r, nil
}
type FilesMappings struct {
mappings kv.Store[string, string]
storage TorrentFileDeleter
}
var ErrNotFound = errors.New("not found")
type TorrentFileDeleter interface {
DeleteFile(file *torrent.File) error
}
func fileKey(file *torrent.File) string {
return file.Torrent().InfoHash().HexString() + "/" + file.Path()
}
func (r *FilesMappings) MapFile(ctx context.Context, file *torrent.File, target string) error {
return r.mappings.Set(ctx, fileKey(file), target)
}
func (r *FilesMappings) ExcludeFile(ctx context.Context, file *torrent.File) error {
return r.mappings.Set(ctx, fileKey(file), "")
}
func (r *FilesMappings) FileMappings(ctx context.Context, ih infohash.T) (map[string]string, error) {
out := map[string]string{}
err := r.mappings.RangeWithPrefix(ctx, ih.HexString(), func(k, v string) bool {
out[k] = v
return true
})
return out, err
}
func (r *FilesMappings) Close(ctx context.Context) error {
return r.mappings.Close(ctx)
}