From 60b7ccc3bb3a74b915be0c2c1b993ef61d2291d6 Mon Sep 17 00:00:00 2001 From: marston Date: Thu, 11 Jul 2024 16:07:17 -0400 Subject: [PATCH] more ipfs api --- api/ipfs.go | 18 ++++++++++++++ api/server.go | 1 + api/types/responses.go | 4 ++++ file_system/ipfs.go | 53 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/api/ipfs.go b/api/ipfs.go index 18954ad..be23801 100644 --- a/api/ipfs.go +++ b/api/ipfs.go @@ -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) + } + } +} diff --git a/api/server.go b/api/server.go index 9ff6a0d..d22acb5 100644 --- a/api/server.go +++ b/api/server.go @@ -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)) diff --git a/api/types/responses.go b/api/types/responses.go index 40a6e48..5cd34b0 100644 --- a/api/types/responses.go +++ b/api/types/responses.go @@ -41,3 +41,7 @@ type LegacyAPIListResponse struct { type PeersResponse struct { Peers peer.IDSlice `json:"peers"` } + +type CidResponse struct { + Cids []string `json:"cids"` +} diff --git a/file_system/ipfs.go b/file_system/ipfs.go index f282226..940a733 100644 --- a/file_system/ipfs.go +++ b/file_system/ipfs.go @@ -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 +}