-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgc_files.cpp
377 lines (310 loc) · 11 KB
/
tgc_files.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "tgclient.h"
#include <QtEndian>
#include <QFileInfo>
#include "tlschema.h"
#define FILE_PART_SIZE 524288
TgFileCtx::TgFileCtx(QString fileName)
: fileId()
, localFile()
, length()
, bytesLeft()
, fileParts()
, currentPart()
, isBig()
, queue()
, download()
{
fileId = randomLong();
localFile.setFileName(fileName);
length = localFile.size();
bytesLeft = length;
fileParts = (qint32) ((length - 1) / FILE_PART_SIZE) + 1; //512 KiB
currentPart = 0;
isBig = length > 10485760; //10 MB as Telegram says (10 MiB)
}
TgFileCtx::TgFileCtx(TgObject input, QString fileName, TgLong fileSize)
: fileId()
, localFile()
, length()
, bytesLeft()
, fileParts()
, currentPart()
, isBig()
, queue()
, download()
{
fileId = randomLong();
localFile.setFileName(fileName);
length = fileSize;
bytesLeft = length;
fileParts = (qint32) ((length - 1) / FILE_PART_SIZE) + 1; //512 KiB
currentPart = 0;
isBig = length > 10485760; //10 MB as Telegram says (10 MiB)
download = input;
}
TgLongVariant TgClient::downloadFile(QString filePath, TgObject inputFile, TgLongVariant fileSize, qint32 dcId, TgLongVariant fileId)
{
//TODO: file references
//TODO: CDN DCs
if ((isUser(inputFile) || isChat(inputFile)) && GETID(inputFile["photo"].toMap()) != 0) {
TGOBJECT(TLType::InputPeerPhotoFileLocation, fileLocation);
fileLocation["peer"] = toInputPeer(inputFile);
fileLocation["photo_id"] = inputFile["photo"].toMap()["photo_id"];
return downloadFile(filePath, fileLocation, fileSize, inputFile["photo"].toMap()["dc_id"].toInt(), fileId);
}
switch (GETID(inputFile)) {
case TLType::MessageMediaPhoto:
return downloadFile(filePath, inputFile["photo"].toMap(), fileSize, dcId, fileId);
case TLType::MessageMediaDocument:
return downloadFile(filePath, inputFile["document"].toMap(), fileSize, dcId, fileId);
case TLType::Photo:
{
TGOBJECT(TLType::InputPhotoFileLocation, loc);
loc["id"] = inputFile["id"];
loc["access_hash"] = inputFile["access_hash"];
loc["file_reference"] = inputFile["file_reference"];
TgList sizes = inputFile["sizes"].toList();
TgObject thumbSize;
for (qint32 i = 0; i < sizes.size(); ++i) {
TgObject obj = sizes[i].toMap();
if (ID(thumbSize) != 0 && (
obj["type"].toString() == "a"
|| obj["type"].toString() == "b"
|| obj["type"].toString() == "c"
|| obj["type"].toString() == "d"
|| obj["type"].toString() == "w"
))
continue;
if (obj["w"].toInt() > thumbSize["w"].toInt() || obj["h"].toInt() > thumbSize["h"].toInt())
thumbSize = obj;
}
loc["thumb_size"] = thumbSize["type"];
return downloadFile(filePath, loc, thumbSize["size"].toLongLong(), inputFile["dc_id"].toInt(), fileId);
}
case TLType::Document:
{
TGOBJECT(TLType::InputDocumentFileLocation, loc);
loc["id"] = inputFile["id"];
loc["access_hash"] = inputFile["access_hash"];
loc["file_reference"] = inputFile["file_reference"];
loc["thumb_size"] = "";
return downloadFile(filePath, loc, inputFile["size"].toLongLong(), inputFile["dc_id"].toInt(), fileId);
}
}
if (inputFile["id"].toLongLong() == 0 && inputFile["photo_id"].toLongLong() == 0) {
return 0;
}
TgFileCtx *ctx = new TgFileCtx(inputFile, filePath, fileSize.toLongLong());
if (!ctx->localFile.open(QFile::WriteOnly)) {
delete ctx;
return 0;
}
if (fileId != 0) {
ctx->fileId = fileId.toLongLong();
}
TgClient* dcClient = getClientForDc(dcId);
if (dcClient == 0) {
delete ctx;
return 0;
}
dcClient->processedDownloadFiles.insert(ctx->fileId, ctx);
dcClient->downloadNextFilePart();
//TODO: load files one after other sequensively (only one file at time)
return ctx->fileId;
}
TgLongVariant TgClient::migrateFileTo(TgLongVariant messageId, TgInt dcId)
{
TgLong fileId = filePackets.take(messageId.toLongLong());
TgFileCtx *ctx = processedDownloadFiles.take(fileId);
if (ctx == NULL)
return 0;
TgClient* client = isMain() ? this : static_cast<TgClient*>(parent());
if (client == 0) {
client = this;
}
ctx->localFile.close();
TgLong mid = client->downloadFile(ctx->localFile.fileName(), ctx->download, ctx->length, dcId, ctx->fileId).toLongLong();
delete ctx;
currentDownloading = 0;
downloadNextFilePart();
return mid;
}
void TgClient::fileProbablyDownloaded(TgLongVariant messageId)
{
TgLong fileId = filePackets.take(messageId.toLongLong());
TgFileCtx *ctx = processedDownloadFiles.take(fileId);
if (ctx == NULL)
return;
TgClient* client = isMain() ? this : static_cast<TgClient*>(parent());
if (client == 0) {
client = this;
}
ctx->localFile.close();
emit client->fileDownloaded(ctx->fileId, ctx->localFile.fileName());
delete ctx;
currentDownloading = 0;
downloadNextFilePart();
}
void TgClient::cancelDownload(TgLongVariant fileId)
{
TgFileCtx *ctx = processedDownloadFiles.take(fileId.toLongLong());
if (ctx == NULL)
return;
TgClient* client = isMain() ? this : static_cast<TgClient*>(parent());
if (client == 0) {
client = this;
}
ctx->localFile.close();
ctx->localFile.remove();
emit client->fileDownloadCanceled(fileId, ctx->localFile.fileName());
delete ctx;
currentDownloading = 0;
downloadNextFilePart();
}
TgLongVariant TgClient::downloadNextFilePart()
{
//TODO: seek offset and get bytes as part number (offset = partNumber * partSize, length = qMin(FILE_PART_SIZE, length - offset))
//TODO: get 2-3 parts in queue
//TODO: Verify hash chunks
if (!isAuthorized()) {
return 0;
}
if (processedDownloadFiles.isEmpty()) {
currentDownloading = 0;
return 0;
}
if (currentDownloading == 0) {
currentDownloading = processedDownloadFiles.begin().key();
}
TgFileCtx *ctx = processedDownloadFiles.value(currentDownloading, NULL);
if (ctx == NULL)
return 0;
if (!ctx->queue.isEmpty()) {
return 0;
}
if (ctx->length != 0 && ctx->bytesLeft <= 0) {
processedDownloadFiles.remove(ctx->fileId);
TgClient* client = isMain() ? this : static_cast<TgClient*>(parent());
if (client == 0) {
client = this;
}
ctx->localFile.close();
emit client->fileDownloaded(ctx->fileId, ctx->localFile.fileName());
delete ctx;
return 0;
}
TGOBJECT(TLType::UploadGetFileMethod, getFile);
getFile["location"] = ctx->download;
getFile["offset"] = (ctx->currentPart++) * (qint64) FILE_PART_SIZE;
getFile["limit"] = FILE_PART_SIZE;
TgLong mid = sendObject<&writeTLMethodUploadGetFile>(getFile);
ctx->queue.append(mid);
filePackets.insert(mid, ctx->fileId);
return mid;
}
void TgClient::handleDownloadingFile(TgObject response, TgLongVariant messageId)
{
TgLong fileId = filePackets.take(messageId.toLongLong());
TgFileCtx *ctx = processedDownloadFiles.value(fileId, NULL);
if (ctx != NULL) {
QByteArray bytes = response["bytes"].toByteArray();
ctx->localFile.write(bytes);
ctx->bytesLeft -= bytes.size();
ctx->queue.removeOne(messageId.toLongLong());
TgClient* client = isMain() ? this : static_cast<TgClient*>(parent());
if (client == 0) {
client = this;
}
if (bytes.size() != FILE_PART_SIZE) {
processedDownloadFiles.remove(fileId);
ctx->localFile.close();
emit client->fileDownloaded(ctx->fileId, ctx->localFile.fileName());
delete ctx;
currentDownloading = 0;
downloadNextFilePart();
return;
}
else {
qint64 processed = ctx->length - ctx->bytesLeft;
emit client->fileDownloading(ctx->fileId, processed, ctx->length, (qint32) (processed * 100 / ctx->length));
}
}
downloadNextFilePart();
}
TgLongVariant TgClient::uploadFile(QString filePath)
{
TgFileCtx *ctx = new TgFileCtx(filePath);
if (ctx->length == 0) {
delete ctx;
return 0;
}
if (!ctx->localFile.open(QFile::ReadOnly)) {
delete ctx;
return 0;
}
processedFiles.insert(ctx->fileId, ctx);
uploadNextFilePart(ctx->fileId);
return ctx->fileId;
}
TgLongVariant TgClient::uploadNextFilePart(TgLongVariant fileId)
{
//TODO: seek offset and get bytes as part number (offset = partNumber * partSize, length = qMin(FILE_PART_SIZE, length - offset))
//TODO: send 2-3 parts in queue
//TODO: FILE_PARTS_INVALID
//TODO: FILE_PART_X_MISSING
//TODO: MD5_CHECKSUM_INVALID
if (!isAuthorized()) {
return 0;
}
TgFileCtx *ctx = processedFiles.value(fileId.toLongLong(), NULL);
if (ctx == NULL)
return 0;
if (ctx->length != 0 && ctx->bytesLeft <= 0) {
TGOBJECT(ctx->isBig ? TLType::InputFileBig : TLType::InputFile, inputFile);
inputFile["id"] = ctx->fileId;
inputFile["parts"] = ctx->fileParts;
inputFile["name"] = QFileInfo(ctx->localFile).fileName();
processedFiles.remove(ctx->fileId);
delete ctx;
emit fileUploaded(inputFile["id"].toLongLong(), inputFile);
return 0;
}
QByteArray fileBytes = readFully(ctx->localFile, qMin((qint64) FILE_PART_SIZE, ctx->bytesLeft));
ctx->bytesLeft -= fileBytes.size();
TGOBJECT(ctx->isBig ? TLType::UploadSaveBigFilePartMethod : TLType::UploadSaveFilePartMethod, saveFile);
saveFile["file_id"] = ctx->fileId;
saveFile["file_total_parts"] = ctx->fileParts;
saveFile["file_part"] = (ctx->currentPart)++;
saveFile["bytes"] = fileBytes;
TgLong mid;
if (ctx->isBig) {
mid = sendObject<&writeTLMethodUploadSaveBigFilePart>(saveFile);
}
else {
mid = sendObject<&writeTLMethodUploadSaveFilePart>(saveFile);
}
ctx->queue << mid;
filePackets.insert(mid, ctx->fileId);
qint64 processed = qMin(ctx->length, saveFile["file_part"].toInt() * (qint64) FILE_PART_SIZE);
emit fileUploading(saveFile["file_id"].toLongLong(), processed, ctx->length, (qint32) (processed * 100 / ctx->length));
return mid;
}
void TgClient::cancelUpload(TgLongVariant fileId)
{
TgFileCtx *ctx = processedFiles.take(fileId.toLongLong());
if (ctx == NULL)
return;
ctx->localFile.close();
emit fileUploadCanceled(fileId);
delete ctx;
}
void TgClient::handleUploadingFile(bool response, TgLongVariant messageId)
{
TgLong fileId = filePackets.take(messageId.toLongLong());
if (!response) {
kgWarning() << "File upload response is false " << messageId.toLongLong();
cancelUpload(fileId);
return;
}
uploadNextFilePart(fileId);
}