From a681912fc10599d854c88a9bef5115b5d8c4c463 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 25 May 2024 01:54:35 +0200 Subject: [PATCH] Add bins to home page --- internal/handlers/home.go | 47 ++++++++++++++++++++++++------------- internal/torrent/manager.go | 4 ++-- internal/torrent/refresh.go | 40 +++++++++++++++---------------- 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/internal/handlers/home.go b/internal/handlers/home.go index 2df3748..ca88566 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -10,6 +10,7 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/pkg/realdebrid" + mapset "github.com/deckarep/golang-set/v2" ) type SponsorResponse struct { @@ -18,21 +19,23 @@ type SponsorResponse struct { Paypal string `json:"paypal"` } type RootResponse struct { - Version string `json:"version"` - BuiltAt string `json:"built_at"` - GitCommit string `json:"git_commit"` - Html string `json:"html"` - Dav string `json:"dav"` - Infuse string `json:"infuse"` - Logs string `json:"logs"` - UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type - MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB - TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB - Sys uint64 `json:"sys"` // System memory in MB - NumGC uint32 `json:"num_gc"` // Number of completed GC cycles - PID int `json:"pid"` // Process ID - Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links - Config config.ZurgConfig `json:"config"` + Version string `json:"version"` + BuiltAt string `json:"built_at"` + GitCommit string `json:"git_commit"` + Html string `json:"html"` + Dav string `json:"dav"` + Infuse string `json:"infuse"` + Logs string `json:"logs"` + UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type + MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB + TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB + Sys uint64 `json:"sys"` // System memory in MB + NumGC uint32 `json:"num_gc"` // Number of completed GC cycles + PID int `json:"pid"` // Process ID + Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links + Config config.ZurgConfig `json:"config"` + ImmediateBin mapset.Set[string] `json:"immediate_bin"` + OnceDoneBin mapset.Set[string] `json:"once_done_bin"` } func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { @@ -64,7 +67,9 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { Github: "https://github.com/sponsors/debridmediamanager", Paypal: "https://paypal.me/yowmamasita", }, - Config: zr.cfg.GetConfig(), + Config: zr.cfg.GetConfig(), + ImmediateBin: zr.torMgr.ImmediateBin, + OnceDoneBin: zr.torMgr.OnceDoneBin, } out := ` @@ -244,6 +249,14 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { + + + + + + + + @@ -321,6 +334,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { response.Config.ShouldServeFromRclone(), response.Config.ShouldForceIPv6(), response.Config.GetOnLibraryUpdate(), + response.ImmediateBin.String(), + response.OnceDoneBin.String(), ) fmt.Fprint(resp, out) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index f11a449..46254c3 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -45,8 +45,8 @@ type TorrentManager struct { repairRunning bool repairRunningMu sync.Mutex - immediateBin mapset.Set[string] - onceDoneBin mapset.Set[string] + ImmediateBin mapset.Set[string] + OnceDoneBin mapset.Set[string] } // NewTorrentManager creates a new torrent manager diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 091f13d..c42fec8 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -412,16 +412,16 @@ func (t *TorrentManager) IsPlayable(filePath string) bool { func (t *TorrentManager) initializeBins() { if _, err := os.Stat("data/bins.json"); os.IsNotExist(err) { t.log.Warn("data/bins.json does not exist. Initializing empty bins.") - t.immediateBin = mapset.NewSet[string]() - t.onceDoneBin = mapset.NewSet[string]() + t.ImmediateBin = mapset.NewSet[string]() + t.OnceDoneBin = mapset.NewSet[string]() return } fileData, err := os.ReadFile("data/bins.json") if err != nil { t.log.Errorf("Failed to read bins.json file: %v", err) - t.immediateBin = mapset.NewSet[string]() - t.onceDoneBin = mapset.NewSet[string]() + t.ImmediateBin = mapset.NewSet[string]() + t.OnceDoneBin = mapset.NewSet[string]() return } @@ -433,44 +433,44 @@ func (t *TorrentManager) initializeBins() { return } - t.immediateBin = mapset.NewSet[string](data["trash_bin"]...) - t.onceDoneBin = mapset.NewSet[string](data["repair_bin"]...) + t.ImmediateBin = mapset.NewSet[string](data["trash_bin"]...) + t.OnceDoneBin = mapset.NewSet[string](data["repair_bin"]...) t.log.Debug("Successfully read bins from bins.json") - t.log.Debugf("Bin immediately: %v", t.immediateBin.ToSlice()) - t.log.Debugf("Bin once done: %v", t.onceDoneBin.ToSlice()) + t.log.Debugf("Bin immediately: %v", t.ImmediateBin.ToSlice()) + t.log.Debugf("Bin once done: %v", t.OnceDoneBin.ToSlice()) } func (t *TorrentManager) setToBinImmediately(torrentId string) { t.log.Debugf("Set to delete immediately: %s", torrentId) - t.immediateBin.Add(torrentId) + t.ImmediateBin.Add(torrentId) t.persistBins() } func (t *TorrentManager) setToBinOnceDone(torrentId string) { t.log.Debugf("Set to delete once completed: %s", torrentId) - t.onceDoneBin.Add(torrentId) + t.OnceDoneBin.Add(torrentId) t.persistBins() } func (t *TorrentManager) binImmediately(torrentId string) bool { - if t.immediateBin.Contains(torrentId) { + if t.ImmediateBin.Contains(torrentId) { if err := t.api.DeleteTorrent(torrentId); err != nil { t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err) return false } - t.immediateBin.Remove(torrentId) + t.ImmediateBin.Remove(torrentId) return true } return false } func (t *TorrentManager) binOnceDone(torrentId string) bool { - if t.onceDoneBin.Contains(torrentId) { + if t.OnceDoneBin.Contains(torrentId) { if err := t.api.DeleteTorrent(torrentId); err != nil { t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err) return false } - t.onceDoneBin.Remove(torrentId) + t.OnceDoneBin.Remove(torrentId) return true } return false @@ -478,8 +478,8 @@ func (t *TorrentManager) binOnceDone(torrentId string) bool { func (t *TorrentManager) persistBins() { data := map[string]interface{}{ - "trash_bin": t.immediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string] - "repair_bin": t.onceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string] + "trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string] + "repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string] } jsonData, err := json.Marshal(data) @@ -502,12 +502,12 @@ func (t *TorrentManager) persistBins() { } func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) { - t.immediateBin.Difference(freshIDs).Each(func(id string) bool { - t.immediateBin.Remove(id) + t.ImmediateBin.Difference(freshIDs).Each(func(id string) bool { + t.ImmediateBin.Remove(id) return false }) - t.onceDoneBin.Difference(freshIDs).Each(func(id string) bool { - t.onceDoneBin.Remove(id) + t.OnceDoneBin.Difference(freshIDs).Each(func(id string) bool { + t.OnceDoneBin.Remove(id) return false }) t.persistBins()
On Library Update%v
Immediate Bin%s
Once Done Bin %s