FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/data/data_cloud_file.cpp at dev · dmitry/tdesktop · GitHub
dmitry
tdesktop
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
data
/
data_cloud_file.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
379 lines (350 loc) · 9.09 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
data
/
data_cloud_file.cpp
Copy path
File metadata and controls
379 lines (350 loc) · 9.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
378
379
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#
include
"
data/data_cloud_file.h
"
#
include
"
data/data_file_origin.h
"
#
include
"
data/data_session.h
"
#
include
"
storage/cache/storage_cache_database.h
"
#
include
"
storage/file_download.h
"
#
include
"
ui/image/image.h
"
#
include
"
main/main_session.h
"
namespace
Data
{
CloudFile::~CloudFile
() {
//
Destroy loader with still alive CloudFile with already zero '.loader'.
//
Otherwise in ~FileLoader it tries to clear file.loader and crashes.
base::take
(loader);
}
void
CloudFile::clear
() {
location = {};
base::take
(loader);
byteSize =
0
;
progressivePartSize =
0
;
flags = {};
}
CloudImage::CloudImage
() =
default
;
CloudImage::CloudImage
(
not_null<Main::Session*> session,
const
ImageWithLocation &data) {
update
(session, data);
}
void
CloudImage::set
(
not_null<Main::Session*> session,
const
ImageWithLocation &data) {
const
auto
&was = _file.
location
.
file
().
data
;
const
auto
&now = data.
location
.
file
().
data
;
if
(!data.
location
.
valid
()) {
_file.
flags
|= CloudFile::Flag::Cancelled;
_file.
loader
=
nullptr
;
_file.
location
=
ImageLocation
();
_file.
byteSize
=
0
;
_file.
flags
=
CloudFile::Flag
();
_view = std::weak_ptr<QImage>();
}
else
if
(was != now
&& (!v::is<InMemoryLocation>(was) || v::is<InMemoryLocation>(now))) {
_file.
location
=
ImageLocation
();
_view = std::weak_ptr<QImage>();
}
UpdateCloudFile
(
_file,
data,
session->
data
().
cache
(),
kImageCacheTag
,
[=](FileOrigin origin) {
load
(session, origin); },
[=](QImage preloaded, QByteArray) {
setToActive
(session,
std::move
(preloaded));
});
}
void
CloudImage::update
(
not_null<Main::Session*> session,
const
ImageWithLocation &data) {
UpdateCloudFile
(
_file,
data,
session->
data
().
cache
(),
kImageCacheTag
,
[=](FileOrigin origin) {
load
(session, origin); },
[=](QImage preloaded, QByteArray) {
setToActive
(session,
std::move
(preloaded));
});
}
bool
CloudImage::empty
()
const
{
return
!_file.
location
.
valid
();
}
bool
CloudImage::loading
()
const
{
return
(_file.
loader
!=
nullptr
);
}
bool
CloudImage::failed
()
const
{
return
(_file.
flags
& CloudFile::Flag::Failed);
}
bool
CloudImage::loadedOnce
()
const
{
return
(_file.
flags
& CloudFile::Flag::Loaded);
}
void
CloudImage::load
(not_null<Main::Session*> session, FileOrigin origin) {
const
auto
autoLoading =
false
;
const
auto
finalCheck = [=] {
if
(
const
auto
active =
activeView
()) {
return
active->
isNull
();
}
else
if
(_file.
flags
& CloudFile::Flag::Loaded) {
return
false
;
}
return
!(_file.
flags
& CloudFile::Flag::Loaded);
};
const
auto
done = [=](QImage result, QByteArray) {
setToActive
(session,
std::move
(result));
};
LoadCloudFile
(
session,
_file,
origin,
LoadFromCloudOrLocal,
autoLoading,
kImageCacheTag
,
finalCheck,
done);
}
const
ImageLocation &
CloudImage::location
()
const
{
return
_file.
location
;
}
int
CloudImage::byteSize
()
const
{
return
_file.
byteSize
;
}
std::shared_ptr<QImage>
CloudImage::createView
() {
if
(
auto
active =
activeView
()) {
return
active;
}
auto
view = std::make_shared<QImage>();
_view = view;
return
view;
}
std::shared_ptr<QImage>
CloudImage::activeView
()
const
{
return
_view.
lock
();
}
bool
CloudImage::isCurrentView
(
const
std::shared_ptr<QImage> &view)
const
{
if
(!view) {
return
empty
();
}
return
!view.
owner_before
(_view) && !_view.
owner_before
(view);
}
void
CloudImage::setToActive
(
not_null<Main::Session*> session,
QImage image) {
if
(
const
auto
view =
activeView
()) {
*view = image.
isNull
()
?
Image::Empty
()->
original
()
:
std::move
(image);
session->
notifyDownloaderTaskFinished
();
}
}
void
UpdateCloudFile
(
CloudFile &file,
const
ImageWithLocation &data,
Storage::Cache::Database &cache,
uint8 cacheTag,
Fn<
void
(FileOrigin)> restartLoader,
Fn<void(QImage, QByteArray)> usePreloaded) {
if
(!data.
location
.
valid
()) {
if
(data.
progressivePartSize
&& !file.
location
.
valid
()) {
file.
progressivePartSize
= data.
progressivePartSize
;
}
if
(data.
location
.
width
()
&& data.
location
.
height
()
&& !file.
location
.
valid
()
&& !file.
location
.
width
()) {
file.
location
= data.
location
;
}
return
;
}
const
auto
needStickerThumbnailUpdate = [&] {
const
auto
was = std::get_if<StorageFileLocation>(
&file.
location
.
file
().
data
);
const
auto
now = std::get_if<StorageFileLocation>(
&data.
location
.
file
().
data
);
using
Type = StorageFileLocation::Type;
if
(!was || !now || was->
type
() != Type::StickerSetThumb) {
return
false
;
}
return
now->
valid
()
&& (now->
type
() != Type::StickerSetThumb
|| now->
cacheKey
() != was->
cacheKey
());
};
const
auto
update = !file.
location
.
valid
()
|| (data.
location
.
file
().
cacheKey
()
&& (!file.
location
.
file
().
cacheKey
()
|| (file.
location
.
width
() < data.
location
.
width
())
|| (file.
location
.
height
() < data.
location
.
height
())
||
needStickerThumbnailUpdate
()));
if
(!update) {
return
;
}
auto
cacheBytes = !data.
bytes
.
isEmpty
()
? data.
bytes
: v::is<InMemoryLocation>(file.
location
.
file
().
data
)
? v::get<InMemoryLocation>(file.
location
.
file
().
data
).
bytes
:
QByteArray
();
if
(!cacheBytes.
isEmpty
()) {
if
(
const
auto
cacheKey = data.
location
.
file
().
cacheKey
()) {
cache.
putIfEmpty
(
cacheKey,
Storage::Cache::Database::TaggedValue
(
std::move
(cacheBytes),
cacheTag));
}
}
file.
location
= data.
location
;
file.
byteSize
= data.
bytesCount
;
if
(!data.
preloaded
.
isNull
()) {
file.
loader
=
nullptr
;
if
(usePreloaded) {
usePreloaded
(data.
preloaded
, data.
bytes
);
}
}
else
if
(file.
loader
) {
const
auto
origin =
base::take
(file.
loader
)->
fileOrigin
();
restartLoader
(origin);
}
else
if
(file.
flags
& CloudFile::Flag::Failed) {
file.
flags
&= ~CloudFile::Flag::Failed;
}
}
void
LoadCloudFile
(
not_null<Main::Session*> session,
CloudFile &file,
FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool
autoLoading,
uint8 cacheTag,
Fn<
bool
()> finalCheck,
Fn<void(CloudFile&)> done,
Fn<void(
bool
)> fail,
Fn<void()> progress,
int downloadFrontPartSize = 0) {
const
auto
loadSize = downloadFrontPartSize
?
std::min
(downloadFrontPartSize, file.
byteSize
)
: file.
byteSize
;
if
(file.
loader
) {
if
(fromCloud == LoadFromCloudOrLocal) {
file.
loader
->
permitLoadFromCloud
();
}
if
(file.
loader
->
loadSize
() < loadSize) {
file.
loader
->
increaseLoadSize
(loadSize, autoLoading);
}
return
;
}
else
if
((file.
flags
& CloudFile::Flag::Failed)
|| !file.
location
.
valid
()
|| (finalCheck && !
finalCheck
())) {
return
;
}
file.
flags
&= ~CloudFile::Flag::Cancelled;
file.
loader
=
CreateFileLoader
(
session,
file.
location
.
file
(),
origin,
QString
(),
loadSize,
file.
byteSize
,
UnknownFileLocation,
LoadToCacheAsWell,
fromCloud,
autoLoading,
cacheTag);
const
auto
finish = [done](CloudFile &file) {
if
(!file.
loader
|| file.
loader
->
cancelled
()) {
file.
flags
|= CloudFile::Flag::Cancelled;
}
else
{
file.
flags
|= CloudFile::Flag::Loaded;
done
(file);
}
//
NB! file.loader may be in ~FileLoader() already.
if
(
const
auto
loader =
base::take
(file.
loader
)) {
if
((file.
flags
& CloudFile::Flag::Cancelled)
&& !loader->
cancelled
()) {
loader->
cancel
();
}
}
};
file.
loader
->
updates
(
) |
rpl::on_next_error_done
([=] {
if
(
const
auto
onstack = progress) {
onstack
();
}
}, [=, &file](FileLoader::Error error) {
finish
(file);
file.
flags
|= CloudFile::Flag::Failed;
if
(
const
auto
onstack = fail) {
onstack
(error.
started
);
}
}, [=, &file] {
finish
(file);
}, file.
loader
->
lifetime
());
file.
loader
->
start
();
}
void
LoadCloudFile
(
not_null<Main::Session*> session,
CloudFile &file,
FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool
autoLoading,
uint8 cacheTag,
Fn<
bool
()> finalCheck,
Fn<void(QImage, QByteArray)> done,
Fn<void(
bool
)> fail,
Fn<void()> progress,
int downloadFrontPartSize) {
const
auto
callback = [=](CloudFile &file) {
if
(
auto
read = file.
loader
->
imageData
(); read.
isNull
()) {
file.
flags
|= CloudFile::Flag::Failed;
if
(
const
auto
onstack = fail) {
onstack
(
true
);
}
}
else
if
(
const
auto
onstack = done) {
onstack
(
std::move
(read), file.
loader
->
bytes
());
}
};
LoadCloudFile
(
session,
file,
origin,
fromCloud,
autoLoading,
cacheTag,
finalCheck,
callback,
std::move
(fail),
std::move
(progress),
downloadFrontPartSize);
}
void
LoadCloudFile
(
not_null<Main::Session*> session,
CloudFile &file,
FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool
autoLoading,
uint8 cacheTag,
Fn<
bool
()> finalCheck,
Fn<void(QByteArray)> done,
Fn<void(
bool
)> fail,
Fn<void()> progress) {
const
auto
callback = [=](CloudFile &file) {
if
(
auto
bytes = file.
loader
->
bytes
(); bytes.
isEmpty
()) {
file.
flags
|= CloudFile::Flag::Failed;
if
(
const
auto
onstack = fail) {
onstack
(
true
);
}
}
else
if
(
const
auto
onstack = done) {
onstack
(
std::move
(bytes));
}
};
LoadCloudFile
(
session,
file,
origin,
fromCloud,
autoLoading,
cacheTag,
finalCheck,
callback,
std::move
(fail),
std::move
(progress));
}
}
//
namespace Data
Back
|
FazBrowse Home
|
New Git URL