diff --git a/config.example.yml b/config.example.yml index 7df0327..49fbe02 100644 --- a/config.example.yml +++ b/config.example.yml @@ -5,7 +5,7 @@ token: YOUR_RD_API_TOKEN # https://real-debrid.com/apitoken host: "[::]" # do not change this if you are running it inside a docker container port: 9999 # do not change this if you are running it inside a docker container -concurrent_workers: 10 +concurrent_workers: 200 check_for_changes_every_secs: 15 info_cache_time_hours: 12 diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index f7dcb85..ce5817a 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -20,7 +20,10 @@ import ( "go.uber.org/zap" ) -const ALL_TORRENTS = "__all__" +const ( + ALL_TORRENTS = "__all__" + DATA_DIR = "data" +) type TorrentManager struct { DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent @@ -47,6 +50,8 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p mu: &sync.Mutex{}, } + ensureDir(DATA_DIR) + // create special directory t.DirectoryMap.Set("__all__", cmap.New[*Torrent]()) // key is AccessKey // create directory maps @@ -414,7 +419,7 @@ func (t *TorrentManager) getName(name, originalName string) string { } func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error { - filePath := "data/" + torrent.ID + ".bin" + filePath := DATA_DIR + "/" + torrent.ID + ".bin" file, err := os.Create(filePath) if err != nil { return fmt.Errorf("failed creating file: %w", err) @@ -433,7 +438,7 @@ func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error { } func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo { - filePath := "data/" + torrentID + ".bin" + filePath := DATA_DIR + "/" + torrentID + ".bin" file, err := os.Open(filePath) if err != nil { if os.IsNotExist(err) { diff --git a/internal/torrent/util.go b/internal/torrent/util.go index a97218c..2e3fe79 100644 --- a/internal/torrent/util.go +++ b/internal/torrent/util.go @@ -2,6 +2,7 @@ package torrent import ( "fmt" + "os" "strings" ) @@ -22,3 +23,15 @@ func isStreamable(filePath string) bool { strings.HasSuffix(filePath, ".wmv") || strings.HasSuffix(filePath, ".m4v") } + +func ensureDir(dirName string) error { + if _, err := os.Stat(dirName); os.IsNotExist(err) { + err := os.Mkdir(dirName, 0755) + if err != nil { + return err + } + } else if err != nil { + return err + } + return nil +}