feat(downloader): add cancel download handler for task management
parent
fdbce4abec
commit
37a36a3e40
|
@ -119,6 +119,24 @@ func downloadStatusHandler(downloaderCache *cache.Cache) handleFunc {
|
|||
})
|
||||
}
|
||||
|
||||
func cancelDownloadHandler(downloaderCache *cache.Cache) handleFunc {
|
||||
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
||||
if !d.user.Perm.Create || !d.Check(r.URL.Path) {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
taskID := r.URL.Path
|
||||
taskCacheRaw, ok := downloaderCache.Get(taskID)
|
||||
if !ok {
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
taskCache := taskCacheRaw.(*DownloadTask)
|
||||
if taskCache.cancel != nil {
|
||||
taskCache.cancel()
|
||||
}
|
||||
return http.StatusOK, nil
|
||||
})
|
||||
}
|
||||
|
||||
func downloadWithTask(fs afero.Fs, task *DownloadTask) error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
task.cancel = cancel
|
||||
|
|
|
@ -96,6 +96,7 @@ func NewHandler(
|
|||
|
||||
api.PathPrefix("/download").Handler(monkey(downloadHandler(downloaderCache), "/api/download/")).Methods("POST")
|
||||
api.PathPrefix("/download").Handler(monkey(downloadStatusHandler(downloaderCache), "/api/download/")).Methods("GET")
|
||||
api.PathPrefix("/download").Handler(monkey(cancelDownloadHandler(downloaderCache), "/api/download/")).Methods("DELETE")
|
||||
|
||||
return stripPrefix(server.BaseURL, r), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue