2019-09-11 18:30:01 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-10-03 22:13:08 +02:00
|
|
|
"encoding/hex"
|
2019-09-11 18:30:01 +02:00
|
|
|
"os"
|
2020-10-06 00:07:06 +02:00
|
|
|
"time"
|
2019-09-11 18:30:01 +02:00
|
|
|
|
2021-07-23 22:35:41 +02:00
|
|
|
"github.com/lbryio/reflector.go/server/peer"
|
2019-10-03 22:34:57 +02:00
|
|
|
"github.com/lbryio/reflector.go/store"
|
2019-09-11 18:30:01 +02:00
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2019-10-03 22:13:08 +02:00
|
|
|
|
2019-09-11 18:30:01 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
Use: "getstream ADDRESS:PORT SDHASH",
|
|
|
|
Short: "Get a stream from a reflector server",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
Run: getStreamCmd,
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStreamCmd(cmd *cobra.Command, args []string) {
|
|
|
|
addr := args[0]
|
|
|
|
sdHash := args[1]
|
|
|
|
|
2020-10-22 19:12:31 +02:00
|
|
|
s := store.NewCachingStore(
|
2020-10-29 17:39:53 +01:00
|
|
|
"getstream",
|
2019-11-22 12:53:42 +01:00
|
|
|
peer.NewStore(peer.StoreOpts{Address: addr}),
|
2020-10-22 19:12:31 +02:00
|
|
|
store.NewDiskStore("/tmp/lbry_downloaded_blobs", 2),
|
2019-10-03 22:13:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
2019-09-11 18:30:01 +02:00
|
|
|
if err != nil {
|
2019-10-03 22:13:08 +02:00
|
|
|
log.Fatal(err)
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
var sd stream.SDBlob
|
2019-09-11 18:30:01 +02:00
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
sdb, _, err := s.Get(sdHash)
|
2019-09-11 18:30:01 +02:00
|
|
|
if err != nil {
|
2019-09-27 20:38:49 +02:00
|
|
|
log.Fatal(err)
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
err = sd.FromBlob(sdb)
|
2019-09-11 18:30:01 +02:00
|
|
|
if err != nil {
|
2019-10-03 22:13:08 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:07:06 +02:00
|
|
|
filename := sd.SuggestedFileName
|
|
|
|
if filename == "" {
|
|
|
|
filename = "stream_" + time.Now().Format("20060102_150405")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(wd + "/" + filename)
|
2019-10-03 22:13:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(sd.BlobInfos)-1; i++ {
|
2021-01-09 05:08:20 +01:00
|
|
|
b, _, err := s.Get(hex.EncodeToString(sd.BlobInfos[i].BlobHash))
|
2019-10-03 22:13:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Plaintext(sd.Key, sd.BlobInfos[i].IV)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
}
|