summaryrefslogtreecommitdiff
path: root/plugins/Dropbox/src/dropbox_transfers.cpp
blob: 4d5dd7c2e6c7b3433e005475a0e024d895ec980c (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "common.h"

HttpRequest *CDropbox::CreateFileSendChunkedRequest(const char *data, int length)
{
	HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_PUT, DROPBOX_APICONTENT_URL "/chunked_upload");
	request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
	if (length > 0)
	{
		request->AddHeader("Content-Type", "application/octet-stream");
		request->dataLength = length;
		request->pData = (char*)mir_alloc(sizeof(char) * (length + 1));
		memcpy(request->pData, data, length);
		request->pData[length] = 0;
	}

	return request;
}

void CDropbox::SendFileChunkedFirst(const char *data, int length, char *uploadId, int &offset)
{
	HttpRequest *request = CreateFileSendChunkedRequest(data, length);
	NETLIBHTTPREQUEST *response = request->Send();

	delete request;

	if (response)
	{
		if (response->resultCode == HttpStatus::OK)
		{
			JSONNODE *root = json_parse(response->pData);
			if (root != NULL)
			{
				JSONNODE *node = json_get(root, "upload_id");
				strcpy(uploadId, mir_u2a(json_as_string(node)));

				node = json_get(root, "offset");
				offset = json_as_int(node);

				delete node;
				delete root;
			}
		}

		mir_free(response);
	}
}

void CDropbox::SendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset)
{
	HttpRequest *request = CreateFileSendChunkedRequest(data, length);
	request->AddParameter("upload_id", uploadId);
	request->AddParameter("offset", offset);

	NETLIBHTTPREQUEST *response = request->Send();

	delete request;

	if (response)
	{
		if (response->resultCode == HttpStatus::OK)
		{
			JSONNODE *root = json_parse(response->pData);
			if (root != NULL)
			{
				JSONNODE *node = json_get(root, "offset");
				offset = json_as_int(node);

				delete node;
				delete root;
			}
		}

		mir_free(response);
	}
}

void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, MCONTACT hContact)
{
	CMStringA url;
	url.AppendFormat(
		"%s/commit_chunked_upload/%s/%s",
		DROPBOX_APICONTENT_URL,
		DROPBOX_API_ROOT,
		fileName);
	url.Replace('\\', '/');

	HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, url);
	request->AddParameter("upload_id", uploadId);
	request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));

	NETLIBHTTPREQUEST *response = request->Send();

	delete request;

	if (response)
	{
		if (response->resultCode == HttpStatus::OK && !strchr(fileName, '\\'))
		{
			url.Replace(DROPBOX_APICONTENT_URL, DROPBOX_API_URL);
			url.Replace("commit_chunked_upload", "shares");

			request = new HttpRequest(hNetlibUser, REQUEST_POST, url);
			request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));

			mir_free(response);

			response = request->Send();

			if (response)
			{
				if (response->resultCode == HttpStatus::OK)
				{
					JSONNODE *root = json_parse(response->pData);
					if (root != NULL)
					{
						JSONNODE *node = json_get(root, "url");
						char message[1024];
						mir_snprintf(
							message,
							SIZEOF(message),
							Translate("Link to download file \"%s\": %s"),
							fileName,
							mir_utf8encodeW(json_as_string(node)));

						DBEVENTINFO dbei = { sizeof(dbei) };
						dbei.szModule = MODULE;
						dbei.timestamp = time(NULL);
						dbei.eventType = EVENTTYPE_MESSAGE;
						dbei.cbBlob = strlen(message);
						dbei.pBlob = (PBYTE)mir_strdup(message);
						dbei.flags = DBEF_UTF;
						::db_event_add(hContact, &dbei);

						delete node;
						delete root;
					}
				}

				mir_free(response);
			}
		}
		else
			mir_free(response);
	}
}

void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact)
{
	CMStringA folder = folderName;
	folder.Replace('\\', '/');

	HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/fileops/create_folder");
	request->AddParameter("root", DROPBOX_API_ROOT);
	request->AddParameter("path", folderName);
	request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));

	NETLIBHTTPREQUEST *response = request->Send();

	delete request;

	if (response)
	{
		if (response->resultCode == HttpStatus::OK && !strchr(folderName, '\\'))
		{
			char url[MAX_PATH];
			mir_snprintf(
				url,
				SIZEOF(url),
				"%s/shares/%s/%s",
				DROPBOX_API_URL,
				DROPBOX_API_ROOT,
				folder.GetBuffer());

			request = new HttpRequest(hNetlibUser, REQUEST_POST, url);
			request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));

			mir_free(response);

			response = request->Send();

			if (response)
			{
				if (response->resultCode == HttpStatus::OK)
				{
					JSONNODE *root = json_parse(response->pData);
					if (root != NULL)
					{
						JSONNODE *node = json_get(root, "url");
						char message[1024];
						mir_snprintf(
							message,
							SIZEOF(message),
							Translate("Link to download folder \"%s\": %s"),
							folderName,
							mir_utf8encodeW(json_as_string(node)));

						DBEVENTINFO dbei = { sizeof(dbei) };
						dbei.szModule = MODULE;
						dbei.timestamp = time(NULL);
						dbei.eventType = EVENTTYPE_MESSAGE;
						dbei.cbBlob = strlen(message);
						dbei.pBlob = (PBYTE)mir_strdup(message);
						dbei.flags = DBEF_UTF;
						::db_event_add(hContact, &dbei);

						delete node;
						delete root;
					}
				}

				mir_free(response);
			}
		}
		else
			mir_free(response);
	}
}

void _cdecl CDropbox::SendFileAsync(void *arg)
{
	FileTransferParam *ftp = (FileTransferParam *)arg;

	ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, ftp->hProcess, 0);

	for (int i = 0; ftp->pszFolders[i]; i++)
		Singleton<CDropbox>::GetInstance()->CreateFolder(ftp->pszFolders[i], ftp->pfts.hContact);

	for (int i = 0; ftp->pfts.pszFiles[i]; i++)
	{
		FILE *file = fopen(ftp->pfts.pszFiles[i], "rb");
		if (file != NULL)
		{
			int offset = 0;
			char *uploadId = new char[32];

			const char *fileName = NULL;
			if (!ftp->relativePathStart)
				fileName = strrchr(ftp->pfts.pszFiles[i], '\\') + 1;
			else
				fileName = &ftp->pfts.pszFiles[i][ftp->relativePathStart];

			fseek(file, 0, SEEK_END);
			DWORD fileSize = ftell(file);
			fseek(file, 0, SEEK_SET);

			ftp->pfts.currentFileNumber = i;
			ftp->pfts.currentFileSize = fileSize;
			ftp->pfts.currentFileProgress = 0;
			ftp->pfts.szCurrentFile = strrchr(ftp->pfts.pszFiles[i], '\\') + 1;

			ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);

			while (!feof(file) && !ferror(file))
			{
				int chunkSize = DROPBOX_FILE_CHUNK_SIZE;
				if (fileSize < 1024*1024)
					chunkSize = DROPBOX_FILE_CHUNK_SIZE / 5;
				else if (fileSize > 20*1024*1024)
					chunkSize = DROPBOX_FILE_CHUNK_SIZE * 4;

				char *data = new char[chunkSize + 1];
				int count = fread(data, sizeof(char), chunkSize, file);

				if (!offset)
					Singleton<CDropbox>::GetInstance()->SendFileChunkedFirst(data, count, uploadId, offset);
				else
					Singleton<CDropbox>::GetInstance()->SendFileChunkedNext(data, count, uploadId, offset);

				ftp->pfts.currentFileProgress += count;
				ftp->pfts.totalProgress += count;

				ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);
			}

			fclose(file);

			Singleton<CDropbox>::GetInstance()->SendFileChunkedLast(fileName, uploadId, ftp->pfts.hContact);
			ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize;

			if (i < ftp->pfts.totalFiles - 1)
				ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftp->hProcess, 0);
		}
	}

	ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0);

	delete ftp;
}