forked from agrinman/chasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox.go
230 lines (180 loc) · 5.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/users"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
"github.com/fatih/color"
"github.com/toqueteos/webbrowser"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
type DropboxStore struct {
Dropbox users.Client
AccessToken string
UserID string
config dropbox.Config
}
func GetClientKeys() (key, secret string) {
return DropboxClientKey, DropboxClientSecret
}
func connect(key,secret,code string) *oauth2.Token{
ctx := context.Background()
conf := &oauth2.Config{
ClientID: key,
ClientSecret: secret,
Endpoint: dropbox.OAuthEndpoint(""),
}
tok, err := conf.Exchange(ctx, code)
return tok
}
func (d *DropboxStore) Setup() bool {
key, secret := GetClientKeys()
code, err := getDropboxTokenFromWeb()
tok := connect(key,secret,code)
config := dropbox.Config{
Token: tok.AccessToken,
LogLevel: dropbox.LogOff,
}
db := users.New(config)
if err != nil {
color.Red("Unable to get client token: %v", err)
return false
}
account, err := db.GetCurrentAccount()
if err != nil {
color.Red("Unable to get account information: %v", err)
return false
}
uid := account.AccountId
for _, d := range preferences.DropboxStores {
if d.UserID == uid {
color.Red("Account for %s already exists.", account.Name.DisplayName)
return false
}
}
d.Dropbox = db
d.AccessToken = tok.AccessToken
d.UserID = uid
d.config = config
return true
}
func (d DropboxStore) Upload(share Share) {
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
fmt.Print(color.MagentaString("Uploading Dropbox/%s...", share.SID))
input := ioutil.NopCloser(bytes.NewReader(share.Data))
_, err := d.Dropbox.FilesPut(input, int64(len(share.Data)), string(share.SID), true, "")
if err != nil {
color.Red("Error uploading file: ", err)
return
}
//print check mark
fmt.Print(color.MagentaString("\u2713\n"))
}
func (d DropboxStore) Delete(sid ShareID) {
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
fmt.Print(color.YellowString("Deleting Dropbox/%s...", sid))
_, err := d.Dropbox.Delete(string(sid))
if err != nil {
color.Red("Error deleting file: ", err)
return
}
//print check mark
fmt.Print(color.GreenString("\u2713\n"))
}
func (d DropboxStore) Description() string {
label := "Dropbox Store"
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
// get all chasm files from drive
entry, err := d.Dropbox.Metadata("", true, false, "", "", 0)
if err != nil {
color.Red("Unable to iterate names %v", err)
return label
}
account, err := d.Dropbox.GetAccountInfo()
if err != nil {
color.Red("Unable to get account information: %v", err)
return label
}
label = fmt.Sprintf("Dropbox Store: %v", account.DisplayName)
for _, i := range entry.Contents {
label += fmt.Sprintf("\n\t%s %s", color.YellowString("-"), filepath.Base(i.Path))
}
return label
}
func (d DropboxStore) ShortDescription() string {
label := "Dropbox Store"
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
account, err := d.Dropbox.GetAccountInfo()
if err != nil {
color.Red("Unable to get account information: %v", err)
return label
}
return fmt.Sprintf("Dropbox Store: %v", account.DisplayName)
}
func (d DropboxStore) Clean() {
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
entry, err := d.Dropbox.Metadata("", true, false, "", "", 0)
if err != nil {
color.Red("Unable to iterate names %v", err)
return
}
for _, i := range entry.Contents {
name := filepath.Base(i.Path)
color.Yellow("Removing Dropbox: %v", name)
d.Dropbox.Delete(name)
}
return
}
func (d DropboxStore) Restore() string {
key, secret := GetClientKeys()
d.Dropbox.SetAppInfo(key, secret)
d.Dropbox.SetAccessToken(d.AccessToken)
restoreDir, err := ioutil.TempDir("", "chasm_dropbox_restore")
if err != nil {
color.Red("Error cannot create temp dir: %v", err)
return ""
}
entry, err := d.Dropbox.Metadata("", true, false, "", "", 0)
if err != nil {
color.Red("Unable to iterate names %v", err)
return ""
}
color.Yellow("Downloading shares from Dropbox...")
for _, i := range entry.Contents {
if !i.IsDir {
name := filepath.Base(i.Path)
err := d.Dropbox.DownloadToFile(name, filepath.Join(restoreDir, name), "")
if err != nil {
color.Red("Unable to download file %s: %v", name, err)
return ""
}
fmt.Println("\t - got share ", name)
}
}
return restoreDir
}
func getDropboxTokenFromWeb() (string, error) {
key, _ := GetClientKeys()
authURL := fmt.Sprintf("https://www.dropbox.com/1/oauth2/authorize?client_id=%s&response_type=code", key)
webbrowser.Open(authURL)
color.Yellow("Enter Auth Code: ")
var code string
if _, err := fmt.Scan(&code); err != nil {
color.Red("Unable to read authorization code %v", err)
return "", err
}
return code, nil
}