forked from livepeer/go-livepeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivers.go
83 lines (71 loc) · 1.98 KB
/
drivers.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
// Package drivers abstracts different object storages, such as local, s3
package drivers
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/net"
)
// NodeStorage is current node's primary driver
var NodeStorage OSDriver
// OSDriver common interface for Object Storage
type OSDriver interface {
NewSession(path string) OSSession
}
type OSSession interface {
SaveData(name string, data []byte) (string, error)
EndSession()
// Info in order to have this session used via RPC
GetInfo() *net.OSInfo
// Indicates whether data may be external to this node
IsExternal() bool
}
// NewSession returns new session based on OSInfo received from the network
func NewSession(info *net.OSInfo) OSSession {
if info == nil {
return nil
}
switch info.StorageType {
case net.OSInfo_S3:
return newS3Session(info.S3Info)
case net.OSInfo_GOOGLE:
return newGSSession(info.S3Info)
}
return nil
}
func IsOwnExternal(uri string) bool {
return IsOwnStorageS3(uri) || IsOwnStorageGS(uri)
}
func GetSegmentData(uri string) ([]byte, error) {
return getSegmentDataHTTP(uri)
}
var httpc = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
Timeout: common.HTTPTimeout / 2,
}
func getSegmentDataHTTP(uri string) ([]byte, error) {
glog.V(common.VERBOSE).Infof("Downloading uri=%s", uri)
started := time.Now()
resp, err := httpc.Get(uri)
if err != nil {
glog.Errorf("Error getting HTTP uri=%s err=%v", uri, err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
glog.Errorf("Non-200 response for status=%v uri=%s", resp.Status, uri)
return nil, fmt.Errorf(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
glog.Errorf("Error reading body uri=%s err=%v", uri, err)
return nil, err
}
took := time.Since(started)
glog.V(common.VERBOSE).Infof("Downloaded uri=%s dur=%s", uri, took)
return body, nil
}