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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#include "..\stdafx.h"
#include "microsoft_api.h"
COneDriveService::COneDriveService(const char *protoName, const wchar_t *userName)
: CCloudService(protoName, userName)
{
m_hProtoIcon = GetIconHandle(IDI_ONEDRIVE);
}
COneDriveService* COneDriveService::Init(const char *moduleName, const wchar_t *userName)
{
COneDriveService *proto = new COneDriveService(moduleName, userName);
Services.insert(proto);
return proto;
}
int COneDriveService::UnInit(COneDriveService *proto)
{
Services.remove(proto);
delete proto;
return 0;
}
const char* COneDriveService::GetModuleName() const
{
return "/OneDrive";
}
int COneDriveService::GetIconId() const
{
return IDI_ONEDRIVE;
}
bool COneDriveService::IsLoggedIn()
{
ptrA token(getStringA("TokenSecret"));
if (!token || token[0] == 0)
return false;
time_t now = time(nullptr);
time_t expiresIn = getDword("ExpiresIn");
return now < expiresIn;
}
void COneDriveService::Login(HWND owner)
{
ptrA refreshToken(getStringA("RefreshToken"));
if (refreshToken && refreshToken[0]) {
OneDriveAPI::RefreshTokenRequest request(refreshToken);
NLHR_PTR response(request.Send(m_hConnection));
JSONNode root = GetJsonResponse(response);
JSONNode node = root.at("access_token");
db_set_s(NULL, GetAccountName(), "TokenSecret", node.as_string().c_str());
node = root.at("expires_in");
time_t expiresIn = time(nullptr) + node.as_int();
setDword("ExpiresIn", expiresIn);
return;
}
COAuthDlg dlg(this, MICROSOFT_AUTH, RequestAccessTokenThread);
dlg.SetParent(owner);
dlg.DoModal();
}
void COneDriveService::Logout()
{
delSetting("ExpiresIn");
delSetting("TokenSecret");
delSetting("RefreshToken");
}
unsigned COneDriveService::RequestAccessTokenThread(void *owner, void *param)
{
HWND hwndDlg = (HWND)param;
COneDriveService *service = (COneDriveService*)owner;
if (service->IsLoggedIn())
service->Logout();
char requestToken[128];
GetDlgItemTextA(hwndDlg, IDC_OAUTH_CODE, requestToken, _countof(requestToken));
OneDriveAPI::GetAccessTokenRequest request(requestToken);
NLHR_PTR response(request.Send(service->m_hConnection));
if (response == nullptr || response->resultCode != HTTP_CODE_OK) {
const char *error = response->dataLength
? response->pData
: service->HttpStatusToError(response->resultCode);
Netlib_Logf(service->m_hConnection, "%s: %s", service->GetAccountName(), error);
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
EndDialog(hwndDlg, 0);
return 0;
}
JSONNode root = JSONNode::parse(response->pData);
if (root.empty()) {
Netlib_Logf(service->m_hConnection, "%s: %s", service->GetAccountName(), service->HttpStatusToError(response->resultCode));
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
EndDialog(hwndDlg, 0);
return 0;
}
JSONNode node = root.at("error_description");
if (!node.isnull()) {
CMStringW error_description = node.as_mstring();
Netlib_Logf(service->m_hConnection, "%s: %s", service->GetAccountName(), service->HttpStatusToError(response->resultCode));
ShowNotification(error_description, MB_ICONERROR);
EndDialog(hwndDlg, 0);
return 0;
}
node = root.at("access_token");
service->setString("TokenSecret", node.as_string().c_str());
node = root.at("expires_in");
time_t expiresIn = time(nullptr) + node.as_int();
service->setDword("ExpiresIn", expiresIn);
node = root.at("refresh_token");
service->setString("RefreshToken", node.as_string().c_str());
SetDlgItemTextA(hwndDlg, IDC_OAUTH_CODE, "");
EndDialog(hwndDlg, 1);
return 0;
}
void COneDriveService::HandleJsonError(JSONNode &node)
{
JSONNode error = node.at("error");
if (!error.isnull()) {
json_string tag = error.at("message").as_string();
throw Exception(tag.c_str());
}
}
auto COneDriveService::UploadFile(const std::string &parentId, const std::string &fileName, const char *data, size_t size)
{
ptrA token(getStringA("TokenSecret"));
BYTE strategy = db_get_b(NULL, MODULE, "ConflictStrategy", OnConflict::REPLACE);
OneDriveAPI::UploadFileRequest *request = !parentId.empty()
? new OneDriveAPI::UploadFileRequest(token, parentId.c_str(), fileName.c_str(), data, size, (OnConflict)strategy)
: new OneDriveAPI::UploadFileRequest(token, fileName.c_str(), data, size, (OnConflict)strategy);
NLHR_PTR response(request->Send(m_hConnection));
delete request;
JSONNode root = GetJsonResponse(response);
return root["id"].as_string();
}
auto COneDriveService::CreateUploadSession(const std::string &parentId, const std::string &fileName)
{
ptrA token(getStringA("TokenSecret"));
BYTE strategy = db_get_b(NULL, MODULE, "ConflictStrategy", OnConflict::REPLACE);
OneDriveAPI::CreateUploadSessionRequest *request = !parentId.empty()
? new OneDriveAPI::CreateUploadSessionRequest(token, parentId.c_str(), fileName.c_str(), (OnConflict)strategy)
: new OneDriveAPI::CreateUploadSessionRequest(token, fileName.c_str(), (OnConflict)strategy);
NLHR_PTR response(request->Send(m_hConnection));
delete request;
JSONNode root = GetJsonResponse(response);
return root["uploadUrl"].as_string();
}
auto COneDriveService::UploadFileChunk(const std::string &uploadUri, const char *chunk, size_t chunkSize, uint64_t offset, uint64_t fileSize)
{
OneDriveAPI::UploadFileChunkRequest request(uploadUri.c_str(), chunk, chunkSize, offset, fileSize);
NLHR_PTR response(request.Send(m_hConnection));
HandleHttpError(response);
if (response->resultCode == HTTP_CODE_ACCEPTED)
return std::string();
if (HTTP_CODE_SUCCESS(response->resultCode)) {
JSONNode root = GetJsonResponse(response);
return root["id"].as_string();
}
HttpResponseToError(response);
return std::string();
}
auto COneDriveService::CreateFolder(const std::string &path)
{
ptrA token(getStringA("TokenSecret"));
OneDriveAPI::CreateFolderRequest request(token, path.c_str());
NLHR_PTR response(request.Send(m_hConnection));
JSONNode root = GetJsonResponse(response);
return root["id"].as_string();
}
auto COneDriveService::CreateSharedLink(const std::string &itemId)
{
ptrA token(getStringA("TokenSecret"));
OneDriveAPI::CreateSharedLinkRequest request(token, itemId.c_str());
NLHR_PTR response(request.Send(m_hConnection));
JSONNode root = GetJsonResponse(response);
return root["link"]["webUrl"].as_string();
}
void COneDriveService::Upload(FileTransferParam *ftp)
{
std::string folderId;
auto serverDictionary = ftp->GetServerDirectory();
std::string serverFolder = serverDictionary ? T2Utf(serverDictionary) : "";
if (!serverFolder.empty()) {
folderId = CreateFolder(serverFolder);
auto link = CreateSharedLink(folderId);
ftp->AddSharedLink(link.c_str());
}
ftp->FirstFile();
do {
std::string fileName = T2Utf(ftp->GetCurrentRelativeFilePath());
uint64_t fileSize = ftp->GetCurrentFileSize();
size_t chunkSize = ftp->GetCurrentFileChunkSize();
mir_ptr<char> chunk((char*)mir_calloc(chunkSize));
std::string fileId;
if (chunkSize == fileSize) {
ftp->CheckCurrentFile();
size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
fileId = UploadFile(folderId, fileName, chunk, size);
ftp->Progress(size);
}
else {
auto uploadUri = CreateUploadSession(folderId, fileName);
uint64_t offset = 0;
double chunkCount = ceil(double(fileSize) / chunkSize);
for (size_t i = 0; i < chunkCount; i++) {
ftp->CheckCurrentFile();
size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
fileId = UploadFileChunk(uploadUri, chunk, size, offset, fileSize);
offset += size;
ftp->Progress(size);
}
}
if (!ftp->IsCurrentFileInSubDirectory()) {
auto link = CreateSharedLink(fileId);
ftp->AddSharedLink(link.c_str());
}
} while (ftp->NextFile());
}
/////////////////////////////////////////////////////////////////////////////////////////
struct CMPluginOnedrive : public CMPluginBase
{
CMPluginOnedrive() :
CMPluginBase(MODULE "/OneDrive")
{
m_hInst = g_plugin.getInst();
RegisterProtocol(PROTOTYPE_PROTOWITHACCS, (pfnInitProto)COneDriveService::Init, (pfnUninitProto)COneDriveService::UnInit);
}
}
g_pluginOnedrive;
|