This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 285
/
selfhostedstorage.go
70 lines (62 loc) · 1.59 KB
/
selfhostedstorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package selfhosted
import (
"crypto/sha256"
"encoding/hex"
"log"
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
"gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
"os"
"path"
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/ipfs/go-ipfs/core"
)
type SelfHostedStorage struct {
repoPath string
ipfsNode *core.IpfsNode
pushNodes []peer.ID
store func(peerId string, ids []cid.Cid) error
}
func NewSelfHostedStorage(repoPath string, n *core.IpfsNode, pushNodes []peer.ID, store func(peerId string, ids []cid.Cid) error) *SelfHostedStorage {
return &SelfHostedStorage{
repoPath: repoPath,
ipfsNode: n,
pushNodes: pushNodes,
store: store,
}
}
func (s *SelfHostedStorage) Store(peerID peer.ID, ciphertext []byte) (ma.Multiaddr, error) {
b := sha256.Sum256(ciphertext)
hash := hex.EncodeToString(b[:])
filePath := path.Join(s.repoPath, "outbox", hash)
f, err := os.Create(filePath)
if err != nil {
return nil, err
}
defer f.Close()
_, ferr := f.Write(ciphertext)
if ferr != nil {
return nil, ferr
}
addr, err := ipfs.AddFile(s.ipfsNode, filePath)
if err != nil {
return nil, err
}
id, err := cid.Decode(addr)
if err != nil {
return nil, err
}
for _, peer := range s.pushNodes {
go func(peerID string, cid []cid.Cid) {
err := s.store(peerID, cid)
if err != nil {
log.Println(err)
}
}(peer.Pretty(), []cid.Cid{id})
}
maAddr, err := ma.NewMultiaddr("/ipfs/" + addr + "/")
if err != nil {
return nil, err
}
return maAddr, nil
}