-
Notifications
You must be signed in to change notification settings - Fork 0
/
HfLink.go
215 lines (195 loc) · 5.22 KB
/
HfLink.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package httpfsclient
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"path"
"reflect"
"strings"
)
// HfLink := clusterId:serverId/relativePath
type HfLink string
func IsHfLink(url string) bool {
i := strings.Index(url, "/")
if i == -1 {
return false
}
head := url[0:i]
return strings.Index(head, ":") != -1
}
func NewHfLink(clusterId, serverId, filepath string) HfLink {
return HfLink(path.Join(clusterId+":"+serverId, filepath))
}
func urlPathStartIndex(url string) int {
pi := strings.Index(url, "://")
if -1 == pi {
return strings.Index(url, "/")
}
i := strings.Index(url[pi+3:], "/")
if -1 == i {
return -1
}
return i + 3 + pi
}
// http://xxx/image/1.jpg -> s:1/image/1.jpg
func FromUrl(url string) (HfLink, bool) {
pi := strings.Index(url, "://")
i := strings.Index(url[pi+3:], "/") + 3 + pi
proxy := url[0:i]
clusterId, serverId := GetClusters().HfsId(proxy)
if clusterId == "" || serverId == "" {
return HfLink(url), false
}
return NewHfLink(clusterId, serverId, url[i:]), true
}
// eg. s:1/txt/00/00/yyfoatapk5/bdu9kjosiq.go -> http://xxx/txt/00/00/yyfoatapk5/bdu9kjosiq.go
func (d HfLink) Url() string {
clusterId, serverId, path := d.Parts()
return GetClusters().GetServer(clusterId, serverId).Proxy + path
}
func (d HfLink) String() string {
return string(d)
}
func (d HfLink) Stat() (FileInfo, error) {
clusterId, serverId, path := d.Parts()
server := GetServer(clusterId, serverId)
if "" == server.ClusterId {
return FileInfo{}, errors.New("no such server:" + string(d))
}
return (&Client{Server: server.Local}).Stat(path)
}
func (d HfLink) Read() ([]byte, error) {
clusterId, serverId, path := d.Parts()
server := GetServer(clusterId, serverId)
if "" == server.ClusterId {
return nil, errors.New("no such server:" + string(d))
}
return (&Client{Server: server.Local}).Read(path)
}
func (d HfLink) Call(module, method string, args, result interface{}) error {
clusterId, serverId, _ := d.Parts()
return Methods{}.Call(clusterId, serverId, module, method, args, result)
}
func (d HfLink) CallAsync(module, method string, args interface{}) error {
clusterId, serverId, _ := d.Parts()
return Methods{}.CallAsync(clusterId, serverId, module, method, args)
}
func (d HfLink) ImageResize(crop []int, sizes [][]int) ([]HfLink, error) {
return Methods{}.ImageCropResize(d, crop, sizes)
}
func (d HfLink) VideoCompressDash(videoId int, redisProgressKey string) error {
return Methods{}.VideoCompressDash(d, videoId, redisProgressKey)
}
func (d HfLink) Mp4(videoId int, redisProgressKey string) error {
return Methods{}.Mp4(d, videoId, redisProgressKey)
}
func (d HfLink) Path() string {
ds := string(d)
i := strings.Index(ds, "/")
if i == -1 || '/' == ds[0] {
return ds
}
return ds[i:]
}
func (d HfLink) Parts() (string, string, string) {
ds := string(d)
i := strings.Index(ds, "/")
if i == -1 || '/' == ds[0] {
return "", "", ds
}
server := ds[0:i]
j := strings.Index(server, ":")
if j == -1 {
return "", "", ds
}
return server[0:j], server[j+1:], ds[i:]
}
type NullHfLink struct{ sql.NullString }
// NewString creates a new String
func NewNullHfLink(s HfLink, valid bool) NullHfLink {
return NullHfLink{
NullString: sql.NullString{
String: string(s),
Valid: valid,
},
}
}
func FromHfLink(s HfLink) NullHfLink {
return NullHfLink{
NullString: sql.NullString{
String: string(s),
Valid: len(s) == 0,
},
}
}
func (s *NullHfLink) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
if err = json.Unmarshal(data, &v); err != nil {
return err
}
switch x := v.(type) {
case string:
if IsHfLink(x) {
s.String = x
} else {
dd, _ := FromUrl(x)
s.String = string(dd)
}
case map[string]interface{}:
err = json.Unmarshal(data, &s.NullString)
case nil:
s.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name())
}
s.Valid = err == nil
return err
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this String is null.
func (s NullHfLink) MarshalJSON() ([]byte, error) {
if !s.Valid {
return []byte("null"), nil
}
return json.Marshal(HfLink(s.String).Url())
}
func (s NullHfLink) Url() string {
return HfLink(s.String).Url()
}
func (s NullHfLink) HfLink() HfLink {
return HfLink(s.String)
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string when this String is null.
func (s NullHfLink) MarshalText() ([]byte, error) {
if !s.Valid {
return []byte{}, nil
}
return []byte(HfLink(s.String).Url()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null String if the input is a blank string.
func (s *NullHfLink) UnmarshalText(text []byte) error {
s.String = HfLink(string(text)).Url()
s.Valid = s.String != ""
return nil
}
// SetValid changes this String's value and also sets it to be non-null.
func (s *NullHfLink) SetValid(v string) {
s.String = v
s.Valid = true
}
// Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
func (s NullHfLink) Ptr() *string {
if !s.Valid {
return nil
}
return &s.String
}
// IsZero returns true for null strings, for potential future omitempty support.
func (s NullHfLink) IsZero() bool {
return !s.Valid
}