示例#1
0
func downloadImage(u, output string) error {
	url, err := url.Parse(u)
	if err != nil {
		return err
	}

	_, filename := filepath.Split(url.Path)
	out, err := os.Create(filepath.Join(output, filename))
	if err != nil {
		return err
	}
	defer out.Close()

	resp, err := http.Get(u)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	sum := 0

	bar := uiprogress.AddBar(int(resp.ContentLength)).AppendCompleted().PrependElapsed()
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		return strutil.Resize(humanize.Bytes(uint64(sum))+"/"+humanize.Bytes(uint64(resp.ContentLength))+" "+filename+": ", 42)
	})

	for {
		written, err := io.CopyN(out, resp.Body, 4096)
		sum += int(written)
		bar.Set(sum)
		if err != nil {
			return err
		}
	}
}
示例#2
0
func newBar(name string, steps int) *BuildBar {
	buildBar := BuildBar{name: name}
	bar := uiprogress.AddBar(steps).AppendCompleted()
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		return strutil.Resize(buildBar.name+": "+buildBar.currentStep, 20)
	})
	buildBar.bar = bar
	return &buildBar
}
示例#3
0
func deploy(app string, wg *sync.WaitGroup) {
	defer wg.Done()
	bar := uiprogress.AddBar(len(steps)).AppendCompleted().PrependElapsed()
	bar.Width = 100

	// prepend the deploy step to the bar
	bar.PrependFunc(func(b *uiprogress.Bar) string {
		return strutil.Resize(app+": "+steps[b.Current()-1], 22)
	})

	rand.Seed(500)
	for bar.Incr() {
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(2000)))
	}
}
示例#4
0
func upload(bucket *backblaze.Bucket, file string, meta map[string]string) (*backblaze.File, error) {

	stat, err := os.Stat(file)
	if err != nil {
		return nil, err
	}

	reader, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer reader.Close()

	var r io.Reader = reader
	if opts.Verbose {
		bar := uiprogress.AddBar(int(stat.Size()))
		// TODO Stop bar refresh when complete

		if stat.Size() > 1024*100 {
			start := time.Now()
			elapsed := time.Duration(1)
			count := 0
			bar.AppendFunc(func(b *uiprogress.Bar) string {
				count++
				if count < 2 {
					return ""
				}

				// elapsed := b.TimeElapsed()
				if b.Current() < b.Total {
					elapsed = time.Now().Sub(start)
				}
				speed := uint64(float64(b.Current()) / elapsed.Seconds())
				return humanize.IBytes(speed) + "/sec"
			})
		}
		bar.AppendCompleted()
		bar.PrependFunc(func(b *uiprogress.Bar) string { return fmt.Sprintf("%10s", humanize.IBytes(uint64(b.Total))) })
		bar.PrependFunc(func(b *uiprogress.Bar) string { return strutil.Resize(file, 50) })
		bar.Width = 20

		r = &progressReader{bar, reader}
	}

	return bucket.UploadFile(filepath.Base(file), meta, r)
}
示例#5
0
func download(fileInfo *backblaze.File, reader io.ReadCloser, path string) error {
	defer reader.Close()

	err := os.MkdirAll(filepath.Dir(path), 0777)
	if err != nil {
		return err
	}

	writer, err := os.Create(path)
	if err != nil {
		return err
	}
	defer writer.Close()

	var w io.Writer = writer
	if opts.Verbose {
		bar := uiprogress.AddBar(int(fileInfo.ContentLength))
		bar.AppendFunc(func(b *uiprogress.Bar) string {
			speed := (float32(b.Current()) / 1024) / float32(b.TimeElapsed().Seconds())
			return fmt.Sprintf("%7.2f KB/s", speed)
		})
		bar.AppendCompleted()
		bar.PrependFunc(func(b *uiprogress.Bar) string { return fmt.Sprintf("%10d", b.Total) })
		bar.PrependFunc(func(b *uiprogress.Bar) string { return strutil.Resize(fileInfo.Name, 50) })
		bar.Width = 20

		w = &progressWriter{bar, writer}
	}

	sha := sha1.New()
	tee := io.MultiWriter(sha, w)

	_, err = io.Copy(tee, reader)
	if err != nil {
		return err
	}

	// Check SHA
	sha1Hash := hex.EncodeToString(sha.Sum(nil))
	if sha1Hash != fileInfo.ContentSha1 {
		return errors.New("Downloaded data does not match SHA1 hash")
	}

	return nil
}
示例#6
0
func download(fileInfo *backblaze.File, reader io.ReadCloser, path string, o *Get) error {
	defer reader.Close()

	var writer = ioutil.Discard
	if !o.Discard {
		err := os.MkdirAll(filepath.Dir(path), 0777)
		if err != nil {
			return err
		}

		file, err := os.Create(path)
		if err != nil {
			return err
		}
		defer file.Close()
		writer = file
	}

	if opts.Verbose {
		bar := uiprogress.AddBar(int(fileInfo.ContentLength))

		if fileInfo.ContentLength > 1024*100 {
			start := time.Now()
			elapsed := time.Duration(1)
			count := 0
			bar.AppendFunc(func(b *uiprogress.Bar) string {
				count++
				if count < 2 {
					return ""
				}

				// elapsed := b.TimeElapsed()
				if b.Current() < b.Total {
					elapsed = time.Now().Sub(start)
				}
				speed := uint64(float64(b.Current()) / elapsed.Seconds())
				return humanize.IBytes(speed) + "/sec"
			})
		}
		bar.AppendCompleted()
		bar.PrependFunc(func(b *uiprogress.Bar) string { return fmt.Sprintf("%10s", humanize.IBytes(uint64(b.Total))) })
		bar.PrependFunc(func(b *uiprogress.Bar) string { return strutil.Resize(fileInfo.Name, 50) })
		bar.Width = 20

		writer = &progressWriter{bar, writer}
	}

	sha := sha1.New()
	tee := io.MultiWriter(sha, writer)

	_, err := io.Copy(tee, reader)
	if err != nil {
		return err
	}

	// Check SHA
	sha1Hash := hex.EncodeToString(sha.Sum(nil))
	if sha1Hash != fileInfo.ContentSha1 {
		return errors.New("Downloaded data does not match SHA1 hash")
	}

	return nil
}