Ensure data dir is present

This commit is contained in:
Ben Sarmiento
2023-11-21 10:44:13 +01:00
parent cbf14e953f
commit f2922ea7b4
3 changed files with 22 additions and 4 deletions

View File

@@ -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 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 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 check_for_changes_every_secs: 15
info_cache_time_hours: 12 info_cache_time_hours: 12

View File

@@ -20,7 +20,10 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
const ALL_TORRENTS = "__all__" const (
ALL_TORRENTS = "__all__"
DATA_DIR = "data"
)
type TorrentManager struct { type TorrentManager struct {
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent 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{}, mu: &sync.Mutex{},
} }
ensureDir(DATA_DIR)
// create special directory // create special directory
t.DirectoryMap.Set("__all__", cmap.New[*Torrent]()) // key is AccessKey t.DirectoryMap.Set("__all__", cmap.New[*Torrent]()) // key is AccessKey
// create directory maps // create directory maps
@@ -414,7 +419,7 @@ func (t *TorrentManager) getName(name, originalName string) string {
} }
func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error { func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error {
filePath := "data/" + torrent.ID + ".bin" filePath := DATA_DIR + "/" + torrent.ID + ".bin"
file, err := os.Create(filePath) file, err := os.Create(filePath)
if err != nil { if err != nil {
return fmt.Errorf("failed creating file: %w", err) 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 { func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo {
filePath := "data/" + torrentID + ".bin" filePath := DATA_DIR + "/" + torrentID + ".bin"
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View File

@@ -2,6 +2,7 @@ package torrent
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
) )
@@ -22,3 +23,15 @@ func isStreamable(filePath string) bool {
strings.HasSuffix(filePath, ".wmv") || strings.HasSuffix(filePath, ".wmv") ||
strings.HasSuffix(filePath, ".m4v") 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
}