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
/
dropbox.go
67 lines (57 loc) · 1.64 KB
/
dropbox.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
package dropbox
import (
"bytes"
"crypto/sha256"
"encoding/hex"
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
"github.com/dropbox/dropbox-sdk-go-unofficial"
"github.com/dropbox/dropbox-sdk-go-unofficial/files"
"github.com/dropbox/dropbox-sdk-go-unofficial/sharing"
)
type DropBoxStorage struct {
apiToken string
}
func NewDropBoxStorage(apiToken string) (*DropBoxStorage, error) {
api := dropbox.Client(apiToken, dropbox.Options{Verbose: true})
if _, err := api.GetCurrentAccount(); err != nil {
return nil, err
}
return &DropBoxStorage{
apiToken: apiToken,
}, nil
}
func (s *DropBoxStorage) Store(peerID peer.ID, ciphertext []byte) (ma.Multiaddr, error) {
api := dropbox.Client(s.apiToken, dropbox.Options{Verbose: true})
hash := sha256.Sum256(ciphertext)
hexStr := hex.EncodeToString(hash[:])
// Upload ciphertext
uploadArg := files.NewCommitInfo("/" + hexStr)
r := bytes.NewReader(ciphertext)
_, err := api.Upload(uploadArg, r)
if err != nil {
return nil, err
}
// Set public sharing
sharingArg := sharing.NewCreateSharedLinkArg("/" + hexStr)
res, err := api.CreateSharedLink(sharingArg)
if err != nil {
return nil, err
}
// Create encoded multiaddr
url := res.Url[:len(res.Url)-1] + "1"
b, err := mh.Encode([]byte(url), mh.SHA1)
if err != nil {
return nil, err
}
m, err := mh.Cast(b)
if err != nil {
return nil, err
}
addr, err := ma.NewMultiaddr("/ipfs/" + m.B58String() + "/https/")
if err != nil {
return nil, err
}
return addr, nil
}