/
archonurl.go
215 lines (195 loc) · 4.8 KB
/
archonurl.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 common
import (
"fmt"
//permLayer "github.com/archoncloud/archoncloud-go/networking/archon-dht/permission_layer"
"github.com/pkg/errors"
"net/url"
"strconv"
"strings"
)
// Example: arc://george.eth.n2:7/photos/beach.jpg
type UrlPermission string
const (
Eth UrlPermission = "eth"
Neo UrlPermission = "neo"
)
type ArchonUrl struct {
Username string
Permission UrlPermission
Path string // File path or hash
Needed int // Min number of shard needed for reconstruction
Total int // Total number of shards to upload
}
// NewArchonUrl creates an Archon URL from the string representation
func NewArchonUrl(urlString string) (archonUrl *ArchonUrl, err error) {
if urlString == "" {
err = errors.New("missing Archon URL")
return
}
if !strings.HasPrefix(urlString, ArcProtocol+"://") {
err = fmt.Errorf( "should start with %q", ArcProtocol+"://")
return
}
err = errors.New("invalid Archon URL")
parsedUrl, err2 := url.Parse(urlString)
if err2 != nil {return}
if parsedUrl.Path == "" {
err = errors.Wrap(err,"path is empty")
return
}
archonUrl = new(ArchonUrl)
archonUrl.Path = parsedUrl.Path
hostParts := strings.Split(parsedUrl.Host, ".")
l := len(hostParts)
var tld string
switch l {
case 1:
// Hash
tld = hostParts[0]
if len(tld) > 4 || tld[0] != 'h' {
err = errors.Wrap(err, "missing h")
return
}
case 3:
// Named
archonUrl.Username = hostParts[0]
if archonUrl.Username == "" {
err = errors.Wrap(err, "missing user name")
return
}
if err2 := IsLegalUserName(archonUrl.Username); err2 != nil {
err = errors.Wrap(err, err2.Error())
return
}
perm := UrlPermission(hostParts[1])
switch perm {
case Eth, Neo:
archonUrl.Permission = perm
default:
err = errors.Wrap(err, "missing eth or neo")
return
}
tld = hostParts[2]
if len(tld) > 4 || tld[0] != 'n' {
err = errors.Wrap(err, "missing .n")
return
}
default: return
}
encoding := tld[1:]
if len(encoding) == 1 {
// whole file
if( encoding[0] != '0') {
err = errors.Wrap(err, "whole file encoding must be 0")
return
}
archonUrl.Needed = 0
archonUrl.Total = 1
} else {
// Sharded
nums := strings.Split(encoding, ":")
if len(nums) < 2 {return}
archonUrl.Needed, _ = strconv.Atoi(nums[0])
archonUrl.Total, _ = strconv.Atoi(nums[1])
if archonUrl.Needed <= 0 || archonUrl.Total <= 0 ||archonUrl.Needed > archonUrl.Total {
err = errors.Wrap(err, "invalid needed or total (0 < needed <= total)")
return
}
}
if archonUrl.IsHash() {
// Remove path separator
archonUrl.Path = strings.ReplaceAll(archonUrl.Path, "/", "")
_, err = ArchonHashFromString(archonUrl.Path)
} else {
err = IsLegalFilePath(archonUrl.Path);
}
return
}
func (a *ArchonUrl) Tld() string {
var s string
if a.IsHash() {
s = "h" // hash
} else {
s = "n" // named
}
if a.IsWholeFile() {
s += "0"
} else {
s += fmt.Sprintf( "%d:%d", a.Needed, a.Total)
}
return s
}
func (a *ArchonUrl) String() string {
if a.IsHash() {
return fmt.Sprintf("%s://%s/%s", ArcProtocol, a.Tld(), a.Path)
}
path := a.Path
if path[0] != '/' {
path = "/" + path
}
return fmt.Sprintf("%s://%s.%s.%s%s", ArcProtocol, a.Username, a.Permission, a.Tld(), path)
}
func (a *ArchonUrl) IsHash() bool {
return a.Username == ""
}
func (a *ArchonUrl) IsWholeFile() bool {
return a.Needed == 0
}
func (a *ArchonUrl) DownloadUrl(shardIx int) string {
url := DownloadEndpoint + "?"
if shardIx >= 0 {
url += fmt.Sprintf("shardIdx=%d&", shardIx)
}
return url + fmt.Sprintf(`archonUrl=%s`, a)
}
func (a *ArchonUrl) ShardPath(shardIx int) string {
if a.IsHash() {
if a.IsWholeFile() {
return fmt.Sprintf("%s.%s", a.Path, HashFileSuffix)
}
return fmt.Sprintf("%s.%d.%s", a.Path, shardIx, HashFileSuffix)
}
path := strings.Trim(a.Path, `/\`)
topFolder := fmt.Sprintf("%s/%s", string(a.Permission), a.Username)
if a.IsWholeFile() {
return fmt.Sprintf("%s/%s.%s", topFolder, path, WholeFileSuffix)
}
return fmt.Sprintf("%s/%s.%d.%s", topFolder, path, shardIx, ShardFileSuffix)
}
func (a *ArchonUrl) ShardPaths() (paths []string) {
if a.IsWholeFile() {
paths = append(paths, a.ShardPath(0))
} else {
for i := 0; i < a.Total; i++ {
paths = append(paths, a.ShardPath(i))
}
}
return
}
/*
func (a *ArchonUrl) PermissionLayerId() permission_layer.PermissionLayerID {
switch a.Permission {
case Eth: return permission_layer.EthPermissionId
case Neo: return permission_layer.NeoPermissionId
default: return permission_layer.NotPermissionId
}
}
*/
func IndexFromShardPath(shp string) (index int, err error) {
parts := strings.Split(shp, ".")
l := len(parts)
if l < 3 {
} else {
switch parts[l-1] {
case ShardFileSuffix:
case HashFileSuffix:
index, err = strconv.Atoi(parts[l-2])
if err == nil {
return
}
default:
}
}
err = errors.New("not a shard path")
return
}