Skip to content

Commit

Permalink
Merge pull request #55 from JackalLabs/marston/ipfs-api
Browse files Browse the repository at this point in the history
ipfs file list
  • Loading branch information
TheMarstonConnell authored Jul 11, 2024
2 parents 8b1b032 + 0e05a4d commit f634625
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
18 changes: 18 additions & 0 deletions api/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ func IPFSListPeers(f *file_system.FileSystem) func(http.ResponseWriter, *http.Re
}
}
}

func IPFSListCids(f *file_system.FileSystem) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
cids, err := f.ListCids()
if err != nil {
handleErr(err, w, http.StatusInternalServerError)
return
}

f := types.CidResponse{
Cids: cids,
}
err = json.NewEncoder(w).Encode(f)
if err != nil {
log.Error().Err(err)
}
}
}
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.
r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f))

r.HandleFunc("/ipfs/peers", IPFSListPeers(f))
r.HandleFunc("/ipfs/cids", IPFSListCids(f))

r.HandleFunc("/dump", DumpDBHandler(f))

Expand Down
4 changes: 4 additions & 0 deletions api/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ type LegacyAPIListResponse struct {
type PeersResponse struct {
Peers peer.IDSlice `json:"peers"`
}

type CidResponse struct {
Cids []string `json:"cids"`
}
53 changes: 52 additions & 1 deletion file_system/ipfs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
package file_system

import "github.com/libp2p/go-libp2p/core/peer"
import (
"fmt"

"github.com/dgraph-io/badger/v4"
"github.com/libp2p/go-libp2p/core/peer"
)

func (f *FileSystem) ListPeers() peer.IDSlice {
return f.ipfsHost.Peerstore().PeersWithAddrs()
}

func (f *FileSystem) GetCIDFromMerkle(merkle []byte) (cid string, err error) {
err = f.db.View(func(txn *badger.Txn) error {
c, err := txn.Get([]byte(fmt.Sprintf("cid/%x", merkle)))
if err != nil {
return err
}
err = c.Value(func(val []byte) error {
cid = string(val)
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return "", err
}

return
}

func (f *FileSystem) ListCids() ([]string, error) {
cids := make([]string, 0)

err := f.db.View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := []byte("cid/")
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
err := item.Value(func(v []byte) error {
cids = append(cids, string(v))

return nil
})
if err != nil {
return err
}
}
return nil
})

return cids, err
}

0 comments on commit f634625

Please sign in to comment.