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

@@ -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) {