forked from ispysoftware/iSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropbox.cs
235 lines (196 loc) · 7.44 KB
/
Dropbox.cs
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
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using iSpyApplication.Utilities;
using Newtonsoft.Json;
namespace iSpyApplication.Cloud
{
public static class Dropbox
{
private const string ApiKey = "6k40bpqlz573mqt";
private const string Secret= "mx5bth2wj95mkd2";
private static string _accessToken="";
//private static DropNetClient _service;
private static volatile bool _uploading;
private static List<UploadEntry> UploadList { get; set; } = new List<UploadEntry>();
private static readonly DateTime Expires = DateTime.UtcNow;
public static string AccessToken
{
get
{
if (_accessToken != "" && Expires < DateTime.UtcNow)
{
return _accessToken;
}
if (!string.IsNullOrEmpty(MainForm.Conf.Cloud.DropBox))
{
dynamic d = JsonConvert.DeserializeObject(MainForm.Conf.Cloud.DropBox);
//dropbox doesn't seem to use refresh_token
_accessToken = d.access_token.ToString();
return _accessToken;
}
return "";
}
}
public static bool Authorise(string code)
{
try
{
var request =
(HttpWebRequest)
WebRequest.Create("https://api.dropboxapi.com/oauth2/token");
var postData = "client_id=" + ApiKey + "&client_secret=" +
Secret + "&code=" + code + "&grant_type=authorization_code&redirect_uri=https://www.ispyconnect.com/responsecode.aspx";
var dp = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(dp, 0, postData.Length);
}
var response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
if (s == null)
throw new Exception("null response stream");
var responseString = new StreamReader(s).ReadToEnd();
dynamic d2 = JsonConvert.DeserializeObject(responseString);
_accessToken = d2.access_token.ToString();
MainForm.Conf.Cloud.DropBox = responseString;
return true;
}
catch (WebException ex)
{
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
Logger.LogError("Dropbox: "+resp);
}
catch (Exception ex)
{
Logger.LogException(ex);
MainForm.Conf.Cloud.DropBox = "";
}
return false;
}
private class UploadEntry
{
public string SourceFilename;
public string DestinationPath;
}
public static bool Authorised
{
get { return AccessToken != ""; }
}
public static string Upload(string filename, string path, out bool success)
{
success = false;
//if (!Statics.Subscribed)
// return "NotSubscribed";
if (!Authorised)
{
return "CloudAddSettings";
}
if (UploadList.SingleOrDefault(p => p.SourceFilename == filename) != null)
return "FileInQueue";
if (UploadList.Count >= CloudGateway.MaxUploadQueue)
return "UploadQueueFull";
UploadList.Add(new UploadEntry { DestinationPath = path, SourceFilename = filename });
if (!_uploading)
{
_uploading = true;
ThreadPool.QueueUserWorkItem(Upload, null);
}
success = true;
return "AddedToQueue";
}
private static void Upload(object state)
{
try
{
if (UploadList.Count == 0)
{
_uploading = false;
return;
}
UploadEntry entry;
try
{
var l = UploadList.ToList();
entry = l[0]; //could have been cleared by Authorise
l.RemoveAt(0);
UploadList = l.ToList();
}
catch
{
_uploading = false;
return;
}
if (AccessToken == "")
{
_uploading = false;
return;
}
FileInfo fi;
byte[] byteArray;
try
{
fi = new FileInfo(entry.SourceFilename);
byteArray = Helper.ReadBytesWithRetry(fi);
}
catch (Exception ex)
{
//file doesn't exist
Logger.LogException(ex);
return;
}
try
{
var path = entry.DestinationPath.Replace(@"\", "/");
var url = $"https://content.dropboxapi.com/2/files/upload";
var request =
(HttpWebRequest)
WebRequest.Create(url);
request.Headers.Add("Authorization", "Bearer " + AccessToken);
request.Headers.Add("Dropbox-API-Arg",
"{\"path\": \"/" + path + fi.Name +
"\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}");
request.Method = "POST";
request.ContentType = "application/octet-stream";
request.ContentLength = byteArray.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(byteArray, 0, byteArray.Length);
}
var response = (HttpWebResponse) request.GetResponse();
var s = response.GetResponseStream();
if (s == null)
throw new Exception("null response stream");
var responseString = new StreamReader(s).ReadToEnd();
dynamic d = JsonConvert.DeserializeObject(responseString);
Logger.LogMessage("File uploaded to dropbox: " + d.path_lower);
}
catch (WebException wex)
{
var s = wex.Response.GetResponseStream();
if (s != null)
{
var resp = new StreamReader(s).ReadToEnd();
Logger.LogError("Dropbox: " + resp);
}
}
catch (Exception ex)
{
Logger.LogException(ex, "Dropbox");
}
Upload(null);
}
catch (Exception ex)
{
Logger.LogException(ex, "Dropbox");
}
}
}
}