From 215cdcc20982cd066f7abb1546f8adb6b4f2d8ee Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Wed, 22 Nov 2023 16:18:46 +0100 Subject: [PATCH] Set time by ended or added correctly --- internal/dav/listing.go | 2 +- internal/torrent/manager.go | 6 +++++- internal/torrent/types.go | 1 + internal/universal/head.go | 13 +++++++++++-- pkg/realdebrid/types.go | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 1453e7c..5ffd715 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -110,7 +110,7 @@ func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.Torre if file == nil || !strings.HasPrefix(file.Link, "http") { continue } - fmt.Fprint(w, dav.File(filepath.Join(requestPath, filename), file.Bytes, tor.LatestAdded)) + fmt.Fprint(w, dav.File(filepath.Join(requestPath, filename), file.Bytes, file.Ended)) } fmt.Fprint(w, "") diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 85650f4..a77f815 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -365,6 +365,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { selectedFiles = append(selectedFiles, &File{ File: file, Added: info.Added, + Ended: info.Ended, Link: "", // no link yet ZurgFS: hashStringToFh(file.Path + info.Hash), }) @@ -495,6 +496,7 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([ } if !found { if result.Response.Streamable == 1 { + now := time.Now().Format(time.RFC3339) selectedFiles = append(selectedFiles, &File{ File: realdebrid.File{ ID: math.MaxInt32, @@ -502,7 +504,8 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([ Bytes: result.Response.Filesize, Selected: 1, }, - Added: time.Now().Format(time.RFC3339), + Added: now, + Ended: now, Link: result.Response.Link, ZurgFS: hashStringToFh(result.Response.Filename), }) @@ -593,6 +596,7 @@ func (t *TorrentManager) Repair(accessKey string) { fileCopy := &File{ File: file.File, Added: file.Added, + Ended: file.Ended, Link: file.Link, ZurgFS: file.ZurgFS, } diff --git a/internal/torrent/types.go b/internal/torrent/types.go index 1ed2045..4bc3a59 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -25,6 +25,7 @@ func (t *Torrent) InProgress() bool { type File struct { realdebrid.File Added string + Ended string Link string ZurgFS uint64 } diff --git a/internal/universal/head.go b/internal/universal/head.go index f44068a..d3f7abf 100644 --- a/internal/universal/head.go +++ b/internal/universal/head.go @@ -12,6 +12,10 @@ import ( "github.com/hashicorp/golang-lru/v2/expirable" ) +const ( + SPLIT_TOKEN = "$" +) + func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, cache *expirable.LRU[string, string]) { log := logutil.NewLogger().Named("head") @@ -30,11 +34,13 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren } if data, exists := cache.Get("head:" + requestPath); exists { - splits := strings.Split(data, " ") + splits := strings.Split(data, SPLIT_TOKEN) contentType := splits[0] contentLength := splits[1] + lastModified := splits[2] w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", contentLength) + w.Header().Set("Last-Modified", lastModified) w.WriteHeader(http.StatusOK) return } @@ -70,9 +76,12 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren } contentType := getContentMimeType(filename) contentLength := fmt.Sprintf("%d", file.Bytes) + lastModified := file.Ended w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", contentLength) - cache.Add("head:"+requestPath, contentType+" "+contentLength) + w.Header().Set("Last-Modified", lastModified) + cacheVal := strings.Join([]string{contentType, contentLength, lastModified}, SPLIT_TOKEN) + cache.Add("head:"+requestPath, cacheVal) w.WriteHeader(http.StatusOK) } diff --git a/pkg/realdebrid/types.go b/pkg/realdebrid/types.go index 2270c4b..77778d6 100644 --- a/pkg/realdebrid/types.go +++ b/pkg/realdebrid/types.go @@ -53,6 +53,7 @@ type TorrentInfo struct { Progress int `json:"-"` Status string `json:"status"` Added string `json:"added"` + Ended string `json:"ended"` Bytes int64 `json:"bytes"` Links []string `json:"links"` OriginalName string `json:"original_filename"` // from info