Skip to content

Commit

Permalink
Web3.storage driver (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko authored Feb 2, 2023
1 parent 1da2279 commit 90317d6
Show file tree
Hide file tree
Showing 12 changed files with 809 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
id: go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version-file: go.mod

- name: Cache go modules
id: cache-go-mod
Expand Down
14 changes: 14 additions & 0 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ type OSDriver interface {
NewSession(path string) OSSession
Description() string
UriSchemes() []string
Publish(ctx context.Context) (string, error)
}

// ErrNoNextPage indicates that there is no next page in ListFiles
var ErrNoNextPage = fmt.Errorf("no next page")

// ErrNotSupported indicated that the functionality is not supported by the given driver
var ErrNotSupported = fmt.Errorf("not supported")

type FileInfo struct {
Name string
ETag string
Expand All @@ -68,6 +72,7 @@ var AvailableDrivers = []OSDriver{
&IpfsOS{},
&MemoryOS{},
&S3OS{},
&W3sOS{},
}

type PageInfo interface {
Expand Down Expand Up @@ -271,6 +276,15 @@ func ParseOSURL(input string, useFullAPI bool) (OSDriver, error) {
u.Scheme = ""
return NewFSDriver(u), nil
}
if u.Scheme == "w3s" {
// W3S URL format: 'w3s://proof@pubId/path'
// Proof is base64url-encoded
// pubId must be a unique value used until Publish() is called
w3sUcanProof := u.User.Username()
pubId := u.Hostname()
filePath := u.Path
return NewW3sDriver(w3sUcanProof, filePath, pubId), nil
}
return nil, fmt.Errorf("unrecognized OS scheme: %s", u.Scheme)
}

Expand Down
20 changes: 20 additions & 0 deletions drivers/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ func TestGSURL(t *testing.T) {
assert.Equal("bucket-name", gs.S3OS.bucket)
}

func TestW3sURL(t *testing.T) {
assert := assert.New(t)

// given
pubId := "abcdef12345"
proof := "EaJlcm9vdHOAZ3ZlcnNpb24BmgIBcRIguVaNefyQMACKNgi3XA46t5ijCH19S_ndLpkGhZ0kWiOnYXNYRO2hA0CVYBCNOU9IW-u-IUqhZ9gSHPzFMB7tzLYBE0tjOUrg11K3p3bC31kprHJ769ISMQSJDMRvWCGamwks2rsWJA4GYXZlMC45LjFjYXR0gaJjY2FuYSpkd2l0aHg4ZGlkOmtleTp6Nk1rdGdRNGZHOWNFTTdVY3dOTUhuRUJ0a1ZXYmQ2QUJLRFh3VTFKMlpvdVpodnBjYXVkWCLtAeoGmhaC2aAQPNKXr4AK7MOo8OR_9RkLNIZ6_SgZUq2_Y2V4cPZjaXNzWCLtAdNhO-TS5YOYwp4wQuxsFq9Hi2uBoldfmfxUxf3HWuhRY3ByZoDoAgFxEiAdz1OG9whG7Z5aT42jkEMcBiczAba5WgpZ5NO6okLTKKhhc1hE7aEDQJqxaum4RfYm8EF9W2G2SSoI6rI58lC6buIUoSZaThMs0JA3blC7PPrTgL06AqWOaaAnQKN4b9TuBezi3llLnQhhdmUwLjkuMWNhdHSBomNjYW5hKmR3aXRoeDhkaWQ6a2V5Ono2TWt0Z1E0Zkc5Y0VNN1Vjd05NSG5FQnRrVldiZDZBQktEWHdVMUoyWm91Wmh2cGNhdWRYIu0BYFbRZFVNOcB-ZhrKuhujUFU3l9oaQa68-YMRNtYtqDpjZXhw9mNmY3SBoWVzcGFjZaJkbmFtZWR0ZXN0bGlzUmVnaXN0ZXJlZPVjaXNzWCLtAeoGmhaC2aAQPNKXr4AK7MOo8OR_9RkLNIZ6_SgZUq2_Y3ByZoHYKlglAAFxEiC5Vo15_JAwAIo2CLdcDjq3mKMIfX1L-d0umQaFnSRaIw"
path := "/video/hls"

// when
os, err := ParseOSURL(fmt.Sprintf("w3s://%s@%s%s", proof, pubId, path), false)

// then
assert.Equal(nil, err)
w3s, isW3s := os.(*W3sOS)
assert.Equal(true, isW3s)
assert.Equal(proof, w3s.ucanProof)
assert.Equal(pubId, w3s.pubId)
assert.Equal(path, w3s.dirPath)
}

func TestDescribeDriversJson(t *testing.T) {
assert := assert.New(t)
handlersJson := DescribeDriversJson()
Expand Down
4 changes: 4 additions & 0 deletions drivers/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (ostore *FSOS) Description() string {
return "File system driver."
}

func (ostore *FSOS) Publish(ctx context.Context) (string, error) {
return "", ErrNotSupported
}

func (ostore *FSSession) OS() OSDriver {
return ostore.os
}
Expand Down
4 changes: 4 additions & 0 deletions drivers/gs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (os *GsOS) NewSession(path string) OSSession {
return gs
}

func (os *GsOS) Publish(ctx context.Context) (string, error) {
return "", ErrNotSupported
}

func newGSSession(info *S3OSInfo) OSSession {
sess := &s3Session{
host: info.Host,
Expand Down
4 changes: 4 additions & 0 deletions drivers/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (ostore *IpfsOS) Description() string {
return "Pinata cloud IPFS driver."
}

func (ostore *IpfsOS) Publish(ctx context.Context) (string, error) {
return "", ErrNotSupported
}

func (session *IpfsSession) OS() OSDriver {
return session.os
}
Expand Down
4 changes: 4 additions & 0 deletions drivers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (ostore *MemoryOS) NewSession(path string) OSSession {
return session
}

func (ostore *MemoryOS) Publish(ctx context.Context) (string, error) {
return "", ErrNotSupported
}

func (ostore *MemoryOS) GetSession(path string) *MemorySession {
ostore.lock.Lock()
defer ostore.lock.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions drivers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func (ostore *S3OS) UriSchemes() []string {
return []string{"s3", "s3+http", "s3+https"}
}

func (ostore *S3OS) Publish(ctx context.Context) (string, error) {
return "", ErrNotSupported
}

func (ostore *S3OS) Description() string {
return "AWS S3 or S3 compatible storage."
}
Expand Down
Loading

0 comments on commit 90317d6

Please sign in to comment.