webapi: Move download info into api context

This commit is contained in:
Kleissner
2021-12-29 05:41:16 +01:00
parent f2c344dc96
commit ec1bfdfac8
2 changed files with 24 additions and 22 deletions

View File

@@ -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 {

View File

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