@@ -23,6 +23,7 @@ import (
2323 "github.com/sirrobot01/decypharr/internal/logger"
2424 "github.com/sirrobot01/decypharr/internal/utils"
2525 "github.com/sirrobot01/decypharr/pkg/debrid/types"
26+ _ "time/tzdata"
2627)
2728
2829type WebDavFolderNaming string
@@ -109,9 +110,16 @@ type Cache struct {
109110
110111func New (dc config.Debrid , client types.Client ) * Cache {
111112 cfg := config .Get ()
112- cet , _ := time .LoadLocation ("CET" )
113- cetSc , _ := gocron .NewScheduler (gocron .WithLocation (cet ))
114- scheduler , _ := gocron .NewScheduler (gocron .WithLocation (time .Local ))
113+ cetSc , err := gocron .NewScheduler (gocron .WithLocation (time .UTC ))
114+ if err != nil {
115+ // If we can't create a CET scheduler, fallback to local time
116+ cetSc , _ = gocron .NewScheduler (gocron .WithLocation (time .Local ))
117+ }
118+ scheduler , err := gocron .NewScheduler (gocron .WithLocation (time .Local ))
119+ if err != nil {
120+ // If we can't create a local scheduler, fallback to CET
121+ scheduler = cetSc
122+ }
115123
116124 autoExpiresLinksAfter , err := time .ParseDuration (dc .AutoExpireLinksAfter )
117125 if autoExpiresLinksAfter == 0 || err != nil {
@@ -307,10 +315,10 @@ func (c *Cache) load(ctx context.Context) (map[string]CachedTorrent, error) {
307315 }
308316
309317 isComplete := true
310- if len (ct .Files ) != 0 {
318+ if len (ct .GetFiles () ) != 0 {
311319 // Check if all files are valid, if not, delete the file.json and remove from cache.
312- fs := make (map [string ]types.File , len (ct .Files ))
313- for _ , f := range ct .Files {
320+ fs := make (map [string ]types.File , len (ct .GetFiles () ))
321+ for _ , f := range ct .GetFiles () {
314322 if f .Link == "" {
315323 isComplete = false
316324 break
@@ -756,7 +764,7 @@ func (c *Cache) deleteTorrent(id string, removeFromDebrid bool) bool {
756764
757765 newFiles := map [string ]types.File {}
758766 newId := ""
759- for _ , file := range t .Files {
767+ for _ , file := range t .GetFiles () {
760768 if file .TorrentId != "" && file .TorrentId != id {
761769 if newId == "" && file .TorrentId != "" {
762770 newId = file .TorrentId
@@ -815,6 +823,36 @@ func (c *Cache) OnRemove(torrentId string) {
815823 }
816824}
817825
826+ // RemoveFile removes a file from the torrent cache
827+ // TODO sends a re-insert that removes the file from debrid
828+ func (c * Cache ) RemoveFile (torrentId string , filename string ) error {
829+ c .logger .Debug ().Str ("torrent_id" , torrentId ).Msgf ("Removing file %s" , filename )
830+ torrent , ok := c .torrents .getByID (torrentId )
831+ if ! ok {
832+ return fmt .Errorf ("torrent %s not found" , torrentId )
833+ }
834+ file , ok := torrent .GetFile (filename )
835+ if ! ok {
836+ return fmt .Errorf ("file %s not found in torrent %s" , filename , torrentId )
837+ }
838+ file .Deleted = true
839+ torrent .Files [filename ] = file
840+
841+ // If the torrent has no files left, delete it
842+ if len (torrent .GetFiles ()) == 0 {
843+ c .logger .Debug ().Msgf ("Torrent %s has no files left, deleting it" , torrentId )
844+ if err := c .DeleteTorrent (torrentId ); err != nil {
845+ return fmt .Errorf ("failed to delete torrent %s: %w" , torrentId , err )
846+ }
847+ return nil
848+ }
849+
850+ c .setTorrent (torrent , func (torrent CachedTorrent ) {
851+ c .listingDebouncer .Call (true )
852+ }) // Update the torrent in the cache
853+ return nil
854+ }
855+
818856func (c * Cache ) GetLogger () zerolog.Logger {
819857 return c .logger
820858}
0 commit comments