/
os.go
97 lines (88 loc) · 2.3 KB
/
os.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Object store helper functions
*/
package core
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/livepeer/go-livepeer/clog"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/net"
"github.com/livepeer/go-tools/drivers"
)
func GetSegmentData(ctx context.Context, uri string) ([]byte, error) {
return getSegmentDataHTTP(ctx, uri)
}
var httpc = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
Timeout: common.HTTPTimeout / 2,
}
func FromNetOsInfo(os *net.OSInfo) *drivers.OSInfo {
if os == nil {
return nil
}
return &drivers.OSInfo{
StorageType: drivers.OSInfo_StorageType(os.StorageType),
S3Info: FromNetS3Info(os.S3Info),
}
}
func FromNetS3Info(storage *net.S3OSInfo) *drivers.S3OSInfo {
if storage == nil {
return nil
}
return &drivers.S3OSInfo{
Host: storage.Host,
Key: storage.Key,
Policy: storage.Policy,
Signature: storage.Signature,
Credential: storage.Credential,
XAmzDate: storage.XAmzDate,
}
}
func ToNetOSInfo(os *drivers.OSInfo) *net.OSInfo {
if os == nil {
return nil
}
return &net.OSInfo{
StorageType: net.OSInfo_StorageType(os.StorageType),
S3Info: ToNetS3Info(os.S3Info),
}
}
func ToNetS3Info(storage *drivers.S3OSInfo) *net.S3OSInfo {
if storage == nil {
return nil
}
return &net.S3OSInfo{
Host: storage.Host,
Key: storage.Key,
Policy: storage.Policy,
Signature: storage.Signature,
Credential: storage.Credential,
XAmzDate: storage.XAmzDate,
}
}
func getSegmentDataHTTP(ctx context.Context, uri string) ([]byte, error) {
clog.V(common.VERBOSE).Infof(ctx, "Downloading uri=%s", uri)
started := time.Now()
resp, err := httpc.Get(uri)
if err != nil {
clog.Errorf(ctx, "Error getting HTTP uri=%s err=%q", uri, err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
clog.Errorf(ctx, "Non-200 response for status=%v uri=%s", resp.Status, uri)
return nil, fmt.Errorf(resp.Status)
}
body, err := common.ReadAtMost(resp.Body, common.MaxSegSize)
if err != nil {
clog.Errorf(ctx, "Error reading body uri=%s err=%q", uri, err)
return nil, err
}
took := time.Since(started)
clog.V(common.VERBOSE).Infof(ctx, "Downloaded uri=%s dur=%s bytes=%d", uri, took, len(body))
return body, nil
}