aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/download.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2021-01-09 00:49:16 -0500
committerMitch Riedstra <mitch@riedstra.us>2021-01-09 00:49:16 -0500
commite31c9168627c040317e5cc8566724f88910439ae (patch)
tree0ce4b3de81adfe8f9af56c498a969a8a2be44280 /cmd/web/download.go
parentd762cdbae06efd194ffac7b976c16aac21a26f94 (diff)
downloadsteam-export-e31c9168627c040317e5cc8566724f88910439ae.tar.gz
steam-export-e31c9168627c040317e5cc8566724f88910439ae.tar.xz
Add some download stats, and a status endpoint.
Diffstat (limited to 'cmd/web/download.go')
-rw-r--r--cmd/web/download.go41
1 files changed, 35 insertions, 6 deletions
diff --git a/cmd/web/download.go b/cmd/web/download.go
index 93c4ff5..1c70717 100644
--- a/cmd/web/download.go
+++ b/cmd/web/download.go
@@ -1,7 +1,10 @@
package main
import (
+ "io"
"net/http"
+ "time"
+ "fmt"
"github.com/gorilla/mux"
)
@@ -20,14 +23,40 @@ func gameDownloader(w http.ResponseWriter, r *http.Request) {
}
w.Header().Add("Content-type", "application/tar")
+ w.Header().Add("Estimated-size", fmt.Sprintf("%d", g.Size))
Logger.Printf("Client %s is downloading: %s", r.RemoteAddr, game)
- err := g.Package(w)
- if err != nil {
- Logger.Printf("Client %s Error Sending game: %s", r.RemoteAddr, err)
- // Headers already sent, don't bother sending an error
- return
+
+ // Invert the writer so we can break up the copy and get progress
+ // information in here
+ rdr, pwrtr := io.Pipe()
+ go func() {
+ err := g.Package(pwrtr)
+ if err != nil {
+ Logger.Println("Error in package writing: ", err)
+ }
+ }()
+
+ var total int64
+
+ start := time.Now()
+ for {
+ n, err := io.CopyN(w, rdr, 256*1024*1024)
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ Logger.Printf("Client %s Error Sending game: %s", r.RemoteAddr, err)
+ // Headers already sent, don't bother sending an error
+ return
+ }
+ total += n
+ mb := float64(total / 1024 / 1024)
+ rate := mb / time.Since(start).Seconds()
+
+ Logger.Printf("Client %s is downloading %s: %0.1f%% done %.2f mb/s",
+ r.RemoteAddr, game, float64(total)/float64(g.Size)*100, rate)
}
+
Logger.Printf("Client %s finished downloading: %s", r.RemoteAddr, game)
}
-