diff --git a/internal/handlers/home.go b/internal/handlers/home.go index c684c91..82851f7 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -269,21 +269,24 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
- - - - - + @@ -387,6 +390,13 @@ func (zr *Handlers) handleDumpTorrents(resp http.ResponseWriter, req *http.Reque fmt.Fprint(resp, "Dumping torrents...") } +func (zr *Handlers) handleAnalyzeTorrents(resp http.ResponseWriter, req *http.Request) { + resp.Header().Set("Refresh", "2; url=/") + zr.torMgr.AnalyzeTrigger <- struct{}{} + zr.log.Infof("Triggered media analysis of torrents") + fmt.Fprint(resp, "Analyzing all torrents...") +} + func bToMb(b uint64) uint64 { return b / 1024 / 1024 } diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 9e2e475..35de821 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -50,11 +50,12 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t router.Use(hs.options) router.Get("/", hs.handleHome) - router.Post("/reboot/worker", hs.handleRebootWorkerPool) - router.Post("/reboot/refresh", hs.handleRebootRefreshWorker) - router.Post("/reboot/repair", hs.handleRebootRepairWorker) - router.Post("/remount/downloads", hs.handleRemountDownloads) - router.Post("/dump/torrents", hs.handleDumpTorrents) + router.Post("/reboot-worker", hs.handleRebootWorkerPool) + router.Post("/reboot-refresh", hs.handleRebootRefreshWorker) + router.Post("/reboot-repair", hs.handleRebootRepairWorker) + router.Post("/downloads/remount", hs.handleRemountDownloads) + router.Post("/torrents/dump", hs.handleDumpTorrents) + router.Post("/torrents/analyze", hs.handleAnalyzeTorrents) // version router.Get(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile) router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 14b4256..cc085e1 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -42,6 +42,7 @@ type TorrentManager struct { RepairKillSwitch chan struct{} RemountTrigger chan struct{} DumpTrigger chan struct{} + AnalyzeTrigger chan struct{} latestState *LibraryState @@ -102,18 +103,11 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w t.StartDownloadsJob() t.StartRepairJob() t.StartDumpJob() + t.StartMediaAnalysisJob() t.setNewLatestState(t.getCurrentState()) t.TriggerRepair(nil) - - t.log.Info("Applying media info details to all torrents") - allTorrents, _ := t.DirectoryMap.Get(INT_ALL) - allTorrents.IterCb(func(_ string, torrent *Torrent) { - t.applyMediaInfoDetails(torrent) - t.writeTorrentToFile(torrent) - t.log.Debugf("Applied media info details to torrent %s", t.GetKey(torrent)) - }) }) return t @@ -414,6 +408,27 @@ func (t *TorrentManager) StartDumpJob() { }) } +func (t *TorrentManager) analyzeAllTorrents() { + allTorrents, _ := t.DirectoryMap.Get(INT_ALL) + totalCount := allTorrents.Count() + t.log.Infof("Applying media info details to all %d torrents", totalCount) + idx := 0 + allTorrents.IterCb(func(_ string, torrent *Torrent) { + t.applyMediaInfoDetails(torrent) + t.writeTorrentToFile(torrent) + idx++ + t.log.Debugf("Applied media info details to torrent %s (%d/%d)", t.GetKey(torrent), idx, totalCount) + }) +} + +func (t *TorrentManager) StartMediaAnalysisJob() { + _ = t.workerPool.Submit(func() { + for range t.AnalyzeTrigger { + t.analyzeAllTorrents() + } + }) +} + func (t *TorrentManager) initializeDirectoryMaps() { // create internal directories t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()