Skip to content

Commit

Permalink
ipfs: sanitize the ipfs path
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Feb 21, 2024
1 parent 41c5738 commit 3d1892f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 9 additions & 0 deletions data/ipfs/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ func CIDequals(cid1, cid2 string) bool {
}
return c1.Hash().String() == c2.Hash().String()
}

// sanitizePath removes the ipfs:// prefix and adds the /ipfs/ prefix if missing.
func sanitizePath(path string) string {
c := strings.Replace(path, "ipfs://", "/ipfs/", 1)
if len(c) > 0 && c[0] != '/' {
c = "/ipfs/" + c
}
return c
}
11 changes: 4 additions & 7 deletions data/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

lru "github.com/hashicorp/golang-lru/v2"
Expand Down Expand Up @@ -188,7 +187,7 @@ func (i *Handler) Publish(ctx context.Context, msg []byte) (cid string, err erro

// Pin adds a file to ipfs and returns the resulting CID v1.
func (i *Handler) Pin(ctx context.Context, path string) error {
path = strings.Replace(path, "ipfs://", "/ipfs/", 1)
path = sanitizePath(path)
p, err := ipfspath.NewPath(path)
if err != nil {
return data.ErrInvalidPath
Expand All @@ -215,7 +214,7 @@ func (i *Handler) addAndPin(ctx context.Context, path string) (ipfspath.Immutabl

// Unpin removes a file pin from ipfs.
func (i *Handler) Unpin(ctx context.Context, path string) error {
path = strings.Replace(path, "ipfs://", "/ipfs/", 1)
path = sanitizePath(path)
cpath, err := ipfspath.NewPath(path)
if err != nil {
return data.ErrInvalidPath
Expand Down Expand Up @@ -269,8 +268,7 @@ func (i *Handler) ListPins(ctx context.Context) (map[string]string, error) {
// RetrieveDir gets an IPFS directory and returns a map of all files and their content.
// It only supports 1 level of directory depth, so subdirectories are ignored.
func (i *Handler) RetrieveDir(ctx context.Context, path string, maxSize int64) (map[string][]byte, error) {
path = strings.Replace(path, "ipfs://", "/ipfs/", 1)

path = sanitizePath(path)
// first resolve the path
p, err := ipfspath.NewPath(path)
if err != nil {
Expand Down Expand Up @@ -312,8 +310,7 @@ func (i *Handler) RetrieveDir(ctx context.Context, path string, maxSize int64) (
// Retrieve gets an IPFS file (either from the p2p network or from the local cache).
// If maxSize is 0, it is set to the hardcoded maximum of MaxFileSizeBytes.
func (i *Handler) Retrieve(ctx context.Context, path string, maxSize int64) ([]byte, error) {
path = strings.Replace(path, "ipfs://", "/ipfs/", 1)

path = sanitizePath(path)
// check if we have the file in the local cache
ccontent, _ := i.retrieveCache.Get(path)
if ccontent != nil {
Expand Down

0 comments on commit 3d1892f

Please sign in to comment.