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 284
/
dropbox.go
66 lines (57 loc) · 1.63 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
package dropbox
import (
"bytes"
"crypto/sha256"
"encoding/hex"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
"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)
hex := hex.EncodeToString(hash[:])
// Upload ciphertext
uploadArg := files.NewCommitInfo("/" + hex)
r := bytes.NewReader(ciphertext)
_, err := api.Upload(uploadArg, r)
if err != nil {
return nil, err
}
// Set public sharing
sharingArg := sharing.NewCreateSharedLinkArg("/" + hex)
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
}