コード例 #1
0
ファイル: dl.go プロジェクト: chonthu/dl
func get(source string) error {
	u, err := url.Parse(source)
	if err != nil {
		return err
	}
	dest := path.Base(u.Path)
	if dest == "" {
		log.Println("using default dest filename:", defaultDest)
		dest = defaultDest
	}

	rp := func(r *http.Request) {
		if *username != "" || *password != "" {
			r.SetBasicAuth(*username, *password)
		}
	}
	size, state, err := fetch.Fetch(dest, source, rp)
	if err != nil {
		return err
	}
	if size == 0 {
		return nil
	}

	var (
		lastState fetch.State
		lastCount int64
		ticker    = time.NewTicker(time.Second)
	)
	defer ticker.Stop()
	for {
		select {
		case s, ok := <-state:
			if !ok {
				log.Printf("%v bytes received",
					humanize.Comma(lastState.Count))
				return lastState.Err
			}
			lastState = s
		case <-ticker.C:
			log.Printf(
				"%v/%v bytes received (%v/sec)",
				humanize.Comma(lastState.Count),
				humanize.Comma(size),
				humanize.Bytes(uint64(lastState.Count-lastCount)),
			)
			lastCount = lastState.Count
		}
	}
}
コード例 #2
0
ファイル: mirror.go プロジェクト: chonthu/dl
func (m *Manager) fetch(url string, statusC chan<- status) (int64, error) {
	dest := filepath.ToSlash(strings.TrimPrefix(url, m.base))
	if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
		return 0, err
	}
	size, state, err := fetch.Fetch(dest, url, m.rp)
	if err != nil {
		return size, err
	}
	go func() {
		for s := range state {
			statusC <- status{url, s}
		}
	}()
	return size, nil
}