aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/Photo.cpp
blob: d6d4aa68b1ac5c54b6d77417b49e45be360e5dbf (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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/Photo.h"

#include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileEncryptionKey.h"
#include "td/telegram/files/FileLocation.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/files/FileType.h"
#include "td/telegram/Global.h"
#include "td/telegram/net/DcId.h"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/PhotoSizeSource.h"
#include "td/telegram/PhotoSizeType.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"

#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/overloaded.h"
#include "td/utils/SliceBuilder.h"

namespace td {

Photo get_encrypted_file_photo(FileManager *file_manager, unique_ptr<EncryptedFile> &&file,
                               tl_object_ptr<secret_api::decryptedMessageMediaPhoto> &&photo,
                               DialogId owner_dialog_id) {
  FileId file_id = file_manager->register_remote(
      FullRemoteFileLocation(FileType::Encrypted, file->id_, file->access_hash_, DcId::create(file->dc_id_), string()),
      FileLocationSource::FromServer, owner_dialog_id, photo->size_, 0,
      PSTRING() << static_cast<uint64>(file->id_) << ".jpg");
  file_manager->set_encryption_key(file_id, FileEncryptionKey{photo->key_.as_slice(), photo->iv_.as_slice()});

  Photo res;
  res.id = 0;
  res.date = 0;

  if (!photo->thumb_.empty()) {
    res.photos.push_back(get_secret_thumbnail_photo_size(file_manager, std::move(photo->thumb_), owner_dialog_id,
                                                         photo->thumb_w_, photo->thumb_h_));
  }

  PhotoSize s;
  s.type = PhotoSizeType('i');
  s.dimensions = get_dimensions(photo->w_, photo->h_, nullptr);
  s.size = photo->size_;
  s.file_id = file_id;
  res.photos.push_back(s);

  return res;
}

Photo get_photo(Td *td, tl_object_ptr<telegram_api::Photo> &&photo, DialogId owner_dialog_id, FileType file_type) {
  if (photo == nullptr || photo->get_id() == telegram_api::photoEmpty::ID) {
    return Photo();
  }
  CHECK(photo->get_id() == telegram_api::photo::ID);
  return get_photo(td, move_tl_object_as<telegram_api::photo>(photo), owner_dialog_id, file_type);
}

Photo get_photo(Td *td, tl_object_ptr<telegram_api::photo> &&photo, DialogId owner_dialog_id, FileType file_type) {
  CHECK(photo != nullptr);
  Photo res;

  res.id = photo->id_;
  res.date = photo->date_;
  res.has_stickers = photo->has_stickers_;

  if (res.is_empty()) {
    LOG(ERROR) << "Receive photo with identifier " << res.id.get();
    res.id = -3;
  }

  DcId dc_id = DcId::create(photo->dc_id_);
  for (auto &size_ptr : photo->sizes_) {
    auto photo_size = get_photo_size(td->file_manager_.get(), PhotoSizeSource::thumbnail(file_type, 0), photo->id_,
                                     photo->access_hash_, photo->file_reference_.as_slice().str(), dc_id,
                                     owner_dialog_id, std::move(size_ptr), PhotoFormat::Jpeg);
    if (photo_size.get_offset() == 0) {
      PhotoSize &size = photo_size.get<0>();
      if (size.type == 0 || size.type == 't' || size.type == 'i' || size.type == 'p' || size.type == 'u' ||
          size.type == 'v') {
        LOG(ERROR) << "Skip unallowed photo size " << size;
        continue;
      }
      res.photos.push_back(std::move(size));
    } else {
      res.minithumbnail = std::move(photo_size.get<1>());
    }
  }

  for (auto &size_ptr : photo->video_sizes_) {
    auto animation =
        process_video_size(td, PhotoSizeSource::thumbnail(file_type, 0), photo->id_, photo->access_hash_,
                           photo->file_reference_.as_slice().str(), dc_id, owner_dialog_id, std::move(size_ptr));
    if (animation.empty()) {
      continue;
    }
    animation.visit(overloaded(
        [&](AnimationSize &&animation_size) {
          if (animation_size.type != 0 && animation_size.dimensions.width == animation_size.dimensions.height) {
            res.animations.push_back(std::move(animation_size));
          }
        },
        [&](unique_ptr<StickerPhotoSize> &&sticker_photo_size) {
          res.sticker_photo_size = std::move(sticker_photo_size);
        }));
  }

  return res;
}

Photo get_web_document_photo(FileManager *file_manager, tl_object_ptr<telegram_api::WebDocument> web_document,
                             DialogId owner_dialog_id) {
  PhotoSize s = get_web_document_photo_size(file_manager, FileType::Photo, owner_dialog_id, std::move(web_document));
  Photo photo;
  if (s.file_id.is_valid() && s.type != 'v' && s.type != 'g') {
    photo.id = 0;
    photo.photos.push_back(s);
  }
  return photo;
}

Result<Photo> create_photo(FileManager *file_manager, FileId file_id, PhotoSize &&thumbnail, int32 width, int32 height,
                           vector<FileId> &&sticker_file_ids) {
  TRY_RESULT(input_photo_size, get_input_photo_size(file_manager, file_id, width, height));

  Photo photo;
  auto file_view = file_manager->get_file_view(file_id);
  const auto *full_remote_location = file_view.get_full_remote_location();
  if (full_remote_location != nullptr && !full_remote_location->is_web()) {
    photo.id = full_remote_location->get_id();
  }
  if (photo.is_empty()) {
    photo.id = 0;
  }
  photo.date = G()->unix_time();
  if (thumbnail.file_id.is_valid()) {
    photo.photos.push_back(std::move(thumbnail));
  }
  photo.photos.push_back(std::move(input_photo_size));
  photo.has_stickers = !sticker_file_ids.empty();
  photo.sticker_file_ids = std::move(sticker_file_ids);
  return std::move(photo);
}

Photo dup_photo(Photo photo) {
  CHECK(!photo.photos.empty());

  // Find 'i' or largest
  PhotoSize input_size;
  for (const auto &size : photo.photos) {
    if (size.type == 'i') {
      input_size = size;
    }
  }
  if (input_size.type == 0) {
    for (const auto &size : photo.photos) {
      if (input_size.type == 0 || input_size < size) {
        input_size = size;
      }
    }
  }

  // Find 't' or smallest
  PhotoSize thumbnail;
  for (const auto &size : photo.photos) {
    if (size.type == 't') {
      thumbnail = size;
    }
  }
  if (thumbnail.type == 0) {
    for (const auto &size : photo.photos) {
      if (size.type != input_size.type && (thumbnail.type == 0 || size < thumbnail)) {
        thumbnail = size;
      }
    }
  }

  Photo result;
  result.id = std::move(photo.id);
  result.date = photo.date;
  result.minithumbnail = std::move(photo.minithumbnail);
  result.has_stickers = photo.has_stickers;
  result.sticker_file_ids = std::move(photo.sticker_file_ids);

  if (thumbnail.type != 0) {
    thumbnail.type = PhotoSizeType('t');
    result.photos.push_back(std::move(thumbnail));
  }
  input_size.type = PhotoSizeType('i');
  result.photos.push_back(std::move(input_size));

  return result;
}

tl_object_ptr<td_api::photo> get_photo_object(FileManager *file_manager, const Photo &photo) {
  if (photo.is_empty()) {
    return nullptr;
  }

  return td_api::make_object<td_api::photo>(photo.has_stickers, get_minithumbnail_object(photo.minithumbnail),
                                            get_photo_sizes_object(file_manager, photo.photos));
}

void merge_photos(Td *td, const Photo *old_photo, Photo *new_photo, DialogId dialog_id, bool need_merge_files,
                  bool &is_content_changed, bool &need_update) {
  if (old_photo->date != new_photo->date) {
    LOG(DEBUG) << "Photo date has changed from " << old_photo->date << " to " << new_photo->date;
    is_content_changed = true;
  }
  if (old_photo->id.get() != new_photo->id.get() || old_photo->minithumbnail != new_photo->minithumbnail) {
    need_update = true;
  }
  if (old_photo->photos != new_photo->photos) {
    LOG(DEBUG) << "Merge photos " << old_photo->photos << " and " << new_photo->photos
               << ", need_merge_files = " << need_merge_files;
    auto new_photos_size = new_photo->photos.size();
    auto old_photos_size = old_photo->photos.size();

    bool need_merge = false;
    if (need_merge_files && (old_photos_size == 1 || (old_photos_size == 2 && old_photo->photos[0].type == 't')) &&
        old_photo->photos.back().type == 'i') {
      // first time get info about sent photo
      if (!new_photo->photos.empty() && new_photo->photos.back().type == 'i') {
        // remove previous 'i' size for the photo if any
        new_photo->photos.pop_back();
      }
      if (!new_photo->photos.empty() && new_photo->photos.back().type == 't') {
        // remove previous 't' size for the photo if any
        new_photo->photos.pop_back();
      }

      // add back 't' and 'i' sizes
      if (old_photos_size == 2) {
        new_photo->photos.push_back(old_photo->photos[0]);
      }
      new_photo->photos.push_back(old_photo->photos.back());
      need_merge = true;
      need_update = true;
    } else {
      // get sent photo again
      if (old_photos_size == 2 + new_photos_size && old_photo->photos[new_photos_size].type == 't') {
        new_photo->photos.push_back(old_photo->photos[new_photos_size]);
      }
      if (old_photos_size == 1 + new_photo->photos.size() && old_photo->photos.back().type == 'i') {
        new_photo->photos.push_back(old_photo->photos.back());
        need_merge = true;
      }
      if (old_photo->photos != new_photo->photos) {
        new_photo->photos.resize(new_photos_size);  // return previous size, because we shouldn't add local photo sizes
        need_merge = false;
        need_update = true;
      }
    }

    LOG(DEBUG) << "Merge photos " << old_photo->photos << " and " << new_photo->photos
               << " with new photos size = " << new_photos_size << ", need_merge = " << need_merge
               << ", need_update = " << need_update;
    if (need_merge && new_photos_size != 0) {
      CHECK(!old_photo->photos.empty());
      CHECK(old_photo->photos.back().type == 'i');
      FileId old_file_id = old_photo->photos.back().file_id;
      FileView old_file_view = td->file_manager_->get_file_view(old_file_id);
      const auto *old_main_remote_location = old_file_view.get_main_remote_location();
      FileId new_file_id = new_photo->photos[0].file_id;
      FileView new_file_view = td->file_manager_->get_file_view(new_file_id);
      const auto *new_full_remote_location = new_file_view.get_full_remote_location();
      CHECK(new_full_remote_location != nullptr);

      LOG(DEBUG) << "Trying to merge old file " << old_file_id << " and new file " << new_file_id;

      if (new_full_remote_location->is_web()) {
        LOG(ERROR) << "Have remote web photo location";
      } else if (old_main_remote_location == nullptr ||
                 old_main_remote_location->get_file_reference() != new_full_remote_location->get_file_reference() ||
                 old_main_remote_location->get_access_hash() != new_full_remote_location->get_access_hash()) {
        FileId file_id = td->file_manager_->register_remote(
            FullRemoteFileLocation(PhotoSizeSource::thumbnail(new_file_view.get_type(), 'i'),
                                   new_full_remote_location->get_id(), new_full_remote_location->get_access_hash(),
                                   DcId::invalid(), new_full_remote_location->get_file_reference().str()),
            FileLocationSource::FromServer, dialog_id, old_photo->photos.back().size, 0, "");
        LOG_STATUS(td->file_manager_->merge(file_id, old_file_id));
      }
    }
  }
}

void photo_delete_thumbnail(Photo &photo) {
  for (size_t i = 0; i < photo.photos.size(); i++) {
    if (photo.photos[i].type == 't') {
      photo.photos.erase(photo.photos.begin() + i);
      return;
    }
  }
}

tl_object_ptr<telegram_api::InputMedia> photo_get_input_media(
    const FileManager *file_manager, const Photo &photo, telegram_api::object_ptr<telegram_api::InputFile> input_file,
    int32 ttl, bool has_spoiler, FileId video_file_id) {
  bool is_live_photo = false;
  telegram_api::object_ptr<telegram_api::InputDocument> input_video;
  if (video_file_id != FileId()) {
    auto video_file_view = file_manager->get_file_view(video_file_id);
    if (!video_file_view.is_encrypted()) {
      const auto *main_remote_location = video_file_view.get_main_remote_location();
      if (main_remote_location != nullptr && !main_remote_location->is_web()) {
        input_video = main_remote_location->as_input_document();
        is_live_photo = true;
      }
    }
  }
  if (!photo.photos.empty()) {
    auto file_id = photo.photos.back().file_id;
    auto file_view = file_manager->get_file_view(file_id);
    if (file_view.is_encrypted()) {
      return nullptr;
    }
    const auto *main_remote_location = file_view.get_main_remote_location();
    if (main_remote_location != nullptr && !main_remote_location->is_web() && input_file == nullptr) {
      int32 flags = 0;
      if (ttl != 0) {
        flags |= telegram_api::inputMediaPhoto::TTL_SECONDS_MASK;
      }
      return make_tl_object<telegram_api::inputMediaPhoto>(
          flags, has_spoiler, is_live_photo, main_remote_location->as_input_photo(), ttl, std::move(input_video));
    }
    const auto *url = file_view.get_url();
    if (url != nullptr) {
      int32 flags = 0;
      if (ttl != 0) {
        flags |= telegram_api::inputMediaPhotoExternal::TTL_SECONDS_MASK;
      }
      LOG(INFO) << "Create inputMediaPhotoExternal with a URL " << *url << " and self-destruct time " << ttl;
      return make_tl_object<telegram_api::inputMediaPhotoExternal>(flags, has_spoiler, *url, ttl);
    }
    if (input_file == nullptr) {
      CHECK(main_remote_location == nullptr);
    }
  }
  if (input_file != nullptr) {
    int32 flags = 0;
    vector<tl_object_ptr<telegram_api::InputDocument>> added_stickers;
    if (photo.has_stickers) {
      flags |= telegram_api::inputMediaUploadedPhoto::STICKERS_MASK;
      added_stickers = file_manager->get_input_documents(photo.sticker_file_ids);
    }
    if (ttl != 0) {
      flags |= telegram_api::inputMediaUploadedPhoto::TTL_SECONDS_MASK;
    }

    CHECK(!photo.photos.empty());
    return make_tl_object<telegram_api::inputMediaUploadedPhoto>(flags, has_spoiler, is_live_photo,
                                                                 std::move(input_file), std::move(added_stickers), ttl,
                                                                 std::move(input_video));
  }
  return nullptr;
}

SecretInputMedia photo_get_secret_input_media(const FileManager *file_manager, const Photo &photo,
                                              telegram_api::object_ptr<telegram_api::InputEncryptedFile> input_file,
                                              const string &caption, BufferSlice thumbnail) {
  FileId file_id;
  int32 width = 0;
  int32 height = 0;

  FileId thumbnail_file_id;
  int32 thumbnail_width = 0;
  int32 thumbnail_height = 0;
  for (const auto &size : photo.photos) {
    if (size.type == 'i') {
      file_id = size.file_id;
      width = size.dimensions.width;
      height = size.dimensions.height;
    }
    if (size.type == 't') {
      thumbnail_file_id = size.file_id;
      thumbnail_width = size.dimensions.width;
      thumbnail_height = size.dimensions.height;
    }
  }
  if (file_id.empty()) {
    LOG(ERROR) << "NO SIZE";
    return {};
  }
  auto file_view = file_manager->get_file_view(file_id);
  auto &encryption_key = file_view.encryption_key();
  if (!file_view.is_encrypted_secret() || encryption_key.empty()) {
    return {};
  }
  const auto *main_remote_location = file_view.get_main_remote_location();
  if (main_remote_location != nullptr) {
    LOG(INFO) << "Photo has remote location";
    input_file = main_remote_location->as_input_encrypted_file();
  }
  if (input_file == nullptr) {
    return {};
  }
  if (thumbnail_file_id.is_valid() && thumbnail.empty()) {
    return {};
  }
  auto size = file_view.size();
  if (size < 0 || size >= 1000000000) {
    size = 0;
  }

  return SecretInputMedia{
      std::move(input_file),
      make_tl_object<secret_api::decryptedMessageMediaPhoto>(
          std::move(thumbnail), thumbnail_width, thumbnail_height, width, height, static_cast<int32>(size),
          BufferSlice(encryption_key.key_slice()), BufferSlice(encryption_key.iv_slice()), caption)};
}

telegram_api::object_ptr<telegram_api::InputMedia> photo_get_cover_input_media(const FileManager *file_manager,
                                                                               const Photo &photo, bool force,
                                                                               bool allow_external) {
  auto input_media = photo_get_input_media(file_manager, photo, nullptr, 0, false, FileId());
  if (input_media == nullptr || (!allow_external && input_media->get_id() != telegram_api::inputMediaPhoto::ID)) {
    return nullptr;
  }
  auto file_reference = FileManager::extract_file_reference(input_media);
  if (file_reference == FileReferenceView::invalid_file_reference()) {
    if (!force) {
      LOG(INFO) << "Have invalid file reference for cover " << photo;
      return nullptr;
    }
  }
  return input_media;
}

vector<FileId> photo_get_file_ids(const Photo &photo) {
  vector<FileId> file_ids;
  photo_append_file_ids(photo, file_ids);
  return file_ids;
}

void photo_append_file_ids(const Photo &photo, vector<FileId> &file_ids) {
  // photo file identifiers must be at the beginning for register_suggested_profile_photo
  for (auto &photo_size : photo.photos) {
    file_ids.push_back(photo_size.file_id);
  }
  for (auto &animation_size : photo.animations) {
    file_ids.push_back(animation_size.file_id);
  }
}

FileId get_photo_any_file_id(const Photo &photo) {
  const auto &sizes = photo.photos;
  if (!sizes.empty()) {
    return sizes.back().file_id;
  }
  return FileId();
}

FileId get_photo_thumbnail_file_id(const Photo &photo) {
  for (auto &size : photo.photos) {
    if (size.type == 't') {
      return size.file_id;
    }
  }
  return FileId();
}

bool operator==(const Photo &lhs, const Photo &rhs) {
  return lhs.id.get() == rhs.id.get() && lhs.photos == rhs.photos && lhs.animations == rhs.animations &&
         lhs.sticker_photo_size == rhs.sticker_photo_size;
}

bool operator!=(const Photo &lhs, const Photo &rhs) {
  return !(lhs == rhs);
}

StringBuilder &operator<<(StringBuilder &string_builder, const Photo &photo) {
  string_builder << "[ID = " << photo.id.get() << ", date = " << photo.date << ", photos = " << photo.photos;
  if (!photo.animations.empty()) {
    string_builder << ", animations = " << photo.animations;
  }
  if (photo.sticker_photo_size != nullptr) {
    string_builder << ", sticker = " << *photo.sticker_photo_size;
  }
  return string_builder << ']';
}

}  // namespace td