func Initialize(srcs []Downloadable, threads int) (d *DownloadEngine, err error) { if srcs == nil || threads <= 0 { return nil, errors.New("Неверный аргумент при инициализации загрузчика") } d = new(DownloadEngine) d.registerQMLTypes() d.srcs = srcs d.engine = qml.NewEngine() component, err := d.engine.LoadFile("qrc:///qml/downloadgui.qml") if err != nil { log.Panicln(err) } d.engine.Context().SetVar("engine", d) d.mainwindow = component.CreateWindow(nil) d.mainwindow.Show() model := d.mainwindow.Root().ObjectByName("filetable").ObjectByName("dllist") d.threadPool, err = tunny.CreatePool(threads, func(object interface{}) interface{} { dlo := object.(Downloadable) d.completedl = append(d.completedl, 0) d.totaldl = append(d.totaldl, 0) model.Call("appendStruct", &DisplayableItem{Fname: filepath.Base(dlo.ActualPath()), Dlspeed: "0B/s", Dlprogress: 0.0}) item := model.Call("back").(qml.Object) index := model.Int("count") - 1 dlo.Progress(func(p curl.ProgressStatus) { if p.Size != 0 && d.completedl[index] != p.Size { d.completedl[index] = p.Size } if p.ContentLength != 0 && d.totaldl[index] != p.ContentLength { d.totaldl[index] = p.ContentLength } //when download completes, percents sets to 0 if p.Percent != 0 { item.Set("dlprogress", p.Percent) } else { item.Set("dlprogress", 1) d.completedl[index] = d.totaldl[index] } item.Set("dlspeed", curl.PrettySpeedString(p.Speed)) d.updateTotalProgress() }, 100*time.Millisecond) _, err := object.(Downloadable).Do() if err != nil { /*dialogboxes.ShowErrorDialog(err.Error())*/ log.Println(err.Error()) } return nil }).Open() if err != nil { log.Panicln(err) } for _, v := range srcs { d.threadPool.SendWorkAsync(v, func(interface{}, error) {}) } return }
func DownloadFile(url string, fileName string, verbose bool) { req := curl.New(url) req.Method("POST") req.SaveToFile(fileName) // Print progress status per one second req.Progress(func(p curl.ProgressStatus) { if verbose { log.SetOutput(os.Stdout) log.Println( "speed", curl.PrettySpeedString(p.Speed), "len", curl.PrettySizeString(p.ContentLength), "got", curl.PrettySizeString(p.Size), ) } }, time.Second) req.Do() }
func (u *Ui) refreshActiveDownloadWidgets() { u.activeDownloadWidgets = make([]*termui.Gauge, 0, len(u.activeDownloads)) uris := make([]string, 0, len(u.activeDownloads)) for uri, _ := range u.activeDownloads { uris = append(uris, uri) } sort.Strings(uris) for _, uri := range uris { progress := u.activeDownloads[uri] widget := termui.NewGauge() widget.Height = 3 widget.Percent = int(progress.Percent * 100) widget.Border.Label = "Downloading: " + uri widget.Label = fmt.Sprintf("{{percent}}%% (%s)", curl.PrettySpeedString(progress.Speed)) u.activeDownloadWidgets = append(u.activeDownloadWidgets, widget) } }