remove file downloads
This commit is contained in:
@@ -2,11 +2,7 @@ package page
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
@@ -27,29 +23,6 @@ const (
|
||||
// defaultProclamationPlayCount int = 1
|
||||
)
|
||||
|
||||
// ProgressUpdate is sent over a channel as bytes are read from the network
|
||||
// during a download. Read/Total let a consumer compute percentage; Done
|
||||
// signals a clean finish; Err signals failure. The channel carrying these
|
||||
// is closed after the final update (whether that's a Done or an Err), so
|
||||
// ranging over it is sufficient to know when the download has finished.
|
||||
type ProgressUpdate struct {
|
||||
Read int64
|
||||
Total int64
|
||||
Done bool
|
||||
Err error
|
||||
}
|
||||
|
||||
// ProgressReader wraps an io.Reader and emits a ProgressUpdate on every
|
||||
// Read() call. It never blocks on the send: if nobody is currently
|
||||
// listening on updates (or the consumer is behind), the update is dropped
|
||||
// rather than stalling the download itself.
|
||||
type ProgressReader struct {
|
||||
Reader io.Reader
|
||||
Total int64
|
||||
read int64
|
||||
updates chan<- ProgressUpdate
|
||||
}
|
||||
|
||||
type ScrollerPage struct {
|
||||
Base
|
||||
|
||||
@@ -91,64 +64,6 @@ type ScrollerPage struct {
|
||||
useColorPalettes bool
|
||||
}
|
||||
|
||||
func (pr *ProgressReader) Read(p []byte) (int, error) {
|
||||
n, err := pr.Reader.Read(p)
|
||||
if n > 0 {
|
||||
pr.read += int64(n)
|
||||
select {
|
||||
case pr.updates <- ProgressUpdate{Read: pr.read, Total: pr.Total}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// downloadFile starts the download in its own goroutine and returns
|
||||
// immediately with a channel of progress updates. The caller ranges over
|
||||
// the returned channel to observe progress; the channel is closed when the
|
||||
// download finishes, successfully or not, so the loop ends naturally.
|
||||
func downloadFile(url, destPath string) <-chan ProgressUpdate {
|
||||
updates := make(chan ProgressUpdate, 16)
|
||||
|
||||
go func() {
|
||||
defer close(updates)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
updates <- ProgressUpdate{Err: err}
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
updates <- ProgressUpdate{Err: fmt.Errorf("bad status: %s", resp.Status)}
|
||||
return
|
||||
}
|
||||
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
updates <- ProgressUpdate{Err: err}
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
pr := &ProgressReader{
|
||||
Reader: resp.Body,
|
||||
Total: resp.ContentLength, // -1 if server didn't send it
|
||||
updates: updates,
|
||||
}
|
||||
|
||||
if _, err := io.Copy(out, pr); err != nil {
|
||||
updates <- ProgressUpdate{Err: err}
|
||||
return
|
||||
}
|
||||
|
||||
updates <- ProgressUpdate{Read: pr.read, Total: pr.Total, Done: true}
|
||||
}()
|
||||
|
||||
return updates
|
||||
}
|
||||
|
||||
type ScrollerProclamation struct {
|
||||
db.Proclamation
|
||||
colorPattern.ColorPattern
|
||||
@@ -351,33 +266,6 @@ func (p *ScrollerPage) Init(ctx *PageCtx) error {
|
||||
|
||||
if err := p.Context.DbPtr.Where("Selected = ?", true).First(&selectedAudio).Error; err != nil {
|
||||
logger.Error("error querying database for selected audio: %v", err)
|
||||
} else {
|
||||
_, err = os.Stat(selectedAudio.Path)
|
||||
if err != nil { // If err == nil, then the file was most likely not found
|
||||
url := "https://files.radiantwavetech.com/audio/" + filepath.Base(selectedAudio.Path)
|
||||
p.IsDownloadingAudio = true
|
||||
updates := downloadFile(url, selectedAudio.Path)
|
||||
var downloadErr error
|
||||
for u := range updates {
|
||||
if u.Err != nil {
|
||||
downloadErr = u.Err
|
||||
break
|
||||
}
|
||||
if u.Total > 0 {
|
||||
pct := float64(u.Read) / float64(u.Total) * 100
|
||||
fmt.Printf("\rDownloaded %d / %d bytes (%.1f%%)\n", u.Read, u.Total, pct)
|
||||
} else {
|
||||
fmt.Printf("\rDownloaded %d bytes\n", u.Read)
|
||||
}
|
||||
if u.Done {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
if downloadErr != nil {
|
||||
p.IsDownloadingAudio = false
|
||||
return fmt.Errorf("failed to downoad audio file: %v", downloadErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info("Playing %s", selectedAudio.Path)
|
||||
|
||||
Reference in New Issue
Block a user