slideshow/internal/streams.go

74 lines
1.6 KiB
Go
Raw Normal View History

package internal
import (
"errors"
"io"
"os"
"os/exec"
"strings"
"time"
"git.velvettear.de/velvettear/loggo"
)
// pipe data from a stream to 'feh'
func streamToFeh(stream io.ReadCloser) error {
timestamp := time.Now().UnixMilli()
defer stream.Close()
cmd := exec.Command("feh", "--no-fehbg", "--bg-fill", "-")
stdin, stdinError := cmd.StdinPipe()
if stdinError != nil {
return stdinError
}
defer stdin.Close()
stdout, stdoutError := cmd.StdoutPipe()
if stdoutError != nil {
return stdoutError
}
defer stdout.Close()
stderr, stderrError := cmd.StderrPipe()
if stderrError != nil {
return stderrError
}
defer stderr.Close()
cmd.Start()
written, copyError := io.Copy(stdin, stream)
if copyError != nil {
return copyError
}
if written == 0 {
_, stdoutError = io.ReadAll(stdout)
if stdoutError != nil {
return stdoutError
}
errorBytes, stderrError := io.ReadAll(stderr)
if stderrError != nil {
return stderrError
}
error := strings.TrimSpace(string(errorBytes))
if len(error) > 0 {
return errors.New(error)
}
}
stdin.Close()
cmd.Wait()
loggo.DebugTimed("successfully piped the data stream to 'feh''", timestamp, "size: "+FormatBytes(written))
return nil
}
// write data from a stream to a file
func streamToFile(stream io.ReadCloser, path string) error {
timestamp := time.Now().UnixMilli()
defer stream.Close()
file, error := os.Create(path)
if error != nil {
return error
}
written, error := io.Copy(file, stream)
if error != nil {
return error
}
loggo.DebugTimed("successfully piped the data stream to file '"+path+"'", timestamp, "size: "+FormatBytes(written))
return nil
}