mirror of
https://github.com/sot-tech/mochi.git
synced 2026-04-27 16:10:00 -07:00
WIP Add support for custom torrents' approval storages
* migrate torrentapproval to list storage * add initial support for torrent file storage (watch directory with fsnotify) * replace frontend/http/bencode package with github.com/zeebo/bencode module * sanitize code (fix warnings) TODO: * parse torrent files to get hashes, * watch directory event types DON'T use for now
This commit is contained in:
53
middleware/torrentapproval/container/container.go
Normal file
53
middleware/torrentapproval/container/container.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/chihaya/chihaya/bittorrent"
|
||||
"github.com/chihaya/chihaya/pkg/stop"
|
||||
"gopkg.in/yaml.v2"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Builder interface {
|
||||
New() (Container, error)
|
||||
}
|
||||
|
||||
var (
|
||||
buildersMU sync.Mutex
|
||||
builders = make(map[string]Builder)
|
||||
|
||||
ErrContainerDoesNotExist = errors.New("torrent hash container with that name does not exist")
|
||||
)
|
||||
|
||||
func Register(n string, c Builder) {
|
||||
if len(n) == 0 {
|
||||
panic("middleware: could not register a Container with an empty name")
|
||||
}
|
||||
if c == nil {
|
||||
panic("middleware: could not register a Container with nil builder")
|
||||
}
|
||||
|
||||
buildersMU.Lock()
|
||||
defer buildersMU.Unlock()
|
||||
builders[n] = c
|
||||
}
|
||||
|
||||
type Container interface {
|
||||
stop.Stopper
|
||||
Contains(bittorrent.InfoHash) bool
|
||||
}
|
||||
|
||||
func GetContainer(name string, confBytes []byte) (Container, error) {
|
||||
buildersMU.Lock()
|
||||
defer buildersMU.Unlock()
|
||||
var err error
|
||||
var cn Container
|
||||
if builder, exist := builders[name]; !exist {
|
||||
err = ErrContainerDoesNotExist
|
||||
} else {
|
||||
if err = yaml.Unmarshal(confBytes, &cn); err == nil {
|
||||
cn, err = builder.New()
|
||||
}
|
||||
}
|
||||
return cn, err
|
||||
}
|
||||
Reference in New Issue
Block a user