Skip to content

Commit

Permalink
feat(display): add progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-heaven committed Aug 24, 2022
1 parent 371f74e commit 6096777
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
24 changes: 24 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,44 @@ func Info(msg ...interface{}) {
}
}

func Infof(format string, a interface{}) {
if consts.LogLevelInfo <= logLevel {
fmt.Printf(format, a)
}
}

func Error(msg ...interface{}) {
if consts.LogLevelError <= logLevel {
fmt.Println(msg...)
}
}

func Errorf(format string, a interface{}) {
if consts.LogLevelError <= logLevel {
fmt.Printf(format, a)
}
}

func Warn(msg ...interface{}) {
if consts.LogLevelWarn <= logLevel {
fmt.Println(msg...)
}
}

func Warnf(format string, a interface{}) {
if consts.LogLevelWarn <= logLevel {
fmt.Printf(format, a)
}
}

func Debug(msg ...interface{}) {
if consts.LogLevelDebug <= logLevel {
fmt.Println(msg...)
}
}

func Debugf(format string, a interface{}) {
if consts.LogLevelDebug <= logLevel {
fmt.Printf(format, a)
}
}
7 changes: 3 additions & 4 deletions processor/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package processor

import (
"fmt"
"os"
"path"
"strings"

"github.com/foamzou/audio-get/consts"
"github.com/foamzou/audio-get/ffmpeg"
"github.com/foamzou/audio-get/logger"
"github.com/foamzou/audio-get/meta"
"github.com/foamzou/audio-get/utils"
"os"
"path"
"strings"
)

func (p *Processor) download(mediaMeta *meta.MediaMeta) error {
Expand Down
51 changes: 46 additions & 5 deletions utils/wget.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,48 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"

"github.com/foamzou/audio-get/logger"
)

type WgetBinaryProgressReader struct {
io.Reader
Total int64
Current int64
}

func (r *WgetBinaryProgressReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)

r.Current += int64(n)
logger.Infof("\rdownloading percent: %.2f%%", float64(r.Current*10000/r.Total)/100)
return
}

type TCPProgressReader struct {
net.Conn
Init bool
Current int64
Total int64
}

func (r *TCPProgressReader) Read(p []byte) (n int, err error) {
n, err = r.Conn.Read(p)
if !r.Init {
//fmt.Println(string(p[:]))
r.Init = true
}

// TODO: parse Total from first package
//logger.Infof("\rdownloading percent: %.2f%%", float64(r.Current*10000/r.Total)/100)
return
}

func WgetBinary(url string, downloadTo string, headers map[string]string) error {
out, err := os.Create(downloadTo)
if err != nil {
Expand Down Expand Up @@ -44,7 +78,12 @@ func WgetBinary(url string, downloadTo string, headers map[string]string) error
return errors.New("target is not a binary")
}

_, err = io.Copy(out, resp.Body)
reader := &WgetBinaryProgressReader{
Reader: resp.Body,
Total: resp.ContentLength,
}
_, err = io.Copy(out, reader)
logger.Info("\n")
if err != nil {
return err
}
Expand Down Expand Up @@ -75,13 +114,15 @@ func DownloadBinaryWithTCP(inputUrl string, downloadTo string, headers map[strin
if err != nil {
return err
}

resp, err := ioutil.ReadAll(conn)
reader := &TCPProgressReader{
Conn: conn,
}
resp, err := io.ReadAll(reader)
if err != nil {
return err
}

err = ioutil.WriteFile(downloadTo, resp, 0600)
err = os.WriteFile(downloadTo, resp, 0600)
if err != nil {
return err
}
Expand Down

0 comments on commit 6096777

Please sign in to comment.