Refactor download creation

This commit is contained in:
Justin Hawkins 2022-04-06 20:35:28 +09:30
parent bdf9730ab0
commit c05bed1148
2 changed files with 23 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/tardisx/gropple/config" "github.com/tardisx/gropple/config"
@ -38,6 +39,8 @@ type Downloads []*Download
var CanStopDownload = false var CanStopDownload = false
var downloadId int32 = 0
// StartQueued starts any downloads that have been queued, we would not exceed // StartQueued starts any downloads that have been queued, we would not exceed
// maxRunning. If maxRunning is 0, there is no limit. // maxRunning. If maxRunning is 0, there is no limit.
func (dls Downloads) StartQueued(maxRunning int) { func (dls Downloads) StartQueued(maxRunning int) {
@ -100,6 +103,23 @@ func (dl *Download) Queue() {
} }
func NewDownload(conf *config.Config, url string) *Download {
atomic.AddInt32(&downloadId, 1)
dl := Download{
Config: conf,
Id: int(downloadId),
Url: url,
PopupUrl: fmt.Sprintf("/fetch/%d", int(downloadId)),
State: "choose profile",
Finished: false,
Eta: "?",
Percent: 0.0,
Log: make([]string, 0, 1000),
}
return &dl
}
func (dl *Download) Stop() { func (dl *Download) Stop() {
if !CanStopDownload { if !CanStopDownload {
log.Print("attempted to stop download on a platform that it is not currently supported on - please report this as a bug") log.Print("attempted to stop download on a platform that it is not currently supported on - please report this as a bug")

19
main.go
View File

@ -320,26 +320,13 @@ func fetchHandler(w http.ResponseWriter, r *http.Request) {
// check the URL for a sudden but inevitable betrayal // check the URL for a sudden but inevitable betrayal
if strings.Contains(url[0], conf.Server.Address) { if strings.Contains(url[0], conf.Server.Address) {
w.WriteHeader(400) w.WriteHeader(400)
fmt.Fprint(w, "you musn't gropple your gropple :-)") fmt.Fprint(w, "you mustn't gropple your gropple :-)")
return return
} }
// create the record // create the record
// XXX should be atomic! newDownload := download.NewDownload(conf, url[0])
downloadId++ downloads = append(downloads, newDownload)
newDownload := download.Download{
Config: conf,
Id: downloadId,
Url: url[0],
PopupUrl: fmt.Sprintf("/fetch/%d", downloadId),
State: "choose profile",
Finished: false,
Eta: "?",
Percent: 0.0,
Log: make([]string, 0, 1000),
}
downloads = append(downloads, &newDownload)
// XXX atomic ^^ // XXX atomic ^^
newDownload.Log = append(newDownload.Log, "start of log...") newDownload.Log = append(newDownload.Log, "start of log...")