Пример #1
0
func (this *GoGetSchedules) Speed() string {
	elaspe := time.Now().Sub(this.startTime).Seconds()
	speed := "0"
	if elaspe > 0 {
		speed = byten.Size(int64(float64(this.CompleteLength) / elaspe))
	}
	return fmt.Sprintf("%s/S     ", speed)
}
Пример #2
0
func (v *GRVisitor) VisitFile(fn string, fi os.FileInfo) {
	if fi.Size() == 0 && !opts.FindFiles {
		return
	}

	if v.ignoreFileMatcher.Match(fn, false) {
		return
	}

	if !v.acceptedFileMatcher.Match(fn, false) {
		return
	}

	if opts.FindFiles {
		v.SearchFileName(fn)
		return
	}

	if fi.Size() >= 1024*1024*10 {
		errhandle(fmt.Errorf("Skipping %s, too big: %s\n", fn, byten.Size(fi.Size())),
			false)
		return
	}

	// just skip invalid symlinks
	if fi.Mode()&os.ModeSymlink != 0 {
		if _, err := os.Stat(fn); err != nil {
			if opts.Verbose {
				errhandle(err, false)
			}
			return
		}
	}

	f, content := v.GetFileAndContent(fn, fi)
	if f == nil {
		return
	}
	defer f.Close()

	changed, result := v.ReplaceInFile(fn, content)
	if changed {
		f.Seek(0, 0)
		n, err := f.Write(result)
		if err != nil {
			errhandle(fmt.Errorf("Error writing replacement to file '%s': %s",
				fn, err), true)
		}
		if int64(n) < fi.Size() {
			err := f.Truncate(int64(n))
			if err != nil {
				errhandle(fmt.Errorf("Error truncating file '%s' to size %d",
					f, n), true)
			}
		}
	}
}
Пример #3
0
func humanByte(in ...interface{}) (out []interface{}) {
	for _, v := range in {
		switch v.(type) {
		case int, uint64:
			s := fmt.Sprintf("%d", v)
			i, _ := strconv.ParseInt(s, 10, 64)
			out = append(out, byten.Size(i))
		case float64:
			s := fmt.Sprintf("%.2f", v)
			out = append(out, s)
		default:
			out = append(out, v)
		}
	}
	return out
}
Пример #4
0
// Format returns human-readable var value representation.
func Format(v VarValue, kind VarKind) string {
	switch kind {
	case KindMemory:
		if _, ok := v.(int64); !ok {
			break
		}
		return fmt.Sprintf("%s", byten.Size(v.(int64)))
	case KindDuration:
		if _, ok := v.(int64); !ok {
			break
		}
		return fmt.Sprintf("%s", roundDuration(time.Duration(v.(int64))))
	}

	if f, ok := v.(float64); ok {
		return fmt.Sprintf("%.2f", f)
	}

	return fmt.Sprintf("%v", v)
}
Пример #5
0
func main() {
	args, err := argparser.Parse()
	if err != nil {
		os.Exit(1)
	}

	if opts.ShowVersion {
		fmt.Printf("goreplace %s\n", Version)
		return
	}

	NoColors = opts.NoColors || runtime.GOOS == "windows"
	if opts.BigFileSize != nil {
		BigFileSize = parseFileSize(opts.BigFileSize)
	}

	cwd, _ := os.Getwd()
	ignoreFileMatcher := NewMatcher(cwd, opts.NoGlobalIgnores)
	ignoreFileMatcher.Append(opts.IgnoreFiles)

	acceptedFileMatcher := NewGeneralMatcher([]string{}, []string{})
	if len(opts.AcceptFiles) > 0 {
		acceptedFileMatcher.Append(opts.AcceptFiles)
	} else {
		acceptedFileMatcher.Append([]string{".*"})
	}

	ignoreSizeText := fmt.Sprintf("Ignoring files bigger than %s\n",
		byten.Size(BigFileSize))
	if opts.NoBigIgnores {
		ignoreSizeText = ""
	}
	argparser.Usage = fmt.Sprintf("[OPTIONS] string-to-search\n\n%s%s",
		ignoreSizeText, ignoreFileMatcher)

	if opts.ShowHelp || len(args) == 0 {
		argparser.WriteHelp(os.Stdout)
		return
	}

	arg := args[0]
	if opts.PlainText {
		arg = regexp.QuoteMeta(arg)
	}
	if opts.IgnoreCase {
		arg = "(?i:" + arg + ")"
	}

	pattern, err := regexp.Compile(arg)
	errhandle(err, true)

	if pattern.Match([]byte("")) {
		errhandle(fmt.Errorf("Your pattern matches empty string"), true)
	}

	if opts.Replace != nil {
		s, err := strconv.Unquote(`"` + *opts.Replace + `"`)
		if err != nil {
			errhandle(err, true)
		}
		*opts.Replace = s
	}

	searchFiles(pattern, ignoreFileMatcher, acceptedFileMatcher)
}
Пример #6
0
func (get *GoGet) Start(config *GoGetConfig) {
	defer func() {
		if err := recover(); err != nil {
			get.DebugLog.Println(err)
			debug.PrintStack()
		}
	}()

	get.Url = config.Url
	get.Cnt = config.Cnt

	if config.ProxyType == PROXYHTTP {
		proxy := func(_ *http.Request) (*url.URL, error) {
			return url.Parse(config.Proxy)
		}

		get.GetClient.Transport = &http.Transport{Proxy: proxy}
	} else if config.ProxyType == PROXYSOCKS5 {
		dialer, err := proxy.SOCKS5("tcp", config.Proxy,
			nil,
			&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 30 * time.Second,
			},
		)
		if err != nil {
			return
		}

		get.GetClient.Transport = &http.Transport{
			Proxy:               nil,
			Dial:                dialer.Dial,
			TLSHandshakeTimeout: 10 * time.Second,
		}
	}

	req, err := http.NewRequest("HEAD", get.Url, nil)

	resp, err := get.GetClient.Do(req)

	if err != nil {
		log.Printf("Get %s error %v.\n", get.Url, err)
		return
	}
	get.Header = resp.Header
	get.MediaType, get.MediaParams, _ = mime.ParseMediaType(get.Header.Get("Content-Disposition"))
	if resp.ContentLength <= 0 {
		log.Printf("ContentLength error", resp.ContentLength)
		return
	}

	get.raw = make([]byte, resp.ContentLength, resp.ContentLength)
	get.Schedule = NewGoGetSchedules(resp.ContentLength)

	if get.MediaParams["filename"] != "" {
		get.FilePath += get.MediaParams["filename"]
	} else if i := strings.LastIndex(get.Url, "/"); i != -1 && i+1 <= len(get.Url) {
		get.FilePath += get.Url[i+1:]
	} else {
		hash := md5.New()
		hash.Write([]byte(get.Url))
		get.FilePath += base64.StdEncoding.EncodeToString(hash.Sum(nil))
	}

	get.File, err = os.Create(get.FilePath)
	if err != nil {
		log.Printf("Create file %s error %v.\n", get.FilePath, err)
		return
	}
	// log.Printf("Get %s MediaType:%s, Filename:%s, Size %d.\n", get.Url, get.MediaType, get.MediaParams["filename"], get.Schedule.ContentLength)
	if get.Header.Get("Accept-Ranges") != "" {
		// log.Printf("Server %s support Range by %s.\n", get.Header.Get("Server"), get.Header.Get("Accept-Ranges"))
	} else {
		log.Printf("Server %s doesn't support Range.\n", get.Header.Get("Server"))
	}

	log.Printf("Start to download %s(%d %s) with %d thread.\n", get.FilePath, resp.ContentLength, byten.Size(get.Schedule.ContentLength), get.Cnt)

	get.jobs = make(chan *GoGetBlock, get.Cnt)
	get.jobStatus = make(chan *GoGetBlock, get.Cnt)
	go get.producer()
	go get.consumer()

	for get.Schedule.Percent() != 1 && get.FailCnt < 300 {
		get.processBar.Process(int(get.Schedule.Percent()*100), get.Schedule.Speed())
		// fmt.Println(get.Schedule.CompleteLength, get.Schedule.ContentLength, get.Schedule.Percent())
		time.Sleep(1 * time.Second)
	}
	if get.Schedule.Percent() == 1 {
		// fmt.Println(get.Schedule.CompleteLength, get.Schedule.ContentLength, get.Schedule.Percent())
		get.processBar.Process(100, get.Schedule.Speed())
	}

	_, err = get.File.Write(get.raw)
	if err != nil {
		panic(err)
	}
	get.File.Close()

	h := md5.New()
	_, err = h.Write(get.raw)
	if err != nil {
		panic(err)
	}
	log.Printf("Download complete and store file %s. MD5: %x.\n", get.FilePath, h.Sum(nil))
	get.DebugLog.Println("========================================")
}