add GetStreamSize

BY MAGIC!

add fileSize for stream update

fix mistake
This commit is contained in:
Niko Storni 2019-05-06 21:35:58 +02:00
parent 2ae66ad3f7
commit b63ee94973
No known key found for this signature in database
GPG key ID: F37FE63398800368
2 changed files with 61 additions and 2 deletions

View file

@ -266,7 +266,6 @@ type StreamCreateOptions struct {
StreamType *streamType `json:"stream_type,omitempty"` StreamType *streamType `json:"stream_type,omitempty"`
ReleaseTime *int64 `json:"release_time,omitempty"` ReleaseTime *int64 `json:"release_time,omitempty"`
Duration *uint64 `json:"duration,omitempty"` Duration *uint64 `json:"duration,omitempty"`
VideoDuration *uint64 `json:"video_duration,omitempty"` //TODO: this shouldn't exist
ImageWidth *uint `json:"image_width,omitempty"` ImageWidth *uint `json:"image_width,omitempty"`
ImageHeight *uint `json:"image_height,omitempty"` ImageHeight *uint `json:"image_height,omitempty"`
VideoWidth *uint `json:"video_width,omitempty"` VideoWidth *uint `json:"video_width,omitempty"`
@ -285,7 +284,7 @@ func (d *Client) StreamCreate(name, filePath string, bid float64, options Stream
Bid string `json:"bid"` Bid string `json:"bid"`
FilePath string `json:"file_path,omitempty"` FilePath string `json:"file_path,omitempty"`
FileSize *string `json:"file_size,omitempty"` FileSize *string `json:"file_size,omitempty"`
IncludeProtoBuf bool `json:"include_protobuf"` IncludeProtobuf bool `json:"include_protobuf"`
*StreamCreateOptions `json:",flatten"` *StreamCreateOptions `json:",flatten"`
}{ }{
Name: name, Name: name,
@ -319,6 +318,7 @@ type StreamUpdateOptions struct {
ClearLocations *bool `json:"clear_locations,omitempty"` ClearLocations *bool `json:"clear_locations,omitempty"`
Name *string `json:"name"` Name *string `json:"name"`
FilePath *string `json:"file_path,omitempty"` FilePath *string `json:"file_path,omitempty"`
FileSize *string `json:"file_size,omitempty"`
Bid *string `json:"bid"` Bid *string `json:"bid"`
*StreamCreateOptions `json:",flatten"` *StreamCreateOptions `json:",flatten"`
} }

View file

@ -1,10 +1,15 @@
package jsonrpc package jsonrpc
import ( import (
"encoding/hex"
"encoding/json" "encoding/json"
"io/ioutil"
"net/http"
"reflect" "reflect"
"github.com/lbryio/lbry.go/extras/errors" "github.com/lbryio/lbry.go/extras/errors"
"github.com/lbryio/lbry.go/stream"
schema "github.com/lbryio/lbryschema.go/claim" schema "github.com/lbryio/lbryschema.go/claim"
lbryschema "github.com/lbryio/types/v2/go" lbryschema "github.com/lbryio/types/v2/go"
@ -296,6 +301,60 @@ type Claim struct {
Value lbryschema.Claim `json:"protobuf"` Value lbryschema.Claim `json:"protobuf"`
} }
const reflectorURL = "http://blobs.lbry.io/"
// GetStreamSizeByMagic uses "magic" to not just estimate, but actually return the exact size of a stream
// It does so by fetching the sd blob and the last blob from our S3 bucket, decrypting and unpadding the last blob
// adding up all full blobs that have a known size and finally adding the real last blob size too.
// This will only work if we host at least the sd blob and the last blob on S3, if not, this will error.
func (c *Claim) GetStreamSizeByMagic() (uint64, error) {
if c.Value.GetStream() == nil {
return 0, errors.Err("this claim is not a stream")
}
resp, err := http.Get(reflectorURL + hex.EncodeToString(c.Value.GetStream().Source.SdHash))
if err != nil {
return 0, errors.Err(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, errors.Err(err)
}
sdb := &stream.SDBlob{}
err = sdb.UnmarshalJSON(body)
if err != nil {
return 0, err
}
lastBlobIndex := len(sdb.BlobInfos) - 2
lastBlobHash := sdb.BlobInfos[lastBlobIndex].BlobHash
var streamSize uint64 = 0
if len(sdb.BlobInfos) > 2 {
streamSize = uint64(stream.MaxBlobSize-1) * uint64(len(sdb.BlobInfos)-2)
}
resp2, err := http.Get(reflectorURL + hex.EncodeToString(lastBlobHash))
if err != nil {
return 0, errors.Err(err)
}
defer resp2.Body.Close()
body2, err := ioutil.ReadAll(resp2.Body)
if err != nil {
return 0, errors.Err(err)
}
lastBlob, err := stream.DecryptBlob(body2, sdb.Key, sdb.BlobInfos[lastBlobIndex].IV)
if err != nil {
return 0, errors.Err(err)
}
streamSize += uint64(len(lastBlob))
return streamSize, nil
}
type ClaimListResponse []Claim type ClaimListResponse []Claim
type ClaimListMineResponse struct { type ClaimListMineResponse struct {