Move is_document_file_type to FileType.cpp.

This commit is contained in:
levlam 2022-04-21 21:54:33 +03:00
parent 42911eae75
commit 7fcd92a622
5 changed files with 44 additions and 51 deletions

View File

@ -2435,36 +2435,30 @@ tl_object_ptr<telegram_api::InputMedia> get_fake_input_media(Td *td, tl_object_p
FileId file_id) {
FileView file_view = td->file_manager_->get_file_view(file_id);
auto file_type = file_view.get_type();
switch (file_type) {
case FileType::Animation:
case FileType::Audio:
case FileType::Document:
case FileType::Sticker:
case FileType::Video:
case FileType::VoiceNote: {
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
auto file_path = file_view.suggested_path();
const PathView path_view(file_path);
Slice file_name = path_view.file_name();
if (!file_name.empty()) {
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(file_name.str()));
}
string mime_type = MimeType::from_extension(path_view.extension());
int32 flags = 0;
if (file_type == FileType::Video) {
flags |= telegram_api::inputMediaUploadedDocument::NOSOUND_VIDEO_MASK;
}
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
flags, false /*ignored*/, false /*ignored*/, std::move(input_file), nullptr, mime_type, std::move(attributes),
vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
if (is_document_file_type(file_type)) {
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
auto file_path = file_view.suggested_path();
const PathView path_view(file_path);
Slice file_name = path_view.file_name();
if (!file_name.empty()) {
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(file_name.str()));
}
case FileType::Photo:
return make_tl_object<telegram_api::inputMediaUploadedPhoto>(
0, std::move(input_file), vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
default:
UNREACHABLE();
string mime_type = MimeType::from_extension(path_view.extension());
int32 flags = 0;
if (file_type == FileType::Video) {
flags |= telegram_api::inputMediaUploadedDocument::NOSOUND_VIDEO_MASK;
}
if (file_type == FileType::DocumentAsFile) {
flags |= telegram_api::inputMediaUploadedDocument::FORCE_FILE_MASK;
}
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
flags, false /*ignored*/, false /*ignored*/, std::move(input_file), nullptr, mime_type, std::move(attributes),
vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
} else {
CHECK(file_type == FileType::Photo);
return make_tl_object<telegram_api::inputMediaUploadedPhoto>(
0, std::move(input_file), vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
}
return nullptr;
}
void delete_message_content_thumbnail(MessageContent *content, Td *td) {

View File

@ -35613,21 +35613,6 @@ bool MessagesManager::update_message_content(DialogId dialog_id, Message *old_me
old_file_view.size() == new_file_view.size()) {
auto old_file_type = old_file_view.get_type();
auto new_file_type = new_file_view.get_type();
auto is_document_file_type = [](FileType file_type) {
switch (file_type) {
case FileType::Animation:
case FileType::Audio:
case FileType::Document:
case FileType::DocumentAsFile:
case FileType::Sticker:
case FileType::Video:
case FileType::VideoNote:
case FileType::VoiceNote:
return true;
default:
return false;
}
};
if (is_document_file_type(old_file_type) && is_document_file_type(new_file_type)) {
auto &old_location = old_file_view.local_location();

View File

@ -2883,12 +2883,6 @@ void FileManager::cancel_upload(FileId file_id) {
return resume_upload(file_id, std::vector<int>(), nullptr, 0, 0);
}
static bool is_document_type(FileType type) {
return type == FileType::Document || type == FileType::Sticker || type == FileType::Audio ||
type == FileType::Animation || type == FileType::VoiceNote || type == FileType::Background ||
type == FileType::DocumentAsFile || type == FileType::Ringtone;
}
static bool is_background_type(FileType type) {
return type == FileType::Wallpaper || type == FileType::Background;
}
@ -2962,7 +2956,7 @@ Result<FileId> FileManager::from_persistent_id_v23(Slice binary, FileType file_t
return Status::Error(400, "Wrong remote file identifier specified: can't unserialize it");
}
auto &real_file_type = remote_location.file_type_;
if (is_document_type(real_file_type) && is_document_type(file_type)) {
if (is_document_file_type(real_file_type) && is_document_file_type(file_type)) {
real_file_type = file_type;
} else if (is_background_type(real_file_type) && is_background_type(file_type)) {
// type of file matches, but real type is in the stored remote location
@ -3082,7 +3076,7 @@ Result<FileId> FileManager::check_input_file_id(FileType type, Result<FileId> re
LOG(INFO) << "Checking file " << file_id << " of type " << type << "/" << real_type;
if (!is_encrypted && !is_secure) {
if (real_type != type && !(real_type == FileType::Temp && file_view.has_url()) &&
!(is_document_type(real_type) && is_document_type(type)) &&
!(is_document_file_type(real_type) && is_document_file_type(type)) &&
!(is_background_type(real_type) && is_background_type(type)) &&
!(file_view.is_encrypted() && type == FileType::Ringtone)) {
// TODO: send encrypted file to unencrypted chat

View File

@ -160,6 +160,24 @@ CSlice get_file_type_name(FileType file_type) {
}
}
bool is_document_file_type(FileType file_type) {
switch (file_type) {
case FileType::Animation:
case FileType::Audio:
case FileType::Background:
case FileType::Document:
case FileType::DocumentAsFile:
case FileType::Ringtone:
case FileType::Sticker:
case FileType::Video:
case FileType::VideoNote:
case FileType::VoiceNote:
return true;
default:
return false;
}
}
StringBuilder &operator<<(StringBuilder &string_builder, FileType file_type) {
return string_builder << get_file_type_name(file_type);
}

View File

@ -50,6 +50,8 @@ FileType get_main_file_type(FileType file_type);
CSlice get_file_type_name(FileType file_type);
bool is_document_file_type(FileType file_type);
StringBuilder &operator<<(StringBuilder &string_builder, FileType file_type);
FileDirType get_file_dir_type(FileType file_type);