2020-06-22 01:20:48 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2020-06-22 01:20:48 +02: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/files/FileType.h"
|
|
|
|
|
2023-09-11 15:34:00 +02:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/PathView.h"
|
|
|
|
|
2020-06-22 01:20:48 +02:00
|
|
|
namespace td {
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
FileType get_file_type(const td_api::FileType &file_type) {
|
2020-06-22 01:20:48 +02:00
|
|
|
switch (file_type.get_id()) {
|
|
|
|
case td_api::fileTypeThumbnail::ID:
|
|
|
|
return FileType::Thumbnail;
|
|
|
|
case td_api::fileTypeProfilePhoto::ID:
|
|
|
|
return FileType::ProfilePhoto;
|
|
|
|
case td_api::fileTypePhoto::ID:
|
|
|
|
return FileType::Photo;
|
2023-07-16 12:03:25 +02:00
|
|
|
case td_api::fileTypePhotoStory::ID:
|
|
|
|
return FileType::PhotoStory;
|
2020-06-22 01:20:48 +02:00
|
|
|
case td_api::fileTypeVoiceNote::ID:
|
|
|
|
return FileType::VoiceNote;
|
|
|
|
case td_api::fileTypeVideo::ID:
|
|
|
|
return FileType::Video;
|
2023-07-16 12:03:25 +02:00
|
|
|
case td_api::fileTypeVideoStory::ID:
|
|
|
|
return FileType::VideoStory;
|
2020-06-22 01:20:48 +02:00
|
|
|
case td_api::fileTypeDocument::ID:
|
|
|
|
return FileType::Document;
|
|
|
|
case td_api::fileTypeSecret::ID:
|
|
|
|
return FileType::Encrypted;
|
|
|
|
case td_api::fileTypeUnknown::ID:
|
|
|
|
return FileType::Temp;
|
|
|
|
case td_api::fileTypeSticker::ID:
|
|
|
|
return FileType::Sticker;
|
|
|
|
case td_api::fileTypeAudio::ID:
|
|
|
|
return FileType::Audio;
|
|
|
|
case td_api::fileTypeAnimation::ID:
|
|
|
|
return FileType::Animation;
|
|
|
|
case td_api::fileTypeSecretThumbnail::ID:
|
|
|
|
return FileType::EncryptedThumbnail;
|
|
|
|
case td_api::fileTypeWallpaper::ID:
|
|
|
|
return FileType::Background;
|
|
|
|
case td_api::fileTypeVideoNote::ID:
|
|
|
|
return FileType::VideoNote;
|
|
|
|
case td_api::fileTypeSecure::ID:
|
2022-04-25 18:00:40 +02:00
|
|
|
return FileType::SecureEncrypted;
|
2022-04-12 21:50:20 +02:00
|
|
|
case td_api::fileTypeNotificationSound::ID:
|
|
|
|
return FileType::Ringtone;
|
2020-06-22 01:20:48 +02:00
|
|
|
case td_api::fileTypeNone::ID:
|
|
|
|
return FileType::None;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return FileType::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::FileType> get_file_type_object(FileType file_type) {
|
2020-06-22 01:20:48 +02:00
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
return make_tl_object<td_api::fileTypeThumbnail>();
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
return make_tl_object<td_api::fileTypeProfilePhoto>();
|
|
|
|
case FileType::Photo:
|
|
|
|
return make_tl_object<td_api::fileTypePhoto>();
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
return make_tl_object<td_api::fileTypeVoiceNote>();
|
|
|
|
case FileType::Video:
|
|
|
|
return make_tl_object<td_api::fileTypeVideo>();
|
|
|
|
case FileType::Document:
|
|
|
|
return make_tl_object<td_api::fileTypeDocument>();
|
|
|
|
case FileType::Encrypted:
|
|
|
|
return make_tl_object<td_api::fileTypeSecret>();
|
|
|
|
case FileType::Temp:
|
|
|
|
return make_tl_object<td_api::fileTypeUnknown>();
|
|
|
|
case FileType::Sticker:
|
|
|
|
return make_tl_object<td_api::fileTypeSticker>();
|
|
|
|
case FileType::Audio:
|
|
|
|
return make_tl_object<td_api::fileTypeAudio>();
|
|
|
|
case FileType::Animation:
|
|
|
|
return make_tl_object<td_api::fileTypeAnimation>();
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
return make_tl_object<td_api::fileTypeSecretThumbnail>();
|
|
|
|
case FileType::Wallpaper:
|
|
|
|
return make_tl_object<td_api::fileTypeWallpaper>();
|
|
|
|
case FileType::VideoNote:
|
|
|
|
return make_tl_object<td_api::fileTypeVideoNote>();
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureEncrypted:
|
2020-06-22 01:20:48 +02:00
|
|
|
return make_tl_object<td_api::fileTypeSecure>();
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureDecrypted:
|
2020-06-22 01:20:48 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
return make_tl_object<td_api::fileTypeSecure>();
|
|
|
|
case FileType::Background:
|
|
|
|
return make_tl_object<td_api::fileTypeWallpaper>();
|
|
|
|
case FileType::DocumentAsFile:
|
|
|
|
return make_tl_object<td_api::fileTypeDocument>();
|
2022-04-12 21:50:20 +02:00
|
|
|
case FileType::Ringtone:
|
|
|
|
return make_tl_object<td_api::fileTypeNotificationSound>();
|
2022-04-21 21:34:26 +02:00
|
|
|
case FileType::CallLog:
|
|
|
|
return make_tl_object<td_api::fileTypeDocument>();
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
2023-07-16 12:03:25 +02:00
|
|
|
return make_tl_object<td_api::fileTypePhotoStory>();
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
2023-07-16 12:03:25 +02:00
|
|
|
return make_tl_object<td_api::fileTypeVideoStory>();
|
2020-06-22 01:20:48 +02:00
|
|
|
case FileType::None:
|
|
|
|
return make_tl_object<td_api::fileTypeNone>();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:43:00 +02:00
|
|
|
FileType get_main_file_type(FileType file_type) {
|
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Wallpaper:
|
|
|
|
return FileType::Background;
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureDecrypted:
|
|
|
|
return FileType::SecureEncrypted;
|
2020-06-22 07:43:00 +02:00
|
|
|
case FileType::DocumentAsFile:
|
|
|
|
return FileType::Document;
|
2022-04-21 21:34:26 +02:00
|
|
|
case FileType::CallLog:
|
|
|
|
return FileType::Document;
|
2020-06-22 07:43:00 +02:00
|
|
|
default:
|
|
|
|
return file_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 01:20:48 +02:00
|
|
|
CSlice get_file_type_name(FileType file_type) {
|
2022-12-15 20:41:55 +01:00
|
|
|
switch (get_main_file_type(file_type)) {
|
2020-06-22 01:20:48 +02:00
|
|
|
case FileType::Thumbnail:
|
|
|
|
return CSlice("thumbnails");
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
return CSlice("profile_photos");
|
|
|
|
case FileType::Photo:
|
|
|
|
return CSlice("photos");
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
return CSlice("voice");
|
|
|
|
case FileType::Video:
|
|
|
|
return CSlice("videos");
|
|
|
|
case FileType::Document:
|
|
|
|
return CSlice("documents");
|
|
|
|
case FileType::Encrypted:
|
|
|
|
return CSlice("secret");
|
|
|
|
case FileType::Temp:
|
|
|
|
return CSlice("temp");
|
|
|
|
case FileType::Sticker:
|
|
|
|
return CSlice("stickers");
|
|
|
|
case FileType::Audio:
|
|
|
|
return CSlice("music");
|
|
|
|
case FileType::Animation:
|
|
|
|
return CSlice("animations");
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
return CSlice("secret_thumbnails");
|
|
|
|
case FileType::VideoNote:
|
|
|
|
return CSlice("video_notes");
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureEncrypted:
|
2020-06-22 01:20:48 +02:00
|
|
|
return CSlice("passport");
|
|
|
|
case FileType::Background:
|
|
|
|
return CSlice("wallpapers");
|
2022-04-12 21:50:20 +02:00
|
|
|
case FileType::Ringtone:
|
|
|
|
return CSlice("notification_sounds");
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
|
|
|
return CSlice("stories");
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
|
|
|
return CSlice("stories");
|
2020-06-22 01:20:48 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return CSlice("none");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 21:14:12 +02:00
|
|
|
FileTypeClass get_file_type_class(FileType file_type) {
|
2022-04-21 20:54:33 +02:00
|
|
|
switch (file_type) {
|
2022-04-21 21:14:12 +02:00
|
|
|
case FileType::Photo:
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
case FileType::Wallpaper:
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
2022-04-21 21:14:12 +02:00
|
|
|
return FileTypeClass::Photo;
|
|
|
|
case FileType::Video:
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
case FileType::Document:
|
|
|
|
case FileType::Sticker:
|
2022-04-21 20:54:33 +02:00
|
|
|
case FileType::Audio:
|
2022-04-21 21:14:12 +02:00
|
|
|
case FileType::Animation:
|
|
|
|
case FileType::VideoNote:
|
2022-04-21 20:54:33 +02:00
|
|
|
case FileType::Background:
|
|
|
|
case FileType::DocumentAsFile:
|
|
|
|
case FileType::Ringtone:
|
2022-04-21 21:34:26 +02:00
|
|
|
case FileType::CallLog:
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
2022-04-21 21:14:12 +02:00
|
|
|
return FileTypeClass::Document;
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureDecrypted:
|
|
|
|
case FileType::SecureEncrypted:
|
2022-04-21 21:14:12 +02:00
|
|
|
return FileTypeClass::Secure;
|
|
|
|
case FileType::Encrypted:
|
|
|
|
return FileTypeClass::Encrypted;
|
|
|
|
case FileType::Temp:
|
|
|
|
return FileTypeClass::Temp;
|
|
|
|
case FileType::None:
|
|
|
|
case FileType::Size:
|
2022-04-21 20:54:33 +02:00
|
|
|
default:
|
2022-04-21 21:14:12 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
return FileTypeClass::Temp;
|
2022-04-21 20:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 21:14:12 +02:00
|
|
|
bool is_document_file_type(FileType file_type) {
|
|
|
|
return get_file_type_class(file_type) == FileTypeClass::Document;
|
|
|
|
}
|
|
|
|
|
2020-06-22 01:20:48 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, FileType file_type) {
|
2022-08-06 23:42:40 +02:00
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
return string_builder << "Thumbnail";
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
return string_builder << "ChatPhoto";
|
|
|
|
case FileType::Photo:
|
|
|
|
return string_builder << "Photo";
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
return string_builder << "VoiceNote";
|
|
|
|
case FileType::Video:
|
|
|
|
return string_builder << "Video";
|
|
|
|
case FileType::Document:
|
|
|
|
return string_builder << "Document";
|
|
|
|
case FileType::Encrypted:
|
|
|
|
return string_builder << "Secret";
|
|
|
|
case FileType::Temp:
|
|
|
|
return string_builder << "Temp";
|
|
|
|
case FileType::Sticker:
|
|
|
|
return string_builder << "Sticker";
|
|
|
|
case FileType::Audio:
|
|
|
|
return string_builder << "Audio";
|
|
|
|
case FileType::Animation:
|
|
|
|
return string_builder << "Animation";
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
return string_builder << "SecretThumbnail";
|
|
|
|
case FileType::Wallpaper:
|
|
|
|
return string_builder << "Wallpaper";
|
|
|
|
case FileType::VideoNote:
|
|
|
|
return string_builder << "VideoNote";
|
|
|
|
case FileType::SecureDecrypted:
|
|
|
|
return string_builder << "Passport";
|
|
|
|
case FileType::SecureEncrypted:
|
|
|
|
return string_builder << "Passport";
|
|
|
|
case FileType::Background:
|
|
|
|
return string_builder << "Background";
|
|
|
|
case FileType::DocumentAsFile:
|
|
|
|
return string_builder << "DocumentAsFile";
|
|
|
|
case FileType::Ringtone:
|
|
|
|
return string_builder << "NotificationSound";
|
|
|
|
case FileType::CallLog:
|
|
|
|
return string_builder << "CallLog";
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
|
|
|
return string_builder << "PhotoStory";
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
|
|
|
return string_builder << "VideoStory";
|
2022-08-06 23:42:40 +02:00
|
|
|
case FileType::Size:
|
|
|
|
case FileType::None:
|
|
|
|
default:
|
|
|
|
return string_builder << "<invalid>";
|
|
|
|
}
|
2020-06-22 01:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileDirType get_file_dir_type(FileType file_type) {
|
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
case FileType::Encrypted:
|
|
|
|
case FileType::Sticker:
|
|
|
|
case FileType::Temp:
|
|
|
|
case FileType::Wallpaper:
|
|
|
|
case FileType::EncryptedThumbnail:
|
2022-04-25 18:00:40 +02:00
|
|
|
case FileType::SecureEncrypted:
|
|
|
|
case FileType::SecureDecrypted:
|
2020-06-22 01:20:48 +02:00
|
|
|
case FileType::Background:
|
2022-04-12 21:50:20 +02:00
|
|
|
case FileType::Ringtone:
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
2020-06-22 01:20:48 +02:00
|
|
|
return FileDirType::Secure;
|
|
|
|
default:
|
|
|
|
return FileDirType::Common;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_file_big(FileType file_type, int64 expected_size) {
|
2022-12-15 20:41:55 +01:00
|
|
|
if (get_file_type_class(file_type) == FileTypeClass::Photo) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-22 01:20:48 +02:00
|
|
|
switch (file_type) {
|
2021-07-29 22:39:07 +02:00
|
|
|
case FileType::VideoNote:
|
2022-04-12 21:50:20 +02:00
|
|
|
case FileType::Ringtone:
|
2022-04-21 21:34:26 +02:00
|
|
|
case FileType::CallLog:
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
2020-06-22 01:20:48 +02:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int64 SMALL_FILE_MAX_SIZE = 10 << 20;
|
|
|
|
return expected_size > SMALL_FILE_MAX_SIZE;
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:28:51 +02:00
|
|
|
bool can_reuse_remote_file(FileType file_type) {
|
|
|
|
switch (file_type) {
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
case FileType::Background:
|
|
|
|
case FileType::CallLog:
|
2023-05-25 00:26:26 +02:00
|
|
|
case FileType::PhotoStory:
|
2023-05-25 13:19:54 +02:00
|
|
|
case FileType::VideoStory:
|
2022-04-22 13:28:51 +02:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:34:00 +02:00
|
|
|
FileType guess_file_type_by_path(Slice file_path, FileType default_file_type) {
|
|
|
|
if (default_file_type != FileType::None) {
|
2023-09-11 15:38:41 +02:00
|
|
|
if (default_file_type == FileType::PhotoStory && ends_with(file_path, ".mp4")) {
|
|
|
|
return FileType::VideoStory;
|
|
|
|
}
|
2023-09-11 15:34:00 +02:00
|
|
|
return default_file_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
PathView path_view(file_path);
|
|
|
|
auto file_name = path_view.file_name();
|
|
|
|
auto extension = path_view.extension();
|
|
|
|
if (extension == "jpg" || extension == "jpeg") {
|
|
|
|
return FileType::Photo;
|
|
|
|
}
|
|
|
|
if (extension == "ogg" || extension == "oga" || extension == "opus") {
|
|
|
|
return FileType::VoiceNote;
|
|
|
|
}
|
|
|
|
if (extension == "3gp" || extension == "mov") {
|
|
|
|
return FileType::Video;
|
|
|
|
}
|
|
|
|
if (extension == "mp3" || extension == "mpeg3" || extension == "m4a") {
|
|
|
|
return FileType::Audio;
|
|
|
|
}
|
|
|
|
if (extension == "webp" || extension == "tgs" || extension == "webm") {
|
|
|
|
return FileType::Sticker;
|
|
|
|
}
|
|
|
|
if (extension == "gif") {
|
|
|
|
return FileType::Animation;
|
|
|
|
}
|
|
|
|
if (extension == "mp4" || extension == "mpeg4") {
|
|
|
|
return to_lower(file_name).find("-gif-") != string::npos ? FileType::Animation : FileType::Video;
|
|
|
|
}
|
|
|
|
return FileType::Document;
|
|
|
|
}
|
|
|
|
|
2020-06-22 01:20:48 +02:00
|
|
|
} // namespace td
|