Set time by ended or added correctly

This commit is contained in:
Ben Sarmiento
2023-11-22 16:18:46 +01:00
parent eee83dacf7
commit 215cdcc209
5 changed files with 19 additions and 4 deletions

View File

@@ -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)
}