Track and show uploaded file (percentage) - Go lang

I am making a process that downloads a file via a passed url parameter. the download is doing correctly, but I cannot print a summary of the download percentage. (every second)

ive built one simulated view of this resume but it doesn't get loaded, it just show how I want it.

I tried to bring io.copy inside my source so I can change its ant print while copying, but it doesn't work.

Can anyone help me? thank

 package main

    import (
        "fmt"
        "io"
        "net/http"
        "os"
        "strings"
    //  "time"
    )

    func downloadFromUrl(url string) {
        tokens := strings.Split(url, "/")
        fileName := tokens[len(tokens)-1]
        fmt.Println("Downloading", url, "to", fileName)

    //create file
    output, err := os.Create(fileName)
    if err != nil {
        fmt.Println("Error while creating", fileName, "-", err)
        return
    }
    fmt.Println("Creating", fileName)
    defer output.Close()

    //get url
    response, err := http.Get(url)
    if err != nil {
        fmt.Println("Error while downloading", url, "-", err)
        return
    }           
    defer response.Body.Close()

    //copy and bytes        
    n, err := io.Copy(output, response.Body)
    if err != nil {
        fmt.Println("Error while downloading", url, "-", err)
        return
    }

        //track progress
        for i := 1; float64(i) < float64(n); i++ {
            fmt.Println("",float64(i),float64(n))
            percent := float64(i) / (float64(n) / float64(100))
            percentTot := fmt.Sprintf(" %#.02f%% ", percent)
            fmt.Println(percentTot,"downloaded\n")
            //time.Sleep(3000 * time.Millisecond)
        }   


    }

    func main() {
        //read args 
        args := os.Args

        //loop args
        for i := 1; i < len(args); i++ {
            url := args[i]
            //call download         
            downloadFromUrl(url)        
        }
    }


**Output:**
go run main.go http://download.geonames.org/export/dump/AD.zip
Downloading http://download.geonames.org/export/dump/AD.zip to AD.zip
Creating AD.zip
 1 64639
 0.00%  downloaded

 2 64639
 0.00%  downloaded

 3 64639
 0.00%  downloaded

 4 64639
 0.01%  downloaded

 5 64639
 0.01%  downloaded

      

+3


source to share


1 answer


I did this in my senvgo project with a wrapperio.Reader

// PassThru wraps an existing io.Reader.
//
// It simply forwards the Read() call, while displaying
// the results from individual calls to it.
type PassThru struct {
    io.Reader
    total    int64 // Total # of bytes transferred
    length   int64 // Expected length
    progress float64
}

      

You define a method Read()

on this shell (to make it io.Reader

)

// Read 'overrides' the underlying io.Reader Read method.
// This is the one that will be called by io.Copy(). We simply
// use it to keep track of byte counts and then forward the call.
func (pt *PassThru) Read(p []byte) (int, error) {
    n, err := pt.Reader.Read(p)
    if n > 0 {
        pt.total += int64(n)
        percentage := float64(pt.total) / float64(pt.length) * float64(100)
        i := int(percentage / float64(10))
        is := fmt.Sprintf("%v", i)
        if percentage-pt.progress > 2 {
            fmt.Fprintf(os.Stderr, is)
            pt.progress = percentage
        }
    }

    return n, err
}

      

And you use this one io.Reader

to read Response

(the result of your HTTP request):



client := &http.Client{
    CheckRedirect: redirectPolicyFunc,
}
response, err := client.Get("http://example.com/something/large.zip")
defer response.Body.Close()
readerpt := &PassThru{Reader: response.Body, length: response.ContentLength}
body, err := ioutil.ReadAll(readerpt)

      

The call ReadAll()

causes the actual load, and PassThru.Read()

will print the% loaded with the expected length ( response.ContentLength

)


Inspirations:

+7


source







All Articles