2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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/DocumentsManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AnimationsManager.h"
|
|
|
|
#include "td/telegram/AudiosManager.h"
|
2020-05-03 14:44:33 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2022-06-02 16:52:12 +02:00
|
|
|
#include "td/telegram/Dimensions.h"
|
2019-04-09 17:38:57 +02:00
|
|
|
#include "td/telegram/Document.h"
|
2018-12-27 20:24:44 +01:00
|
|
|
#include "td/telegram/files/FileEncryptionKey.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2022-08-14 14:04:08 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2019-10-04 17:00:51 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/telegram/PhotoSizeSource.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/secret_api.h"
|
2022-01-18 17:20:43 +01:00
|
|
|
#include "td/telegram/StickerFormat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/StickersManager.h"
|
2022-07-17 22:49:46 +02:00
|
|
|
#include "td/telegram/StickerType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/VideoNotesManager.h"
|
|
|
|
#include "td/telegram/VideosManager.h"
|
|
|
|
#include "td/telegram/VoiceNotesManager.h"
|
|
|
|
|
2022-08-14 14:04:08 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-03-03 22:33:26 +01:00
|
|
|
#include "td/utils/HttpUrl.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/MimeType.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/PathView.h"
|
2018-03-03 22:33:26 +01:00
|
|
|
#include "td/utils/Random.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/utf8.h"
|
|
|
|
|
2023-05-01 20:07:01 +02:00
|
|
|
#include <cmath>
|
2018-03-03 22:33:26 +01:00
|
|
|
#include <limits>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
DocumentsManager::DocumentsManager(Td *td) : td_(td) {
|
|
|
|
}
|
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
DocumentsManager::~DocumentsManager() {
|
|
|
|
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), documents_);
|
|
|
|
}
|
|
|
|
|
2021-08-28 13:16:29 +02:00
|
|
|
tl_object_ptr<td_api::document> DocumentsManager::get_document_object(FileId file_id,
|
|
|
|
PhotoFormat thumbnail_format) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-03 22:23:32 +02:00
|
|
|
auto document = get_document(file_id);
|
2021-08-28 13:16:29 +02:00
|
|
|
CHECK(document != nullptr);
|
2020-05-31 21:22:15 +02:00
|
|
|
return make_tl_object<td_api::document>(
|
|
|
|
document->file_name, document->mime_type, get_minithumbnail_object(document->minithumbnail),
|
|
|
|
get_thumbnail_object(td_->file_manager_.get(), document->thumbnail, thumbnail_format),
|
|
|
|
td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:38:57 +02:00
|
|
|
Document DocumentsManager::on_get_document(RemoteDocument remote_document, DialogId owner_dialog_id,
|
|
|
|
MultiPromiseActor *load_data_multipromise_ptr,
|
2023-05-25 12:57:12 +02:00
|
|
|
Document::Type default_document_type, Subtype document_subtype) {
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::documentAttributeAnimated> animated;
|
|
|
|
tl_object_ptr<telegram_api::documentAttributeVideo> video;
|
|
|
|
tl_object_ptr<telegram_api::documentAttributeAudio> audio;
|
|
|
|
tl_object_ptr<telegram_api::documentAttributeSticker> sticker;
|
2022-07-14 16:11:36 +02:00
|
|
|
tl_object_ptr<telegram_api::documentAttributeCustomEmoji> custom_emoji;
|
2018-12-31 20:04:05 +01:00
|
|
|
Dimensions dimensions;
|
|
|
|
string file_name;
|
|
|
|
bool has_stickers = false;
|
|
|
|
int32 type_attributes = 0;
|
|
|
|
for (auto &attribute : remote_document.attributes) {
|
|
|
|
switch (attribute->get_id()) {
|
|
|
|
case telegram_api::documentAttributeImageSize::ID: {
|
|
|
|
auto image_size = move_tl_object_as<telegram_api::documentAttributeImageSize>(attribute);
|
2021-08-03 00:57:46 +02:00
|
|
|
dimensions =
|
|
|
|
get_dimensions(image_size->w_, image_size->h_, oneline(to_string(remote_document.document)).c_str());
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::documentAttributeAnimated::ID:
|
|
|
|
animated = move_tl_object_as<telegram_api::documentAttributeAnimated>(attribute);
|
|
|
|
type_attributes++;
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeSticker::ID:
|
|
|
|
sticker = move_tl_object_as<telegram_api::documentAttributeSticker>(attribute);
|
|
|
|
type_attributes++;
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeVideo::ID:
|
|
|
|
video = move_tl_object_as<telegram_api::documentAttributeVideo>(attribute);
|
|
|
|
type_attributes++;
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeAudio::ID:
|
|
|
|
audio = move_tl_object_as<telegram_api::documentAttributeAudio>(attribute);
|
|
|
|
type_attributes++;
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeFilename::ID:
|
|
|
|
file_name = std::move(static_cast<telegram_api::documentAttributeFilename *>(attribute.get())->file_name_);
|
|
|
|
break;
|
|
|
|
case telegram_api::documentAttributeHasStickers::ID:
|
|
|
|
has_stickers = true;
|
|
|
|
break;
|
2022-07-01 13:33:59 +02:00
|
|
|
case telegram_api::documentAttributeCustomEmoji::ID:
|
2022-07-14 16:11:36 +02:00
|
|
|
custom_emoji = move_tl_object_as<telegram_api::documentAttributeCustomEmoji>(attribute);
|
|
|
|
type_attributes++;
|
2022-07-01 13:33:59 +02:00
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32 video_duration = 0;
|
2023-05-19 13:57:57 +02:00
|
|
|
int32 video_preload_prefix_size = 0;
|
2022-10-20 22:23:40 +02:00
|
|
|
string video_waveform;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (video != nullptr) {
|
2023-05-01 20:07:01 +02:00
|
|
|
video_duration = static_cast<int32>(std::ceil(video->duration_));
|
2023-05-25 13:19:54 +02:00
|
|
|
if (document_subtype == Subtype::Story) {
|
|
|
|
video_preload_prefix_size = video->preload_prefix_size_;
|
|
|
|
}
|
2021-01-28 13:03:52 +01:00
|
|
|
auto video_dimensions = get_dimensions(video->w_, video->h_, "documentAttributeVideo");
|
|
|
|
if (dimensions.width == 0 || (video_dimensions.width != 0 && video_dimensions != dimensions)) {
|
|
|
|
if (dimensions.width != 0) {
|
|
|
|
LOG(ERROR) << "Receive ambiguous video dimensions " << dimensions << " and " << video_dimensions;
|
|
|
|
}
|
|
|
|
dimensions = video_dimensions;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-20 22:23:40 +02:00
|
|
|
if (audio != nullptr) {
|
|
|
|
video_waveform = audio->waveform_.as_slice().str();
|
|
|
|
type_attributes--;
|
|
|
|
audio = nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (animated != nullptr) {
|
|
|
|
type_attributes--;
|
2021-08-01 06:40:57 +02:00
|
|
|
if ((video->flags_ & telegram_api::documentAttributeVideo::ROUND_MESSAGE_MASK) != 0) {
|
|
|
|
// video note without sound
|
|
|
|
animated = nullptr;
|
2022-07-14 16:11:36 +02:00
|
|
|
} else if (sticker != nullptr || custom_emoji != nullptr) {
|
2022-01-24 17:06:31 +01:00
|
|
|
// sticker
|
|
|
|
type_attributes--;
|
|
|
|
animated = nullptr;
|
|
|
|
video = nullptr;
|
2021-08-01 06:40:57 +02:00
|
|
|
} else {
|
|
|
|
// video animation
|
|
|
|
video = nullptr;
|
|
|
|
}
|
2022-07-14 16:11:36 +02:00
|
|
|
} else if (sticker != nullptr || custom_emoji != nullptr) {
|
2022-01-30 09:59:37 +01:00
|
|
|
// some stickers uploaded before release
|
|
|
|
type_attributes--;
|
|
|
|
video = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (animated != nullptr && audio != nullptr) {
|
|
|
|
// animation send as audio
|
|
|
|
type_attributes--;
|
|
|
|
audio = nullptr;
|
|
|
|
}
|
|
|
|
if (animated != nullptr && sticker != nullptr) {
|
|
|
|
// animation send as sticker
|
|
|
|
type_attributes--;
|
|
|
|
sticker = nullptr;
|
|
|
|
}
|
2022-07-14 16:11:36 +02:00
|
|
|
if (animated != nullptr && custom_emoji != nullptr) {
|
|
|
|
// just in case
|
|
|
|
type_attributes--;
|
|
|
|
custom_emoji = nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-03 22:33:26 +01:00
|
|
|
auto document_type = default_document_type;
|
2018-12-31 20:04:05 +01:00
|
|
|
FileType file_type = FileType::Document;
|
|
|
|
Slice default_extension;
|
2018-02-07 00:31:38 +01:00
|
|
|
bool supports_streaming = false;
|
2022-01-18 17:20:43 +01:00
|
|
|
StickerFormat sticker_format = StickerFormat::Unknown;
|
2020-05-11 21:17:49 +02:00
|
|
|
PhotoFormat thumbnail_format = PhotoFormat::Jpeg;
|
2019-04-09 17:38:57 +02:00
|
|
|
if (type_attributes == 1 || default_document_type != Document::Type::General) { // not a general document
|
|
|
|
if (animated != nullptr || default_document_type == Document::Type::Animation) {
|
|
|
|
document_type = Document::Type::Animation;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::Animation;
|
2019-02-13 00:29:52 +01:00
|
|
|
default_extension = Slice("mp4");
|
2019-04-09 17:38:57 +02:00
|
|
|
} else if (audio != nullptr || default_document_type == Document::Type::Audio ||
|
|
|
|
default_document_type == Document::Type::VoiceNote) {
|
|
|
|
bool is_voice_note = default_document_type == Document::Type::VoiceNote;
|
2018-03-03 22:33:26 +01:00
|
|
|
if (audio != nullptr) {
|
2018-12-04 19:18:07 +01:00
|
|
|
is_voice_note = (audio->flags_ & telegram_api::documentAttributeAudio::VOICE_MASK) != 0;
|
2018-03-03 22:33:26 +01:00
|
|
|
}
|
|
|
|
if (is_voice_note) {
|
2019-04-09 17:38:57 +02:00
|
|
|
document_type = Document::Type::VoiceNote;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::VoiceNote;
|
2019-02-13 00:29:52 +01:00
|
|
|
default_extension = Slice("oga");
|
2018-12-31 20:04:05 +01:00
|
|
|
file_name.clear();
|
|
|
|
} else {
|
2019-04-09 17:38:57 +02:00
|
|
|
document_type = Document::Type::Audio;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::Audio;
|
2019-02-13 00:29:52 +01:00
|
|
|
default_extension = Slice("mp3");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-07-14 16:11:36 +02:00
|
|
|
} else if (sticker != nullptr || custom_emoji != nullptr || default_document_type == Document::Type::Sticker) {
|
2019-04-09 17:38:57 +02:00
|
|
|
document_type = Document::Type::Sticker;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::Sticker;
|
2022-01-18 17:20:43 +01:00
|
|
|
sticker_format = StickerFormat::Webp;
|
2019-02-13 00:29:52 +01:00
|
|
|
default_extension = Slice("webp");
|
2018-12-31 20:04:05 +01:00
|
|
|
owner_dialog_id = DialogId();
|
|
|
|
file_name.clear();
|
2019-04-09 17:38:57 +02:00
|
|
|
} else if (video != nullptr || default_document_type == Document::Type::Video ||
|
|
|
|
default_document_type == Document::Type::VideoNote) {
|
|
|
|
bool is_video_note = default_document_type == Document::Type::VideoNote;
|
2018-03-03 22:33:26 +01:00
|
|
|
if (video != nullptr) {
|
|
|
|
is_video_note = (video->flags_ & telegram_api::documentAttributeVideo::ROUND_MESSAGE_MASK) != 0;
|
|
|
|
if (!is_video_note) {
|
|
|
|
supports_streaming = (video->flags_ & telegram_api::documentAttributeVideo::SUPPORTS_STREAMING_MASK) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_video_note) {
|
2019-04-09 17:38:57 +02:00
|
|
|
document_type = Document::Type::VideoNote;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::VideoNote;
|
|
|
|
file_name.clear();
|
|
|
|
} else {
|
2019-04-09 17:38:57 +02:00
|
|
|
document_type = Document::Type::Video;
|
2018-12-31 20:04:05 +01:00
|
|
|
file_type = FileType::Video;
|
|
|
|
}
|
2019-02-13 00:29:52 +01:00
|
|
|
default_extension = Slice("mp4");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else if (type_attributes >= 2) {
|
|
|
|
LOG(WARNING) << "Receive document with more than 1 type attribute: animated = " << to_string(animated)
|
2022-07-14 16:11:36 +02:00
|
|
|
<< ", sticker = " << to_string(sticker) << ", custom_emoji = " << to_string(custom_emoji)
|
|
|
|
<< ", video = " << to_string(video) << ", audio = " << to_string(audio)
|
|
|
|
<< ", file_name = " << file_name << ", dimensions = " << dimensions
|
2018-12-31 20:04:05 +01:00
|
|
|
<< ", has_stickers = " << has_stickers;
|
|
|
|
}
|
|
|
|
|
2023-05-25 12:57:12 +02:00
|
|
|
switch (document_subtype) {
|
|
|
|
case Subtype::Background:
|
|
|
|
if (document_type != Document::Type::General) {
|
|
|
|
LOG(ERROR) << "Receive background of type " << document_type;
|
|
|
|
document_type = Document::Type::General;
|
|
|
|
}
|
|
|
|
file_type = FileType::Background;
|
|
|
|
default_extension = Slice("jpg");
|
|
|
|
break;
|
|
|
|
case Subtype::Pattern:
|
|
|
|
if (document_type != Document::Type::General) {
|
|
|
|
LOG(ERROR) << "Receive background of type " << document_type;
|
|
|
|
document_type = Document::Type::General;
|
|
|
|
}
|
|
|
|
file_type = FileType::Background;
|
2019-05-07 04:51:56 +02:00
|
|
|
default_extension = Slice("png");
|
2020-05-11 21:17:49 +02:00
|
|
|
thumbnail_format = PhotoFormat::Png;
|
2023-05-25 12:57:12 +02:00
|
|
|
break;
|
|
|
|
case Subtype::Ringtone:
|
|
|
|
if (document_type != Document::Type::Audio) {
|
|
|
|
LOG(ERROR) << "Receive notification tone of type " << document_type;
|
|
|
|
document_type = Document::Type::Audio;
|
|
|
|
}
|
|
|
|
file_type = FileType::Ringtone;
|
|
|
|
default_extension = Slice("mp3");
|
|
|
|
break;
|
2023-05-25 13:19:54 +02:00
|
|
|
case Subtype::Story:
|
|
|
|
if (document_type != Document::Type::Video) {
|
|
|
|
LOG(ERROR) << "Receive story of type " << document_type;
|
|
|
|
document_type = Document::Type::Video;
|
|
|
|
}
|
|
|
|
file_type = FileType::VideoStory;
|
|
|
|
default_extension = Slice("mp4");
|
|
|
|
break;
|
2023-05-25 12:57:12 +02:00
|
|
|
default:
|
|
|
|
break;
|
2022-04-12 21:50:20 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 id;
|
|
|
|
int64 access_hash;
|
|
|
|
int32 dc_id;
|
2022-05-10 16:13:16 +02:00
|
|
|
int64 size;
|
2022-04-15 14:30:51 +02:00
|
|
|
int32 date = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
string mime_type;
|
2018-12-05 10:32:31 +01:00
|
|
|
string file_reference;
|
2019-03-01 20:51:33 +01:00
|
|
|
string minithumbnail;
|
2018-12-31 20:04:05 +01:00
|
|
|
PhotoSize thumbnail;
|
2020-07-07 10:41:01 +02:00
|
|
|
AnimationSize animated_thumbnail;
|
2022-04-26 15:01:29 +02:00
|
|
|
FileId premium_animation_file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
FileEncryptionKey encryption_key;
|
2018-03-03 22:33:26 +01:00
|
|
|
bool is_web = false;
|
|
|
|
bool is_web_no_proxy = false;
|
|
|
|
string url;
|
2019-06-17 15:46:20 +02:00
|
|
|
FileLocationSource source = FileLocationSource::FromServer;
|
2019-07-15 02:43:05 +02:00
|
|
|
|
2022-01-25 12:58:04 +01:00
|
|
|
auto fix_tgs_sticker_type = [&] {
|
2019-07-15 02:43:05 +02:00
|
|
|
if (mime_type != "application/x-tgsticker") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
sticker_format = StickerFormat::Tgs;
|
2022-01-25 12:58:04 +01:00
|
|
|
default_extension = Slice("tgs");
|
2019-07-15 02:43:05 +02:00
|
|
|
if (document_type == Document::Type::General) {
|
|
|
|
document_type = Document::Type::Sticker;
|
|
|
|
file_type = FileType::Sticker;
|
|
|
|
owner_dialog_id = DialogId();
|
|
|
|
file_name.clear();
|
2020-05-31 21:22:15 +02:00
|
|
|
thumbnail_format = PhotoFormat::Webp;
|
2019-07-15 02:43:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (remote_document.document != nullptr) {
|
|
|
|
auto document = std::move(remote_document.document);
|
|
|
|
|
|
|
|
id = document->id_;
|
|
|
|
access_hash = document->access_hash_;
|
|
|
|
dc_id = document->dc_id_;
|
|
|
|
size = document->size_;
|
2023-05-25 12:57:12 +02:00
|
|
|
if (document_subtype == Subtype::Ringtone) {
|
2022-04-15 14:30:51 +02:00
|
|
|
date = document->date_;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
mime_type = std::move(document->mime_type_);
|
2018-12-05 10:32:31 +01:00
|
|
|
file_reference = document->file_reference_.as_slice().str();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-25 19:38:25 +01:00
|
|
|
if (document_type == Document::Type::Sticker && StickersManager::has_webp_thumbnail(document->thumbs_)) {
|
|
|
|
thumbnail_format = PhotoFormat::Webp;
|
2021-03-01 21:48:48 +01:00
|
|
|
}
|
2022-01-25 12:58:04 +01:00
|
|
|
fix_tgs_sticker_type();
|
2019-07-15 02:43:05 +02:00
|
|
|
|
2019-06-17 15:46:20 +02:00
|
|
|
if (owner_dialog_id.get_type() == DialogType::SecretChat) {
|
|
|
|
// secret_api::decryptedMessageMediaExternalDocument
|
|
|
|
if (document_type != Document::Type::Sticker) {
|
|
|
|
LOG(ERROR) << "Receive " << document_type << " in " << owner_dialog_id;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
source = FileLocationSource::FromUser;
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:38:57 +02:00
|
|
|
if (document_type != Document::Type::VoiceNote) {
|
2023-02-14 13:10:27 +01:00
|
|
|
for (auto &thumbnail_ptr : document->thumbs_) {
|
2021-10-27 15:52:22 +02:00
|
|
|
auto photo_size = get_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 0),
|
|
|
|
id, access_hash, file_reference, DcId::create(dc_id), owner_dialog_id,
|
2023-02-14 13:10:27 +01:00
|
|
|
std::move(thumbnail_ptr), thumbnail_format);
|
2019-03-15 14:52:55 +01:00
|
|
|
if (photo_size.get_offset() == 0) {
|
2020-11-09 21:41:23 +01:00
|
|
|
if (!thumbnail.file_id.is_valid()) {
|
|
|
|
thumbnail = std::move(photo_size.get<0>());
|
|
|
|
}
|
2019-03-15 14:52:55 +01:00
|
|
|
} else {
|
|
|
|
minithumbnail = std::move(photo_size.get<1>());
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-02-14 13:10:27 +01:00
|
|
|
for (auto &thumbnail_ptr : document->video_thumbs_) {
|
|
|
|
if (thumbnail_ptr->get_id() != telegram_api::videoSize::ID) {
|
2023-01-06 10:42:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-14 13:10:27 +01:00
|
|
|
auto video_size = move_tl_object_as<telegram_api::videoSize>(thumbnail_ptr);
|
|
|
|
if (video_size->type_ == "v") {
|
2022-04-26 15:01:29 +02:00
|
|
|
if (!animated_thumbnail.file_id.is_valid()) {
|
2023-01-23 09:51:00 +01:00
|
|
|
animated_thumbnail =
|
2023-01-20 15:31:33 +01:00
|
|
|
get_animation_size(td_, PhotoSizeSource::thumbnail(FileType::Thumbnail, 0), id, access_hash,
|
2023-02-14 13:10:27 +01:00
|
|
|
file_reference, DcId::create(dc_id), owner_dialog_id, std::move(video_size));
|
2022-04-26 15:01:29 +02:00
|
|
|
}
|
2023-02-14 13:10:27 +01:00
|
|
|
} else if (video_size->type_ == "f") {
|
2022-04-26 15:01:29 +02:00
|
|
|
if (!premium_animation_file_id.is_valid()) {
|
|
|
|
premium_animation_file_id =
|
|
|
|
register_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 'f'), id,
|
2023-02-14 13:10:27 +01:00
|
|
|
access_hash, file_reference, owner_dialog_id, video_size->size_, DcId::create(dc_id),
|
2023-03-14 22:23:41 +01:00
|
|
|
get_sticker_format_photo_format(sticker_format), "on_get_document");
|
2020-05-30 00:48:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 22:33:26 +01:00
|
|
|
} else if (remote_document.secret_file != nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(remote_document.secret_document != nullptr);
|
|
|
|
auto file = std::move(remote_document.secret_file);
|
|
|
|
auto document = std::move(remote_document.secret_document);
|
|
|
|
|
|
|
|
id = file->id_;
|
|
|
|
access_hash = file->access_hash_;
|
|
|
|
dc_id = file->dc_id_;
|
|
|
|
size = document->size_;
|
|
|
|
mime_type = std::move(document->mime_type_);
|
|
|
|
file_type = FileType::Encrypted;
|
|
|
|
encryption_key = FileEncryptionKey{document->key_.as_slice(), document->iv_.as_slice()};
|
|
|
|
if (encryption_key.empty()) {
|
2019-04-09 22:39:41 +02:00
|
|
|
return {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 12:58:04 +01:00
|
|
|
// do not allow encrypted TGS stickers
|
|
|
|
// fix_tgs_sticker_type();
|
2020-05-05 23:56:44 +02:00
|
|
|
|
2019-04-09 17:38:57 +02:00
|
|
|
if (document_type != Document::Type::VoiceNote) {
|
2019-03-01 20:51:33 +01:00
|
|
|
thumbnail = get_secret_thumbnail_photo_size(td_->file_manager_.get(), std::move(document->thumb_),
|
|
|
|
owner_dialog_id, document->thumb_w_, document->thumb_h_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-03 22:33:26 +01:00
|
|
|
} else {
|
|
|
|
is_web = true;
|
|
|
|
id = Random::fast(0, std::numeric_limits<int32>::max());
|
|
|
|
dc_id = 0;
|
|
|
|
access_hash = 0;
|
2020-05-30 00:48:56 +02:00
|
|
|
if (remote_document.thumbnail.type == 'v') {
|
2020-07-07 10:41:01 +02:00
|
|
|
static_cast<PhotoSize &>(animated_thumbnail) = std::move(remote_document.thumbnail);
|
2020-05-30 00:48:56 +02:00
|
|
|
} else {
|
|
|
|
thumbnail = std::move(remote_document.thumbnail);
|
2020-06-01 19:34:48 +02:00
|
|
|
if (remote_document.thumbnail.type == 'g') {
|
|
|
|
thumbnail_format = PhotoFormat::Gif;
|
|
|
|
}
|
2020-05-30 00:48:56 +02:00
|
|
|
}
|
2018-03-03 22:33:26 +01:00
|
|
|
|
|
|
|
auto web_document_ptr = std::move(remote_document.web_document);
|
|
|
|
switch (web_document_ptr->get_id()) {
|
|
|
|
case telegram_api::webDocument::ID: {
|
|
|
|
auto web_document = move_tl_object_as<telegram_api::webDocument>(web_document_ptr);
|
|
|
|
auto r_http_url = parse_url(web_document->url_);
|
|
|
|
if (r_http_url.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't parse URL " << web_document->url_;
|
2019-04-09 22:39:41 +02:00
|
|
|
return {};
|
2018-03-03 22:33:26 +01:00
|
|
|
}
|
|
|
|
auto http_url = r_http_url.move_as_ok();
|
|
|
|
|
|
|
|
access_hash = web_document->access_hash_;
|
|
|
|
url = http_url.get_url();
|
|
|
|
file_name = get_url_query_file_name(http_url.query_);
|
|
|
|
mime_type = std::move(web_document->mime_type_);
|
|
|
|
size = web_document->size_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::webDocumentNoProxy::ID: {
|
|
|
|
is_web_no_proxy = true;
|
|
|
|
auto web_document = move_tl_object_as<telegram_api::webDocumentNoProxy>(web_document_ptr);
|
|
|
|
|
|
|
|
if (web_document->url_.find('.') == string::npos) {
|
|
|
|
LOG(ERROR) << "Receive invalid URL " << web_document->url_;
|
2019-04-09 22:39:41 +02:00
|
|
|
return {};
|
2018-03-03 22:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
url = std::move(web_document->url_);
|
|
|
|
file_name = get_url_file_name(url);
|
|
|
|
mime_type = std::move(web_document->mime_type_);
|
|
|
|
size = web_document->size_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2020-05-05 23:56:44 +02:00
|
|
|
|
2022-01-25 12:58:04 +01:00
|
|
|
// do not allow web TGS stickers
|
|
|
|
// fix_tgs_sticker_type();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-01-25 19:38:25 +01:00
|
|
|
if (document_type == Document::Type::Sticker && mime_type == "video/webm") {
|
|
|
|
sticker_format = StickerFormat::Webm;
|
|
|
|
default_extension = Slice("webm");
|
|
|
|
}
|
2022-01-25 19:52:15 +01:00
|
|
|
if (file_type == FileType::Encrypted && document_type == Document::Type::Sticker &&
|
2022-07-17 22:49:46 +02:00
|
|
|
size > get_max_sticker_file_size(sticker_format, StickerType::Regular, false)) {
|
2022-01-25 19:52:15 +01:00
|
|
|
document_type = Document::Type::General;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(DEBUG) << "Receive document with ID = " << id << " of type " << document_type;
|
2018-06-25 23:10:53 +02:00
|
|
|
if (!is_web && !DcId::is_valid(dc_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "Wrong dc_id = " << dc_id;
|
2019-04-09 22:39:41 +02:00
|
|
|
return {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 17:00:51 +02:00
|
|
|
file_name = strip_empty_characters(file_name, 255, true);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto suggested_file_name = file_name;
|
|
|
|
if (suggested_file_name.empty()) {
|
2018-03-08 21:12:31 +01:00
|
|
|
suggested_file_name = to_string(static_cast<uint64>(id));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto extension = MimeType::to_extension(mime_type, default_extension);
|
|
|
|
if (!extension.empty()) {
|
2018-01-28 19:28:17 +01:00
|
|
|
suggested_file_name += '.';
|
|
|
|
suggested_file_name += extension;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-03 22:33:26 +01:00
|
|
|
|
|
|
|
FileId file_id;
|
|
|
|
if (!is_web) {
|
|
|
|
file_id = td_->file_manager_->register_remote(
|
2019-06-17 15:46:20 +02:00
|
|
|
FullRemoteFileLocation(file_type, id, access_hash, DcId::internal(dc_id), std::move(file_reference)), source,
|
|
|
|
owner_dialog_id, size, 0, suggested_file_name);
|
2018-03-03 22:33:26 +01:00
|
|
|
if (!encryption_key.empty()) {
|
|
|
|
td_->file_manager_->set_encryption_key(file_id, std::move(encryption_key));
|
|
|
|
}
|
|
|
|
} else if (!is_web_no_proxy) {
|
2019-06-17 15:46:20 +02:00
|
|
|
file_id = td_->file_manager_->register_remote(FullRemoteFileLocation(file_type, url, access_hash), source,
|
|
|
|
owner_dialog_id, 0, size, file_name);
|
2018-03-03 22:33:26 +01:00
|
|
|
} else {
|
|
|
|
auto r_file_id = td_->file_manager_->from_persistent_id(url, file_type);
|
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't register URL: " << r_file_id.error();
|
2019-04-09 22:39:41 +02:00
|
|
|
return {};
|
2018-03-03 22:33:26 +01:00
|
|
|
}
|
|
|
|
file_id = r_file_id.move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dimensions.width != 0 && thumbnail.dimensions.width != 0) {
|
|
|
|
if ((thumbnail.dimensions.width < thumbnail.dimensions.height && dimensions.width > dimensions.height) ||
|
|
|
|
(thumbnail.dimensions.width > thumbnail.dimensions.height && dimensions.width < dimensions.height)) {
|
|
|
|
// fix for wrong dimensions specified by the Android application
|
|
|
|
std::swap(dimensions.width, dimensions.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (document_type) {
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::Animation:
|
2020-05-30 00:48:56 +02:00
|
|
|
td_->animations_manager_->create_animation(
|
|
|
|
file_id, std::move(minithumbnail), std::move(thumbnail), std::move(animated_thumbnail), has_stickers,
|
|
|
|
vector<FileId>(), std::move(file_name), std::move(mime_type), video_duration, dimensions, !is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::Audio: {
|
2018-04-04 11:08:31 +02:00
|
|
|
int32 duration = 0;
|
|
|
|
string title;
|
|
|
|
string performer;
|
|
|
|
if (audio != nullptr) {
|
|
|
|
duration = audio->duration_;
|
|
|
|
title = std::move(audio->title_);
|
|
|
|
performer = std::move(audio->performer_);
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
td_->audios_manager_->create_audio(file_id, std::move(minithumbnail), std::move(thumbnail), std::move(file_name),
|
2022-04-15 14:30:51 +02:00
|
|
|
std::move(mime_type), duration, std::move(title), std::move(performer), date,
|
2019-03-01 20:51:33 +01:00
|
|
|
!is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2018-04-04 11:08:31 +02:00
|
|
|
}
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::General:
|
2019-03-01 20:51:33 +01:00
|
|
|
create_document(file_id, std::move(minithumbnail), std::move(thumbnail), std::move(file_name),
|
|
|
|
std::move(mime_type), !is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::Sticker:
|
2021-03-03 14:33:17 +01:00
|
|
|
if (thumbnail_format == PhotoFormat::Jpeg) {
|
|
|
|
minithumbnail = string();
|
|
|
|
}
|
2022-04-26 15:01:29 +02:00
|
|
|
td_->stickers_manager_->create_sticker(file_id, premium_animation_file_id, std::move(minithumbnail),
|
2022-07-14 16:11:36 +02:00
|
|
|
std::move(thumbnail), dimensions, std::move(sticker),
|
|
|
|
std::move(custom_emoji), sticker_format, load_data_multipromise_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::Video:
|
2020-05-30 00:48:56 +02:00
|
|
|
td_->videos_manager_->create_video(file_id, std::move(minithumbnail), std::move(thumbnail),
|
|
|
|
std::move(animated_thumbnail), has_stickers, vector<FileId>(),
|
|
|
|
std::move(file_name), std::move(mime_type), video_duration, dimensions,
|
2023-05-19 13:57:57 +02:00
|
|
|
supports_streaming, video_preload_prefix_size, !is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::VideoNote:
|
2019-03-01 20:51:33 +01:00
|
|
|
td_->video_notes_manager_->create_video_note(file_id, std::move(minithumbnail), std::move(thumbnail),
|
2022-10-20 22:23:40 +02:00
|
|
|
video_duration, dimensions, std::move(video_waveform), !is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::VoiceNote: {
|
2018-04-04 11:08:31 +02:00
|
|
|
int32 duration = 0;
|
|
|
|
string waveform;
|
|
|
|
if (audio != nullptr) {
|
|
|
|
duration = audio->duration_;
|
|
|
|
waveform = audio->waveform_.as_slice().str();
|
|
|
|
}
|
|
|
|
td_->voice_notes_manager_->create_voice_note(file_id, std::move(mime_type), duration, std::move(waveform),
|
|
|
|
!is_web);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2018-04-04 11:08:31 +02:00
|
|
|
}
|
2019-04-09 17:38:57 +02:00
|
|
|
case Document::Type::Unknown:
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return {document_type, file_id};
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:38:57 +02:00
|
|
|
FileId DocumentsManager::on_get_document(unique_ptr<GeneralDocument> new_document, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_document->file_id;
|
2019-08-01 02:58:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2018-01-05 16:50:35 +01:00
|
|
|
LOG(INFO) << "Receive document " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &d = documents_[new_document->file_id];
|
|
|
|
if (d == nullptr) {
|
|
|
|
d = std::move(new_document);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(d->file_id == new_document->file_id);
|
|
|
|
if (d->mime_type != new_document->mime_type) {
|
|
|
|
LOG(DEBUG) << "Document " << file_id << " mime_type has changed";
|
2022-10-20 00:16:24 +02:00
|
|
|
d->mime_type = std::move(new_document->mime_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (d->file_name != new_document->file_name) {
|
|
|
|
LOG(DEBUG) << "Document " << file_id << " file_name has changed";
|
2022-10-20 00:16:24 +02:00
|
|
|
d->file_name = std::move(new_document->file_name);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (d->minithumbnail != new_document->minithumbnail) {
|
|
|
|
d->minithumbnail = std::move(new_document->minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (d->thumbnail != new_document->thumbnail) {
|
|
|
|
if (!d->thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Document " << file_id << " thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Document " << file_id << " thumbnail has changed from " << d->thumbnail << " to "
|
|
|
|
<< new_document->thumbnail;
|
|
|
|
}
|
2022-10-20 00:16:24 +02:00
|
|
|
d->thumbnail = std::move(new_document->thumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
void DocumentsManager::create_document(FileId file_id, string minithumbnail, PhotoSize thumbnail, string file_name,
|
|
|
|
string mime_type, bool replace) {
|
2019-04-09 17:38:57 +02:00
|
|
|
auto d = make_unique<GeneralDocument>();
|
2018-12-31 20:04:05 +01:00
|
|
|
d->file_id = file_id;
|
|
|
|
d->file_name = std::move(file_name);
|
|
|
|
d->mime_type = std::move(mime_type);
|
2021-05-27 20:15:30 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
d->minithumbnail = std::move(minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
d->thumbnail = std::move(thumbnail);
|
|
|
|
on_get_document(std::move(d), replace);
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:38:57 +02:00
|
|
|
const DocumentsManager::GeneralDocument *DocumentsManager::get_document(FileId file_id) const {
|
2022-08-04 09:50:34 +02:00
|
|
|
return documents_.get_pointer(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 10:18:40 +01:00
|
|
|
bool DocumentsManager::has_input_media(FileId file_id, FileId thumbnail_file_id, bool is_secret) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (is_secret) {
|
2018-06-08 21:23:49 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty() || !file_view.has_remote_location()) {
|
2018-01-31 10:18:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !thumbnail_file_id.is_valid();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-03 14:44:33 +02:00
|
|
|
if (td_->auth_manager_->is_bot() && file_view.has_remote_location()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-10 00:39:58 +01:00
|
|
|
// having remote location is not enough to have InputMedia, because the file may not have valid file_reference
|
|
|
|
// also file_id needs to be duped, because upload can be called to repair the file_reference and every upload
|
|
|
|
// request must have unique file_id
|
|
|
|
return /* file_view.has_remote_location() || */ file_view.has_url();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia DocumentsManager::get_secret_input_media(FileId document_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
2022-05-11 06:46:06 +02:00
|
|
|
const string &caption, BufferSlice thumbnail,
|
|
|
|
int32 layer) const {
|
2019-04-09 17:38:57 +02:00
|
|
|
const GeneralDocument *document = get_document(document_file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(document != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(document_file_id);
|
2022-05-11 00:53:18 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!input_file) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (document->thumbnail.file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
2021-10-19 17:11:16 +02:00
|
|
|
if (!document->file_name.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeFilename>(document->file_name));
|
|
|
|
}
|
2022-05-11 00:53:18 +02:00
|
|
|
return {std::move(input_file),
|
|
|
|
std::move(thumbnail),
|
|
|
|
document->thumbnail.dimensions,
|
|
|
|
document->mime_type,
|
|
|
|
file_view,
|
|
|
|
std::move(attributes),
|
2022-05-11 06:46:06 +02:00
|
|
|
caption,
|
|
|
|
layer};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> DocumentsManager::get_input_media(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
|
2018-01-30 18:06:54 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2022-12-08 18:58:37 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(
|
|
|
|
0, false /*ignored*/, file_view.main_remote_location().as_input_document(), 0, string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2022-12-08 18:58:37 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, false /*ignored*/, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
2019-04-09 17:38:57 +02:00
|
|
|
const GeneralDocument *document = get_document(file_id);
|
2019-01-25 02:38:11 +01:00
|
|
|
CHECK(document != nullptr);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
2021-10-19 17:11:16 +02:00
|
|
|
if (!document->file_name.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(document->file_name));
|
|
|
|
}
|
|
|
|
int32 flags = 0;
|
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
2022-12-15 20:41:55 +01:00
|
|
|
auto file_type = file_view.get_type();
|
|
|
|
if (file_type == FileType::DocumentAsFile) {
|
2020-06-22 01:02:21 +02:00
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::FORCE_FILE_MASK;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2022-12-08 18:58:37 +01:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, std::move(input_file),
|
|
|
|
std::move(input_thumbnail), document->mime_type, std::move(attributes),
|
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
|
2019-02-12 02:50:30 +01:00
|
|
|
} else {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId DocumentsManager::get_document_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto document = get_document(file_id);
|
|
|
|
CHECK(document != nullptr);
|
|
|
|
return document->thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentsManager::delete_document_thumbnail(FileId file_id) {
|
|
|
|
auto &document = documents_[file_id];
|
|
|
|
CHECK(document != nullptr);
|
|
|
|
document->thumbnail = PhotoSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId DocumentsManager::dup_document(FileId new_id, FileId old_id) {
|
2019-04-09 17:38:57 +02:00
|
|
|
const GeneralDocument *old_document = get_document(old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(old_document != nullptr);
|
|
|
|
auto &new_document = documents_[new_id];
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(new_document == nullptr);
|
2019-04-09 17:38:57 +02:00
|
|
|
new_document = make_unique<GeneralDocument>(*old_document);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_document->file_id = new_id;
|
2022-11-21 09:40:43 +01:00
|
|
|
new_document->thumbnail.file_id = td_->file_manager_->dup_file_id(new_document->thumbnail.file_id, "dup_document");
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:38:03 +02:00
|
|
|
void DocumentsManager::merge_documents(FileId new_id, FileId old_id) {
|
2021-08-26 17:50:28 +02:00
|
|
|
CHECK(old_id.is_valid() && new_id.is_valid());
|
|
|
|
CHECK(new_id != old_id);
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge documents " << new_id << " and " << old_id;
|
2019-04-09 17:38:57 +02:00
|
|
|
const GeneralDocument *old_ = get_document(old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
|
2022-08-03 20:58:07 +02:00
|
|
|
const auto *new_ = get_document(new_id);
|
|
|
|
if (new_ == nullptr) {
|
2022-08-03 20:38:03 +02:00
|
|
|
dup_document(new_id, old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (old_->thumbnail != new_->thumbnail) {
|
2019-02-10 00:39:58 +01:00
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->thumbnail.file_id, old_->thumbnail.file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
string DocumentsManager::get_document_search_text(FileId file_id) const {
|
|
|
|
auto document = get_document(file_id);
|
|
|
|
CHECK(document);
|
|
|
|
if (document->file_name.size() > 32) {
|
|
|
|
return document->file_name;
|
|
|
|
}
|
|
|
|
auto buf = StackAllocator::alloc(256);
|
|
|
|
StringBuilder sb(buf.as_slice());
|
|
|
|
auto stem = PathView(document->file_name).file_stem();
|
|
|
|
sb << document->file_name;
|
|
|
|
for (size_t i = 1; i + 1 < stem.size(); i++) {
|
|
|
|
if (is_utf8_character_first_code_unit(stem[i])) {
|
|
|
|
sb << " " << stem.substr(0, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sb.is_error()) {
|
|
|
|
return document->file_name;
|
|
|
|
}
|
|
|
|
return sb.as_cslice().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|