/
url.go
58 lines (43 loc) · 1.59 KB
/
url.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
package ipfs
import (
"fmt"
"net/url"
"strings"
"github.com/ipfs/go-cid"
)
func ParseURL(contentURL string) (endpoint, path string, err error) {
parsedURL, err := url.Parse(contentURL)
if err != nil {
return "", "", fmt.Errorf("parse content url: %w", err)
}
if parsedURL.Scheme == "ipfs" || parsedURL.Scheme == "ipns" {
ipfsCid := parsedURL.Host
urlPath := fmt.Sprintf("%s%s", parsedURL.Host, parsedURL.Path)
if ipfsCid == "ipfs" {
ipfsCid = strings.Split(parsedURL.Path, "/")[1]
urlPath = parsedURL.Path[1:]
}
if _, err := cid.Parse(ipfsCid); err != nil {
return "", "", fmt.Errorf("invalid content id %s: %w", ipfsCid, err)
}
return "", fmt.Sprintf("/%s/%s", parsedURL.Scheme, urlPath), nil
}
// Path gateway
if strings.HasPrefix(parsedURL.Path, "/ipfs/") || strings.HasPrefix(parsedURL.Path, "/ipns/") {
contentID := strings.TrimPrefix(strings.TrimPrefix(parsedURL.Path, "/ipfs/"), "/ipns/")
if _, err := cid.Parse(contentID); err != nil {
return "", "", fmt.Errorf("invalid content id %s: %w", contentID, err)
}
return parsedURL.Host, parsedURL.Path, nil
}
// Subdomain gateway
if splits := strings.Split(parsedURL.Host, "."); len(splits) >= 4 && (splits[1] == "ipfs" || splits[1] == "ipns") {
contentID := splits[0]
if _, err := cid.Parse(contentID); err != nil {
return "", "", fmt.Errorf("invalid content id %s: %w", contentID, err)
}
return strings.Join(splits[2:], "."), fmt.Sprintf("/%s/%s%s", splits[1], contentID, parsedURL.Path), nil
}
// DNSLink gateway is not supported
return "", "", fmt.Errorf("invalid content url %s", contentURL)
}