diff --git a/td/telegram/Photo.cpp b/td/telegram/Photo.cpp index 8c0dd00d6..83eb3740d 100644 --- a/td/telegram/Photo.cpp +++ b/td/telegram/Photo.cpp @@ -328,7 +328,7 @@ Variant get_photo_size(FileManager *file_manager, PhotoSizeSo res.type = 0; LOG(ERROR) << "Wrong photoSize \"" << type << "\" " << res; } else { - res.type = static_cast(type[0]); + res.type = static_cast(type[0]); } if (source.type == PhotoSizeSource::Type::Thumbnail) { source.thumbnail().thumbnail_type = res.type; diff --git a/td/telegram/Photo.h b/td/telegram/Photo.h index 2fb24ed3b..65b9cc410 100644 --- a/td/telegram/Photo.h +++ b/td/telegram/Photo.h @@ -50,22 +50,27 @@ struct PhotoSize { struct OfflineInputStickerSet { int64 sticker_set_id = 0; int64 sticker_set_access_hash = 0; + OfflineInputStickerSet() = default; OfflineInputStickerSet(int64 sticker_set_id, int64 sticker_set_access_hash) : sticker_set_id(sticker_set_id), sticker_set_access_hash(sticker_set_access_hash) { } + tl_object_ptr as_telegram_api() const { return make_tl_object(sticker_set_id, sticker_set_access_hash); } + template void store(StorerT &storer) const; template void parse(ParserT &parser); + struct AsKey { const OfflineInputStickerSet &key; template void store(StorerT &storer) const; }; + AsKey as_key() const { return AsKey{*this}; } @@ -74,10 +79,12 @@ struct OfflineInputStickerSet { struct OfflineInputPeer { DialogId dialog_id; int64 dialog_access_hash = 0; + OfflineInputPeer() = default; OfflineInputPeer(DialogId dialog_id, int64 dialog_access_hash) : dialog_id(dialog_id), dialog_access_hash(dialog_access_hash) { } + tl_object_ptr as_telegram_api() const { switch (dialog_id.get_type()) { case DialogType::User: { @@ -99,18 +106,20 @@ struct OfflineInputPeer { default: UNREACHABLE(); return nullptr; - } // namespace td + } } template void store(StorerT &storer) const; template void parse(ParserT &parser); + struct AsKey { const OfflineInputPeer &key; template void store(StorerT &storer) const; }; + AsKey as_key() const { return AsKey{*this}; } @@ -124,7 +133,7 @@ struct PhotoSizeSource { // for photos, document thumbnails, encrypted thumbnails struct Thumbnail { Thumbnail() = default; - Thumbnail(int32 thumbnail_type) : thumbnail_type(thumbnail_type) { + explicit Thumbnail(int32 thumbnail_type) : thumbnail_type(thumbnail_type) { } int32 thumbnail_type = 0; }; @@ -180,11 +189,13 @@ struct PhotoSizeSource { void store(StorerT &storer) const; template void parse(ParserT &parser); + struct AsKey { const PhotoSizeSource &key; template void store(StorerT &storer) const; }; + AsKey as_key() const { return AsKey{*this}; } diff --git a/td/telegram/Photo.hpp b/td/telegram/Photo.hpp index 96b5f0f19..4c47a52c5 100644 --- a/td/telegram/Photo.hpp +++ b/td/telegram/Photo.hpp @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/DialogId.h" #include "td/telegram/Photo.h" #include "td/telegram/files/FileId.hpp" @@ -13,13 +14,17 @@ #include "td/utils/logging.h" #include "td/utils/tl_helpers.h" +#include + namespace td { + template void OfflineInputStickerSet::store(StorerT &storer) const { using td::store; store(sticker_set_id, storer); store(sticker_set_access_hash, storer); } + template void OfflineInputStickerSet::parse(ParserT &parser) { using td::parse; @@ -39,11 +44,18 @@ void OfflineInputPeer::store(StorerT &storer) const { store(dialog_id, storer); store(dialog_access_hash, storer); } + template void OfflineInputPeer::parse(ParserT &parser) { using td::parse; parse(dialog_id, parser); parse(dialog_access_hash, parser); + switch (dialog_id.get_type()) { + case DialogType::SecretChat: + case DialogType::None: + parser.set_error("Invalid chat id"); + break; + } } template @@ -78,6 +90,7 @@ void PhotoSizeSource::store(StorerT &storer) const { break; } } + template void PhotoSizeSource::parse(ParserT &parser) { using td::parse; @@ -100,6 +113,9 @@ void PhotoSizeSource::parse(ParserT &parser) { case Type::Thumbnail: { Thumbnail thumbnail; parse(thumbnail.thumbnail_type, parser); + if (thumbnail.thumbnail_type < 0 || thumbnail.thumbnail_type > std::numeric_limits::max()) { + parser.set_error("Wrong thumbnail type"); + } variant = thumbnail; break; } diff --git a/td/telegram/Version.h b/td/telegram/Version.h index 5f639dc9a..91eb8bb59 100644 --- a/td/telegram/Version.h +++ b/td/telegram/Version.h @@ -31,7 +31,7 @@ enum class Version : int32 { AddNotificationGroupInfoMaxRemovedMessageId, SupportMinithumbnails, AddVideoCallsSupport, - PhotoSizeSource, + AddPhotoSizeSource, Next }; diff --git a/td/telegram/files/FileDb.cpp b/td/telegram/files/FileDb.cpp index 62edc4b1c..9a2794f7d 100644 --- a/td/telegram/files/FileDb.cpp +++ b/td/telegram/files/FileDb.cpp @@ -10,8 +10,8 @@ #include "td/telegram/files/FileData.hpp" #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileLocation.hpp" -#include "td/telegram/Version.h" #include "td/telegram/logevent/LogEvent.h" // WithVersion +#include "td/telegram/Version.h" #include "td/actor/actor.h" diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index ad06dac7d..0974b51aa 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -487,25 +487,22 @@ class FullRemoteFileLocation { case FileType::Photo: return make_tl_object( photo().id_, photo().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()), - std::string(1, thumbnail.thumbnail_type)); + std::string(1, static_cast(narrow_cast(thumbnail.thumbnail_type)))); case FileType::Thumbnail: return make_tl_object( photo().id_, photo().access_hash_, BufferSlice(FileReferenceView(file_reference_).download()), - std::string(1, thumbnail.thumbnail_type)); + std::string(1, static_cast(narrow_cast(thumbnail.thumbnail_type)))); default: - case FileType::EncryptedThumbnail: UNREACHABLE(); } } case PhotoSizeSource::Type::DialogPhoto: { - LOG(ERROR) << "DIALOG PHOTO"; auto &dialog_photo = photo().source_.dialog_photo(); return make_tl_object( dialog_photo.is_big * telegram_api::inputPeerPhotoFileLocation::Flags::BIG_MASK, dialog_photo.is_big, dialog_photo.input_peer.as_telegram_api(), photo().volume_id_, photo().local_id_); } case PhotoSizeSource::Type::StickerSetThumbnail: { - LOG(ERROR) << "StickerSetThumbnail"; auto &sticker_set_thumbnail = photo().source_.sticker_set_thumbnail(); return make_tl_object( sticker_set_thumbnail.input_sticker_set.as_telegram_api(), photo().volume_id_, photo().local_id_); diff --git a/td/telegram/files/FileLocation.hpp b/td/telegram/files/FileLocation.hpp index 06d6d2a6f..d118837fd 100644 --- a/td/telegram/files/FileLocation.hpp +++ b/td/telegram/files/FileLocation.hpp @@ -9,14 +9,14 @@ #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileType.h" +#include "td/telegram/net/DcId.h" +#include "td/telegram/Photo.hpp" #include "td/telegram/Version.h" #include "td/utils/common.h" #include "td/utils/tl_helpers.h" #include "td/utils/Variant.h" -#include "td/telegram/Photo.hpp" - namespace td { template @@ -58,7 +58,7 @@ void PhotoRemoteFileLocation::parse(ParserT &parser) { parse(volume_id_, parser); parse(secret_, parser); parse(local_id_, parser); - if (parser.version() >= static_cast(Version::PhotoSizeSource)) { + if (parser.version() >= static_cast(Version::AddPhotoSizeSource)) { parse(source_, parser); } } diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index ea9590391..73a10bd9a 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -16,10 +16,10 @@ #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileLocation.hpp" #include "td/telegram/Global.h" +#include "td/telegram/logevent/LogEvent.h" #include "td/telegram/misc.h" #include "td/telegram/SecureStorage.h" #include "td/telegram/TdDb.h" -#include "td/telegram/logevent/LogEvent.h" #include "td/actor/SleepActor.h" diff --git a/td/telegram/files/FileStatsWorker.cpp b/td/telegram/files/FileStatsWorker.cpp index 1873dbb0e..cbd8228c7 100644 --- a/td/telegram/files/FileStatsWorker.cpp +++ b/td/telegram/files/FileStatsWorker.cpp @@ -13,8 +13,8 @@ #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileType.h" #include "td/telegram/Global.h" +#include "td/telegram/logevent/LogEvent.h" #include "td/telegram/TdDb.h" -#include "td/telegram/logevent/LogEvent.h" // WithVersion #include "td/db/SqliteKeyValue.h" @@ -47,7 +47,6 @@ struct DbFileInfo { int64 size; }; -// long and blocking template void scan_db(CancellationToken &token, CallbackT &&callback) { G()->td_db()->get_file_db_shared()->pmc().get_by_range("file0", "file:", [&](Slice key, Slice value) { @@ -99,7 +98,6 @@ struct FsFileInfo { uint64 mtime_nsec; }; -// long and blocking template void scan_fs(CancellationToken &token, CallbackT &&callback) { for (int32 i = 0; i < file_type_size; i++) { @@ -108,33 +106,35 @@ void scan_fs(CancellationToken &token, CallbackT &&callback) { continue; } auto files_dir = get_files_dir(file_type); - td::walk_path(files_dir, [&](CSlice path, WalkPath::Type type) { - if (token) { - return WalkPath::Action::Abort; - } - if (type != WalkPath::Type::NotDir) { - return WalkPath::Action::Continue; - } - auto r_stat = stat(path); - if (r_stat.is_error()) { - LOG(WARNING) << "Stat in files gc failed: " << r_stat.error(); - return WalkPath::Action::Continue; - } - auto stat = r_stat.move_as_ok(); - if (ends_with(path, "/.nomedia") && stat.size_ == 0) { - // skip .nomedia file - return WalkPath::Action::Continue; - } + td::walk_path(files_dir, + [&](CSlice path, WalkPath::Type type) { + if (token) { + return WalkPath::Action::Abort; + } + if (type != WalkPath::Type::NotDir) { + return WalkPath::Action::Continue; + } + auto r_stat = stat(path); + if (r_stat.is_error()) { + LOG(WARNING) << "Stat in files gc failed: " << r_stat.error(); + return WalkPath::Action::Continue; + } + auto stat = r_stat.move_as_ok(); + if (ends_with(path, "/.nomedia") && stat.size_ == 0) { + // skip .nomedia file + return WalkPath::Action::Continue; + } - FsFileInfo info; - info.path = path.str(); - info.size = stat.size_; - info.file_type = file_type; - info.atime_nsec = stat.atime_nsec_; - info.mtime_nsec = stat.mtime_nsec_; - callback(info); - return WalkPath::Action::Continue; - }).ignore(); + FsFileInfo info; + info.path = path.str(); + info.size = stat.size_; + info.file_type = file_type; + info.atime_nsec = stat.atime_nsec_; + info.mtime_nsec = stat.mtime_nsec_; + callback(info); + return WalkPath::Action::Continue; + }) + .ignore(); } } } // namespace