diff --git a/download/download.go b/download/download.go index 224dbde..7cc304b 100644 --- a/download/download.go +++ b/download/download.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/tardisx/gropple/config" @@ -38,6 +39,8 @@ type Downloads []*Download var CanStopDownload = false +var downloadId int32 = 0 + // StartQueued starts any downloads that have been queued, we would not exceed // maxRunning. If maxRunning is 0, there is no limit. 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() { if !CanStopDownload { log.Print("attempted to stop download on a platform that it is not currently supported on - please report this as a bug") diff --git a/main.go b/main.go index d040fbe..4784656 100644 --- a/main.go +++ b/main.go @@ -320,26 +320,13 @@ func fetchHandler(w http.ResponseWriter, r *http.Request) { // check the URL for a sudden but inevitable betrayal if strings.Contains(url[0], conf.Server.Address) { w.WriteHeader(400) - fmt.Fprint(w, "you musn't gropple your gropple :-)") + fmt.Fprint(w, "you mustn't gropple your gropple :-)") return } // create the record - // XXX should be atomic! - downloadId++ - 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) + newDownload := download.NewDownload(conf, url[0]) + downloads = append(downloads, newDownload) // XXX atomic ^^ newDownload.Log = append(newDownload.Log, "start of log...")