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
|
#include "stdafx.h"
char* CDropbox::UploadFile(const char *data, size_t size, char *path)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
ptrA encodedPath(mir_utf8encode(path));
UploadFileRequest request(token, encodedPath, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
JSONNode root = JSONNode::parse(response->pData);
if (!root.empty())
{
JSONNode node = root.at("path_lower");
mir_strcpy(path, node.as_string().c_str());
}
return path;
}
void CDropbox::StartUploadSession(const char *data, size_t size, char *sessionId)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
StartUploadSessionRequest request(token, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
JSONNode root = JSONNode::parse(response->pData);
if (root.empty())
return;
JSONNode node = root.at("session_id");
mir_strcpy(sessionId, node.as_string().c_str());
}
void CDropbox::AppendToUploadSession(const char *data, size_t size, const char *sessionId, size_t offset)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
AppendToUploadSessionRequest request(token, sessionId, offset, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
}
char* CDropbox::FinishUploadSession(const char *data, size_t size, const char *sessionId, size_t offset, char *path)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
FinishUploadSessionRequest request(token, sessionId, offset, path, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
JSONNode root = JSONNode::parse(response->pData);
if (!root.empty())
{
JSONNode node = root.at("path_lower");
mir_strcpy(path, node.as_string().c_str());
}
return path;
}
void CDropbox::CreateFolder(const char *path)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
CreateFolderRequest request(token, path);
NLHR_PTR response(request.Send(hNetlibConnection));
// forder exists on server
if (response->resultCode == HTTP_STATUS_FORBIDDEN)
return;
HandleJsonResponseError(response);
}
void CDropbox::CreateDownloadUrl(const char *path, char *url)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
//bool useShortUrl = db_get_b(NULL, MODULE, "UseSortLinks", 1) > 0;
ShareRequest request(token, path);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
JSONNode root = JSONNode::parse(response->pData);
if (root.empty())
return;
JSONNode node = root.at("url");
mir_strcpy(url, node.as_string().c_str());
}
UINT CDropbox::SendFilesAsync(void *owner, void *arg)
{
CDropbox *instance = (CDropbox*)owner;
FileTransferParam *ftp = (FileTransferParam*)arg;
try {
ftp->FirstFile();
do
{
const TCHAR *fileName = &ftp->GetCurrentFilePath()[ftp->relativePathStart];
uint64_t fileSize = ftp->GetCurrentFileSize();
int chunkSize = ftp->GetCurrentFileChunkSize();
char *data = (char*)mir_calloc(chunkSize);
size_t size = ftp->ReadCurrentFile(data, chunkSize);
size_t offset = 0;
char sessionId[32];
instance->StartUploadSession(data, size, sessionId);
offset += size;
ftp->Progress(size);
for (size_t chunk = 0, size = 0; chunk < (fileSize / chunkSize) - 1; chunk++, size = 0)
{
ftp->CheckCurrentFile();
size = ftp->ReadCurrentFile(data, chunkSize);
instance->AppendToUploadSession(data, size, sessionId, offset);
offset += size;
ftp->Progress(size);
}
if (offset < fileSize)
size = ftp->ReadCurrentFile(data, fileSize - offset);
char path[MAX_PATH];
PreparePath(fileName, path);
instance->FinishUploadSession(data, size, sessionId, offset, path);
ftp->Progress(size);
if (!_tcschr(fileName, L'\\')) {
char url[MAX_PATH];
instance->CreateDownloadUrl(path, url);
ftp->AddUrl(url);
}
} while (ftp->NextFile());
}
catch (DropboxException &ex) {
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, ex.what());
ftp->SetStatus(ACKRESULT_FAILED);
return ACKRESULT_FAILED;
}
ftp->SetStatus(ACKRESULT_SUCCESS);
return ACKRESULT_SUCCESS;
}
UINT CDropbox::SendFilesAndReportAsync(void *owner, void *arg)
{
CDropbox *instance = (CDropbox*)owner;
FileTransferParam *ftp = (FileTransferParam*)arg;
int res = SendFilesAsync(owner, arg);
if (res == ACKRESULT_SUCCESS)
{
CMStringA urls;
for (int i = 0; i < ftp->urls.getCount(); i++)
urls.AppendFormat("%s\r\n", ftp->urls[i]);
instance->Report(ftp->hContact, urls.GetBuffer());
}
instance->transfers.remove(ftp);
delete ftp;
return res;
}
UINT CDropbox::SendFilesAndEventAsync(void *owner, void *arg)
{
CDropbox *instance = (CDropbox*)owner;
FileTransferParam *ftp = (FileTransferParam*)arg;
int res = SendFilesAsync(owner, arg);
TRANSFERINFO ti = { 0 };
ti.hProcess = ftp->hProcess;
ti.status = res;
ti.data = ftp->urls.getArray();
NotifyEventHooks(instance->hFileSentEventHook, ftp->hContact, (LPARAM)&ti);
instance->transfers.remove(ftp);
delete ftp;
return res;
}
|