feat(downloader): add cancel download handler for task management

pull/3834/head
banbxio 2025-04-02 19:19:17 +08:00
parent fdbce4abec
commit 37a36a3e40
2 changed files with 19 additions and 0 deletions

View File

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

View File

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