-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the mimetype check, implement the cool progress checker from go-g…
…etter
- Loading branch information
1 parent
ec66ea6
commit afdddb8
Showing
4 changed files
with
220 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// borrowed from github.com/hashicorp/go-getter/cmd | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/cheggaaa/pb" | ||
getter "github.com/hashicorp/go-getter" | ||
) | ||
|
||
// defaultProgressBar is the default instance of a cheggaaa | ||
// progress bar. | ||
var defaultProgressBar getter.ProgressTracker = &ProgressBar{} | ||
|
||
// ProgressBar wraps a github.com/cheggaaa/pb.Pool | ||
// in order to display download progress for one or multiple | ||
// downloads. | ||
// | ||
// If two different instance of ProgressBar try to | ||
// display a progress only one will be displayed. | ||
// It is therefore recommended to use DefaultProgressBar | ||
type ProgressBar struct { | ||
// lock everything below | ||
lock sync.Mutex | ||
|
||
pool *pb.Pool | ||
|
||
pbs int | ||
} | ||
|
||
// ProgressBarConfig . | ||
func ProgressBarConfig(bar *pb.ProgressBar, prefix string) { | ||
bar.SetUnits(pb.U_BYTES) | ||
bar.Prefix(prefix) | ||
} | ||
|
||
// TrackProgress instantiates a new progress bar that will | ||
// display the progress of stream until closed. | ||
// total can be 0. | ||
func (cpb *ProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { | ||
cpb.lock.Lock() | ||
defer cpb.lock.Unlock() | ||
|
||
newPb := pb.New64(totalSize) | ||
newPb.Set64(currentSize) | ||
ProgressBarConfig(newPb, filepath.Base(src)) | ||
if cpb.pool == nil { | ||
cpb.pool = pb.NewPool() | ||
cpb.pool.Start() | ||
} | ||
cpb.pool.Add(newPb) | ||
reader := newPb.NewProxyReader(stream) | ||
|
||
cpb.pbs++ | ||
return &readCloser{ | ||
Reader: reader, | ||
close: func() error { | ||
cpb.lock.Lock() | ||
defer cpb.lock.Unlock() | ||
|
||
newPb.Finish() | ||
cpb.pbs-- | ||
if cpb.pbs <= 0 { | ||
cpb.pool.Stop() | ||
cpb.pool = nil | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
type readCloser struct { | ||
io.Reader | ||
close func() error | ||
} | ||
|
||
func (c *readCloser) Close() error { return c.close() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.