From ec1bfdfac876c894b236fa866faf16f65e436b43 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Wed, 29 Dec 2021 05:41:16 +0100 Subject: [PATCH] webapi: Move download info into api context --- webapi/API.go | 6 ++++++ webapi/Download.go | 40 ++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/webapi/API.go b/webapi/API.go index 723461e..47ef627 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "net/http" + "sync" "time" "github.com/IncSW/geoip2" @@ -27,6 +28,10 @@ type WebapiInstance struct { // Router can be used to register additional API functions Router *mux.Router AllowKeyInParam []string // List of paths that accept the API key as &k= parameter + + // download info + downloads map[uuid.UUID]*downloadInfo + downloadsMutex sync.RWMutex } // WSUpgrader is used for websocket functionality. It allows all requests. @@ -51,6 +56,7 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi backend: Backend, Router: mux.NewRouter(), AllowKeyInParam: []string{"/file/read", "/file/view"}, + downloads: make(map[uuid.UUID]*downloadInfo), } if APIKey != uuid.Nil { diff --git a/webapi/Download.go b/webapi/Download.go index f17898f..0b6e766 100644 --- a/webapi/Download.go +++ b/webapi/Download.go @@ -76,7 +76,7 @@ func (api *WebapiInstance) apiDownloadStart(w http.ResponseWriter, r *http.Reque return } - info := &downloadInfo{backend: api.backend, id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID} + info := &downloadInfo{backend: api.backend, api: api, id: uuid.New(), created: time.Now(), hash: hash, nodeID: nodeID} // create the file immediately if info.initDiskFile(filePath) != nil { @@ -85,7 +85,7 @@ func (api *WebapiInstance) apiDownloadStart(w http.ResponseWriter, r *http.Reque } // add the download to the list - downloadAdd(info) + api.downloadAdd(info) // start the download! go info.Start() @@ -107,7 +107,7 @@ func (api *WebapiInstance) apiDownloadStatus(w http.ResponseWriter, r *http.Requ return } - info := downloadLookup(id) + info := api.downloadLookup(id) if info == nil { EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound}) return @@ -152,7 +152,7 @@ func (api *WebapiInstance) apiDownloadAction(w http.ResponseWriter, r *http.Requ return } - info := downloadLookup(id) + info := api.downloadLookup(id) if info == nil { EncodeJSON(api.backend, w, r, apiResponseDownloadStatus{APIStatus: DownloadResponseIDNotFound}) return @@ -204,30 +204,26 @@ type downloadInfo struct { // live connections, to be changed peer *core.PeerInfo + api *WebapiInstance backend *core.Backend } -var ( - downloads = make(map[uuid.UUID]*downloadInfo) - downloadsMutex sync.RWMutex -) - -func downloadAdd(info *downloadInfo) { - downloadsMutex.Lock() - downloads[info.id] = info - downloadsMutex.Unlock() +func (api *WebapiInstance) downloadAdd(info *downloadInfo) { + api.downloadsMutex.Lock() + api.downloads[info.id] = info + api.downloadsMutex.Unlock() } -func downloadDelete(id uuid.UUID) { - downloadsMutex.Lock() - delete(downloads, id) - downloadsMutex.Unlock() +func (api *WebapiInstance) downloadDelete(id uuid.UUID) { + api.downloadsMutex.Lock() + delete(api.downloads, id) + api.downloadsMutex.Unlock() } -func downloadLookup(id uuid.UUID) (info *downloadInfo) { - downloadsMutex.Lock() - info = downloads[id] - downloadsMutex.Unlock() +func (api *WebapiInstance) downloadLookup(id uuid.UUID) (info *downloadInfo) { + api.downloadsMutex.Lock() + info = api.downloads[id] + api.downloadsMutex.Unlock() return info } @@ -236,7 +232,7 @@ func downloadLookup(id uuid.UUID) (info *downloadInfo) { func (info *downloadInfo) DeleteDefer(Duration time.Duration) { go func() { <-time.After(Duration) - downloadDelete(info.id) + info.api.downloadDelete(info.id) }() }