mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-08 01:20:27 +00:00
feat: bt improve
This commit is contained in:
1 parent
c6aa83194f
commit
0fc133e46d
28 files changed
+1537
-260
No files matched your search
+54
-10
@@ -15,7 +15,7 @@ func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
if util.ReadJson(w, r, &req) {
|
||||
resource, err := Downloader.Resolve(&req)
|
||||
if err != nil {
|
||||
util.WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(resource))
|
||||
@@ -23,11 +23,17 @@ func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.CreateTaskReq
|
||||
var req model.CreateTask
|
||||
if util.ReadJson(w, r, &req) {
|
||||
taskId, err := Downloader.Create(req.Resource, req.Options)
|
||||
if req.Opts.Connections == 0 {
|
||||
req.Opts.Connections = getServerConfig().Connections
|
||||
}
|
||||
if req.Opts.Path == "" {
|
||||
req.Opts.Path = getServerConfig().DownloadDir
|
||||
}
|
||||
taskId, err := Downloader.Create(req.Res, req.Opts)
|
||||
if err != nil {
|
||||
util.WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(taskId))
|
||||
@@ -37,14 +43,31 @@ func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
Downloader.Pause(taskId)
|
||||
if err := Downloader.Pause(taskId); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func ContinueTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
Downloader.Continue(taskId)
|
||||
if err := Downloader.Continue(taskId); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func DeleteTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
force := r.FormValue("force")
|
||||
if err := Downloader.Delete(taskId, force == "true"); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
@@ -53,7 +76,7 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||
taskId := vars["id"]
|
||||
task := Downloader.GetTask(taskId)
|
||||
if task == nil {
|
||||
util.WriteJson(w, 404, model.NewResultWithMsg("task not found"))
|
||||
util.WriteJson(w, http.StatusNotFound, model.NewResultWithMsg("task not found"))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(task))
|
||||
@@ -62,12 +85,12 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||
func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||
status := r.FormValue("status")
|
||||
if status == "" {
|
||||
util.WriteJson(w, 400, model.NewResultWithMsg("param is required: status"))
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg("param is required: status"))
|
||||
return
|
||||
}
|
||||
statusArr := strings.Split(status, ",")
|
||||
tasks := Downloader.GetTasks()
|
||||
var result []*download.Task
|
||||
result := make([]*download.Task, 0)
|
||||
for _, task := range tasks {
|
||||
for _, s := range statusArr {
|
||||
if task.Status == base.Status(s) {
|
||||
@@ -75,5 +98,26 @@ func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(tasks))
|
||||
util.WriteJsonOk(w, model.NewResultWithData(result))
|
||||
}
|
||||
|
||||
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||
util.WriteJsonOk(w, model.NewResultWithData(getServerConfig()))
|
||||
}
|
||||
|
||||
func PutConfig(w http.ResponseWriter, r *http.Request) {
|
||||
var cfg model.ServerConfig
|
||||
if util.ReadJson(w, r, &cfg) {
|
||||
if err := Downloader.PutConfig(&cfg); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func getServerConfig() *model.ServerConfig {
|
||||
var cfg model.ServerConfig
|
||||
Downloader.GetConfig(&cfg)
|
||||
return &cfg
|
||||
}
|
||||
Reference in new issue
Block a user