Introduce components

This commit is contained in:
Ben Sarmiento
2024-05-20 20:43:19 +02:00
parent ab81eb5f39
commit a3a24124a8
11 changed files with 222 additions and 202 deletions

View File

@@ -18,8 +18,7 @@ import (
)
const (
INT_ALL = "int__all__"
INT_INFO_CACHE = "int__info__"
INT_ALL = "int__all__"
)
type TorrentManager struct {
@@ -41,7 +40,6 @@ type TorrentManager struct {
latestState *LibraryState
allAccessKeys mapset.Set[string]
allIDs mapset.Set[string]
fixers cmap.ConcurrentMap[string, string] // trigger -> [command, id]
repairTrigger chan *Torrent
@@ -55,7 +53,7 @@ type TorrentManager struct {
// and store them in-memory and cached in files
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool *ants.Pool, log *logutil.Logger) *TorrentManager {
t := &TorrentManager{
requiredVersion: "0.9.3-hotfix.10",
requiredVersion: "0.10.0",
Config: cfg,
api: api,
@@ -73,7 +71,6 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
latestState: &LibraryState{},
allAccessKeys: mapset.NewSet[string](),
allIDs: mapset.NewSet[string](),
}
t.fixers = t.readFixersFromFile()
@@ -140,8 +137,10 @@ func (t *TorrentManager) GetPath(file *File) string {
return filename
}
func (t *TorrentManager) writeTorrentToFile(instanceID string, torrent *Torrent) {
filePath := "data/" + instanceID + ".json"
/// torrent functions
func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
filePath := "data/" + torrent.Hash + ".json"
file, err := os.Create(filePath)
if err != nil {
t.log.Warnf("Cannot create file %s: %v", filePath, err)
@@ -162,11 +161,11 @@ func (t *TorrentManager) writeTorrentToFile(instanceID string, torrent *Torrent)
return
}
t.log.Debugf("Saved torrent %s to file", instanceID)
t.log.Debugf("Saved torrent %s to file", torrent.Hash)
}
func (t *TorrentManager) readTorrentFromFile(torrentID string) *Torrent {
filePath := "data/" + torrentID + ".json"
func (t *TorrentManager) readTorrentFromFile(hash string) *Torrent {
filePath := "data/" + hash + ".json"
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
@@ -183,7 +182,7 @@ func (t *TorrentManager) readTorrentFromFile(torrentID string) *Torrent {
if err := json.Unmarshal(jsonData, &torrent); err != nil {
return nil
}
if torrent.DownloadedIDs.Union(torrent.InProgressIDs).IsEmpty() {
if len(torrent.Components) == 0 {
t.log.Fatal("Torrent has no downloaded or in progress ids")
}
if torrent.Version != t.requiredVersion {
@@ -192,14 +191,72 @@ func (t *TorrentManager) readTorrentFromFile(torrentID string) *Torrent {
return torrent
}
func (t *TorrentManager) deleteTorrentFile(torrentID string) {
filePath := "data/" + torrentID + ".json"
func (t *TorrentManager) deleteTorrentFile(hash string) {
filePath := "data/" + hash + ".json"
err := os.Remove(filePath)
if err != nil {
t.log.Warnf("Cannot delete file %s: %v", filePath, err)
}
}
/// end torrent functions
/// info functions
func (t *TorrentManager) writeInfoToFile(info *realdebrid.TorrentInfo) {
filePath := "data/" + info.ID + ".info"
file, err := os.Create(filePath)
if err != nil {
t.log.Warnf("Cannot create info file %s: %v", filePath, err)
return
}
defer file.Close()
jsonData, err := json.Marshal(info)
if err != nil {
t.log.Warnf("Cannot marshal torrent info: %v", err)
return
}
if _, err := file.Write(jsonData); err != nil {
t.log.Warnf("Cannot write to info file %s: %v", filePath, err)
return
}
t.log.Debugf("Saved torrent %s to info file", info.ID)
}
func (t *TorrentManager) readInfoFromFile(torrentID string) *realdebrid.TorrentInfo {
filePath := "data/" + torrentID + ".info"
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return nil
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
return nil
}
var info *realdebrid.TorrentInfo
if err := json.Unmarshal(jsonData, &info); err != nil {
return nil
}
return info
}
func (t *TorrentManager) deleteInfoFile(torrentID string) {
filePath := "data/" + torrentID + ".info"
err := os.Remove(filePath)
if err != nil {
t.log.Warnf("Cannot delete info file %s: %v", filePath, err)
}
}
/// end info functions
func (t *TorrentManager) mountDownloads() {
if !t.Config.EnableDownloadMount() {
return
@@ -246,26 +303,10 @@ func (t *TorrentManager) StartDownloadsJob() {
func (t *TorrentManager) initializeDirectories() {
// create internal directories
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()
t.DirectoryMap.Set(INT_INFO_CACHE, cmap.New[*Torrent]()) // key is Torrent ID
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()
// create directory maps
for _, directory := range t.Config.GetDirectories() {
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
// t.RootNode.AddChild(fs.NewFileNode(directory, true))
}
}
func (t *TorrentManager) saveTorrentChangesToDisk(torrent *Torrent, cb func(*Torrent)) {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Union(torrent.InProgressIDs).Each(func(id string) bool {
info, exists := infoCache.Get(id)
if !exists {
return false
}
if cb != nil {
cb(info)
}
t.writeTorrentToFile(id, info)
return false
})
}