github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/util/httpUtils.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package util
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/getgauge/common"
    28  )
    29  
    30  // progressReader is for indicating the download / upload progress on the console
    31  type progressReader struct {
    32  	io.Reader
    33  	bytesTransfered   int64
    34  	totalBytes        int64
    35  	progress          float64
    36  	progressDisplayed bool
    37  }
    38  
    39  // Read overrides the underlying io.Reader's Read method.
    40  // io.Copy() will be calling this method.
    41  func (w *progressReader) Read(p []byte) (int, error) {
    42  	n, err := w.Reader.Read(p)
    43  	if n > 0 {
    44  		w.bytesTransfered += int64(n)
    45  		percent := float64(w.bytesTransfered) * float64(100) / float64(w.totalBytes)
    46  		if percent-w.progress > 4 {
    47  			fmt.Print(".")
    48  			w.progress = percent
    49  			w.progressDisplayed = true
    50  		}
    51  	}
    52  	return n, err
    53  }
    54  
    55  // Download fires a HTTP GET request to download a resource to target directory
    56  func Download(url, targetDir, fileName string, silent bool) (string, error) {
    57  	if !common.DirExists(targetDir) {
    58  		return "", fmt.Errorf("Error downloading file: %s\nTarget dir %s doesn't exists.", url, targetDir)
    59  	}
    60  
    61  	if fileName == "" {
    62  		fileName = filepath.Base(url)
    63  	}
    64  	targetFile := filepath.Join(targetDir, fileName)
    65  
    66  	resp, err := http.Get(url)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  	if resp.StatusCode >= 400 {
    71  		return "", fmt.Errorf("Error downloading file: %s.\n%s", url, resp.Status)
    72  	}
    73  	defer resp.Body.Close()
    74  
    75  	out, err := os.Create(targetFile)
    76  	if err != nil {
    77  		return "", err
    78  	}
    79  	defer out.Close()
    80  	if silent {
    81  		_, err = io.Copy(out, resp.Body)
    82  	} else {
    83  		progressReader := &progressReader{Reader: resp.Body, totalBytes: resp.ContentLength}
    84  		_, err = io.Copy(out, progressReader)
    85  		if progressReader.progressDisplayed {
    86  			fmt.Println()
    87  		}
    88  	}
    89  	return targetFile, err
    90  }