Пример #1
0
//SetupLog sets up initial ConnectionLog
func SetupLog(length, noOfConn int) error {
	connLog.stats = make([]ConnectionStat, noOfConn)
	barArray := make([]*pb.ProgressBar, noOfConn+1)
	lenSub := length / noOfConn
	for i := 0; i < noOfConn; i++ {
		fileBegin := lenSub * i
		fileEnd := lenSub * (i + 1)
		if i == noOfConn-1 {
			fileEnd = length
		}
		bar := pb.New(fileEnd - fileBegin).Prefix("Connection " + strconv.Itoa(i+1) + " ")
		customizeBar(bar)
		connLog.stats[i] = ConnectionStat{connectionIndex: i, pbar: bar}
		barArray[i] = bar
	}
	bar := pb.New(length).Prefix("Total ")
	customizeBar(bar)
	connLog.totalbar = bar
	barArray[noOfConn] = bar
	var err error
	connLog.pool, err = pb.StartPool(barArray...)
	if err != nil {
		return err
	}
	return nil
}
Пример #2
0
func Example_multiple() {
	// create bars
	first := pb.New(200).Prefix("First ")
	second := pb.New(200).Prefix("Second ")
	third := pb.New(200).Prefix("Third ")
	// start pool
	pool, err := pb.StartPool(first, second, third)
	if err != nil {
		panic(err)
	}
	// update bars
	wg := new(sync.WaitGroup)
	for _, bar := range []*pb.ProgressBar{first, second, third} {
		wg.Add(1)
		go func(cb *pb.ProgressBar) {
			for n := 0; n < 200; n++ {
				cb.Increment()
				time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
			}
			cb.Finish()
			wg.Done()
		}(bar)
	}
	wg.Wait()
	// close pool
	pool.Stop()
}
Пример #3
0
//ReportErrorStat reports a log if an error occurs in a connection
func ReportErrorStat(i int, err error, noOfConn int) {
	connLog.stats[i].Err = err
	connLog.pool.Stop()
	log.Println()
	log.Println("Error in connection " + strconv.Itoa(i+1) + " : " + err.Error())
	log.Println()
	barArray := make([]*pb.ProgressBar, noOfConn+1)
	for i := 0; i < noOfConn; i++ {
		barArray[i] = connLog.stats[i].pbar
	}
	barArray[noOfConn] = connLog.totalbar
	connLog.pool, _ = pb.StartPool(barArray...)
}
Пример #4
0
//SetupResumeLog sets up ConnectionLog for a resumed download
func SetupResumeLog(filename string, length, noOfConn int) error {
	connLog.stats = make([]ConnectionStat, noOfConn)
	barArray := make([]*pb.ProgressBar, noOfConn+1)
	totalbar := pb.New(length).Prefix("Total ")
	lenSub := length / noOfConn
	for i := 0; i < noOfConn; i++ {
		partFilename := "temp/" + filename + "_" + strconv.Itoa(i)
		if _, err := os.Stat(partFilename); err == nil {
			reader, err := ioutil.ReadFile(partFilename)
			if err != nil {
				return err
			}
			header := reader[:16]
			fileBegin := int(binary.LittleEndian.Uint64(header[0:8]))
			fileEnd := int(binary.LittleEndian.Uint64(header[8:16]))
			bar := pb.New(fileEnd - fileBegin).Prefix("Connection " + strconv.Itoa(i+1) + " ")
			for j := 0; j < len(reader)-16; j++ {
				bar.Increment()
				totalbar.Increment()
			}
			customizeBar(bar)
			connLog.stats[i] = ConnectionStat{connectionIndex: i, pbar: bar}
			barArray[i] = bar
		} else {
			fileBegin := lenSub * i
			fileEnd := lenSub * (i + 1)
			if i == noOfConn-1 {
				fileEnd = length
			}
			bar := pb.New(fileEnd - fileBegin).Prefix("Connection " + strconv.Itoa(i+1) + " ")
			customizeBar(bar)
			connLog.stats[i] = ConnectionStat{connectionIndex: i, pbar: bar}
			barArray[i] = bar
		}
	}
	customizeBar(totalbar)
	connLog.totalbar = totalbar
	barArray[noOfConn] = totalbar
	var err error
	connLog.pool, err = pb.StartPool(barArray...)
	if err != nil {
		return err
	}
	return nil
}
Пример #5
0
func copy(c *cli.Context) {
	args := c.Args()
	if len(args) == 0 {
		printError(fmt.Errorf("No URLs are specified"))
	} else {
		var uploaders []*Copy
		albumID := c.String("album")
		for i := range args {
			title := c.String("title")
			if title == "" {
				title = args[i]
			}
			uploader, err := NewCopy(albumID, title, args[i])
			if err != nil {
				printError(err)
				continue
			}
			uploaders = append(uploaders, uploader)
		}
		bars := make([]*pb.ProgressBar, len(uploaders))
		for i, upload := range uploaders {
			bars[i] = upload.progressBar
		}
		p, _ := pb.StartPool(bars...)
		wg := new(sync.WaitGroup)
		for _, uploader := range uploaders {
			wg.Add(1)
			go func(u *Copy) {
				u.Execute()
				wg.Done()
			}(uploader)
		}
		wg.Wait()
		p.Stop()
		for _, uploader := range uploaders {
			if uploader.Media != nil {
				fmt.Printf("Media %s was uploaded.\n", uploader.Media.ID)
			} else {
				printError(fmt.Errorf("[%s] %v", uploader.Title, uploader.UploadError))
			}
		}
	}
}