update
This commit is contained in:
parent
35913e0190
commit
6a1e338af4
34 changed files with 1900 additions and 355 deletions
src/host/store
57
src/host/store/file-mappings.go
Normal file
57
src/host/store/file-mappings.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue