Skip to content

Commit

Permalink
IPFS links
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Nov 4, 2021
1 parent 2e48556 commit c22a96e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
21 changes: 12 additions & 9 deletions cmd/metadata/helpers/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ import (
"time"

"github.com/ipfs/go-cid"
)

const (
prefixIpfs = "ipfs://"
"github.com/pkg/errors"
)

// IPFSHash - separate IPFS hash from link
func IPFSHash(link string) (string, error) {
hash := strings.TrimPrefix(link, prefixIpfs)
if _, err := cid.Decode(hash); err != nil {
return "", err
hash := FindAllIPFSLinks([]byte(link))
if len(hash) != 1 {
return "", errors.Errorf("invalid IPFS link: %s", link)
}
return hash, nil
_, err := cid.Decode(hash[0])
return hash[0], err
}

// IPFSLink - get gateway link
func IPFSLink(gateway, hash string) string {
return fmt.Sprintf("%s/ipfs/%s", gateway, hash)
}

var ipfsURL = regexp.MustCompile(`ipfs:\/\/(?P<hash>Qm[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{44})`)
// IPFSPath - get path without protocol
func IPFSPath(link string) string {
return strings.TrimPrefix(link, "ipfs://")
}

var ipfsURL = regexp.MustCompile(`ipfs:\/\/(?P<hash>(baf[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{56})|Qm[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{44})`)

// FindAllIPFSLinks -
func FindAllIPFSLinks(data []byte) []string {
Expand Down
12 changes: 4 additions & 8 deletions cmd/metadata/resolver/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,15 @@ func (s Ipfs) Resolve(network, address, link string) ([]byte, error) {
return nil, ErrEmptyIPFSGatewayList
}

hash, err := helpers.IPFSHash(link)
if err != nil {
return nil, err
}

path := helpers.IPFSPath(link)
for _, sh := range s.pinning {
_ = sh.Pin(hash)
_ = sh.Pin(path)
}

gateways := helpers.ShuffleGateways(s.gateways)
for i := range gateways {
url := helpers.IPFSLink(gateways[i], hash)
data, err := s.cache.Fetch(hash, time.Hour, func() (interface{}, error) {
url := helpers.IPFSLink(gateways[i], path)
data, err := s.cache.Fetch(path, time.Hour, func() (interface{}, error) {
return s.Http.Resolve(network, address, url)
})
if err == nil {
Expand Down

0 comments on commit c22a96e

Please sign in to comment.