From 55f60866cb221d5bfc9f9ed1dc66f0c6e58d2257 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 29 Aug 2018 00:09:55 +0300 Subject: [PATCH] Set correct cache tags for different file types. --- Telegram/SourceFiles/data/data_document.cpp | 22 ++++++- Telegram/SourceFiles/data/data_document.h | 1 + .../SourceFiles/data/data_media_types.cpp | 4 +- Telegram/SourceFiles/data/data_types.h | 6 ++ .../passport/passport_form_controller.cpp | 11 ++-- .../SourceFiles/storage/file_download.cpp | 58 +++++++++++++++---- Telegram/SourceFiles/storage/file_download.h | 23 ++++++-- Telegram/SourceFiles/storage/file_upload.cpp | 8 ++- Telegram/SourceFiles/ui/images.cpp | 17 ++++-- 9 files changed, 119 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index ab59eaeec..c06b67161 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -771,13 +771,15 @@ void DocumentData::save( &_urlLocation, size, fromCloud, - autoLoading); + autoLoading, + cacheTag()); } else if (!_access && !_url.isEmpty()) { _loader = new webFileLoader( _url, toFile, fromCloud, - autoLoading); + autoLoading, + cacheTag()); } else { _loader = new mtpFileLoader( _dc, @@ -790,7 +792,8 @@ void DocumentData::save( size, (saveToCache() ? LoadToCacheAsWell : LoadToFileOnly), fromCloud, - autoLoading); + autoLoading, + cacheTag()); } _loader->connect(_loader, SIGNAL(progress(FileLoader*)), App::main(), SLOT(documentLoadProgress(FileLoader*))); @@ -1098,6 +1101,19 @@ Storage::Cache::Key DocumentData::cacheKey() const { } } +uint8 DocumentData::cacheTag() const { + if (type == StickerDocument) { + return Data::kStickerCacheTag; + } else if (isVoiceMessage()) { + return Data::kVoiceMessageCacheTag; + } else if (isVideoMessage()) { + return Data::kVideoMessageCacheTag; + } else if (isAnimation()) { + return Data::kAnimationCacheTag; + } + return 0; +} + QString DocumentData::composeNameString() const { if (auto songData = song()) { return ComposeNameString( diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 1286f38c1..2aaf4951b 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -180,6 +180,7 @@ public: MediaKey mediaKey() const; Storage::Cache::Key cacheKey() const; + uint8 cacheTag() const; static QString ComposeNameString( const QString &filename, diff --git a/Telegram/SourceFiles/data/data_media_types.cpp b/Telegram/SourceFiles/data/data_media_types.cpp index 43a62fdbe..d175b7fed 100644 --- a/Telegram/SourceFiles/data/data_media_types.cpp +++ b/Telegram/SourceFiles/data/data_media_types.cpp @@ -357,7 +357,9 @@ bool MediaPhoto::updateSentMedia(const MTPMessageMedia &media) { } Auth().data().cache().putIfEmpty( Data::StorageCacheKey(key), - image->savedData()); + Storage::Cache::Database::TaggedValue( + image->savedData(), + Data::kImageCacheTag)); }; auto &sizes = photo.c_photo().vsizes.v; auto max = 0; diff --git a/Telegram/SourceFiles/data/data_types.h b/Telegram/SourceFiles/data/data_types.h index f4f4b5493..8aabd7499 100644 --- a/Telegram/SourceFiles/data/data_types.h +++ b/Telegram/SourceFiles/data/data_types.h @@ -41,6 +41,12 @@ Storage::Cache::Key StorageCacheKey(const StorageImageLocation &location); Storage::Cache::Key WebDocumentCacheKey(const WebFileLocation &location); Storage::Cache::Key UrlCacheKey(const QString &location); +constexpr auto kImageCacheTag = uint8(0x01); +constexpr auto kStickerCacheTag = uint8(0x02); +constexpr auto kVoiceMessageCacheTag = uint8(0x03); +constexpr auto kVideoMessageCacheTag = uint8(0x04); +constexpr auto kAnimationCacheTag = uint8(0x05); + } // namespace Data struct MessageGroupId { diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index 91937d2d4..64785e087 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -1732,7 +1732,8 @@ void FormController::loadFile(File &file) { file.size, LoadToCacheAsWell, LoadFromCloudOrLocal, - false)); + false, + Data::kImageCacheTag)); const auto loader = j->second.get(); loader->connect(loader, &mtpFileLoader::progress, [=] { if (loader->finished()) { @@ -2341,9 +2342,11 @@ void FormController::fillDownloadedFile( const auto &bytes = i->uploadData->bytes; Auth().data().cache().put( Data::DocumentCacheKey(destination.dcId, destination.id), - QByteArray( - reinterpret_cast(bytes.data()), - bytes.size())); + Storage::Cache::Database::TaggedValue( + QByteArray( + reinterpret_cast(bytes.data()), + bytes.size()), + Data::kImageCacheTag)); } auto FormController::parseValue( diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 0bdfdb5dd..3735442f5 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -107,9 +107,17 @@ WebLoadMainManager *_webLoadMainManager = nullptr; } // namespace -FileLoader::FileLoader(const QString &toFile, int32 size, LocationType locationType, LoadToCacheSetting toCache, LoadFromCloudSetting fromCloud, bool autoLoading) +FileLoader::FileLoader( + const QString &toFile, + int32 size, + LocationType locationType, + LoadToCacheSetting toCache, + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag) : _downloader(&Auth().downloader()) , _autoLoading(autoLoading) +, _cacheTag(cacheTag) , _filename(toFile) , _file(_filename) , _toCache(toCache) @@ -471,15 +479,17 @@ mtpFileLoader::mtpFileLoader( not_null location, Data::FileOrigin origin, int32 size, - LoadFromCloudSetting fromCloud - , bool autoLoading) + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag) : FileLoader( QString(), size, UnknownFileLocation, LoadToCacheAsWell, fromCloud, - autoLoading) + autoLoading, + cacheTag) , _dcId(location->dc()) , _location(location) , _origin(origin) { @@ -502,14 +512,16 @@ mtpFileLoader::mtpFileLoader( int32 size, LoadToCacheSetting toCache, LoadFromCloudSetting fromCloud, - bool autoLoading) + bool autoLoading, + uint8 cacheTag) : FileLoader( to, size, type, toCache, fromCloud, - autoLoading) + autoLoading, + cacheTag) , _dcId(dc) , _id(id) , _accessHash(accessHash) @@ -527,14 +539,16 @@ mtpFileLoader::mtpFileLoader( const WebFileLocation *location, int32 size, LoadFromCloudSetting fromCloud, - bool autoLoading) + bool autoLoading, + uint8 cacheTag) : FileLoader( QString(), size, UnknownFileLocation, LoadToCacheAsWell, fromCloud, - autoLoading) + autoLoading, + cacheTag) , _dcId(location->dc()) , _urlLocation(location) { auto shiftedDcId = MTP::downloadDcId(_dcId, 0); @@ -957,7 +971,11 @@ bool mtpFileLoader::feedPart(int offset, bytes::const_span buffer) { || _locationType == UnknownFileLocation || _toCache == LoadToCacheAsWell) { if (const auto key = cacheKey()) { - Auth().data().cache().put(*key, base::duplicate(_data)); + Auth().data().cache().put( + *key, + Storage::Cache::Database::TaggedValue( + base::duplicate(_data), + _cacheTag)); } } } @@ -1105,8 +1123,20 @@ mtpFileLoader::~mtpFileLoader() { cancelRequests(); } -webFileLoader::webFileLoader(const QString &url, const QString &to, LoadFromCloudSetting fromCloud, bool autoLoading) -: FileLoader(QString(), 0, UnknownFileLocation, LoadToCacheAsWell, fromCloud, autoLoading) +webFileLoader::webFileLoader( + const QString &url, + const QString &to, + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag) +: FileLoader( + QString(), + 0, + UnknownFileLocation, + LoadToCacheAsWell, + fromCloud, + autoLoading, + cacheTag) , _url(url) , _requestSent(false) , _already(0) { @@ -1167,7 +1197,11 @@ void webFileLoader::onFinished(const QByteArray &data) { if (_localStatus == LocalStatus::NotFound) { if (const auto key = cacheKey()) { - Auth().data().cache().put(*key, base::duplicate(_data)); + Auth().data().cache().put( + *key, + Storage::Cache::Database::TaggedValue( + base::duplicate(_data), + _cacheTag)); } } _downloader->taskFinished().notify(); diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h index 94ac563d1..bf32f16d0 100644 --- a/Telegram/SourceFiles/storage/file_download.h +++ b/Telegram/SourceFiles/storage/file_download.h @@ -72,7 +72,15 @@ class FileLoader : public QObject { Q_OBJECT public: - FileLoader(const QString &toFile, int32 size, LocationType locationType, LoadToCacheSetting, LoadFromCloudSetting fromCloud, bool autoLoading); + FileLoader( + const QString &toFile, + int32 size, + LocationType locationType, + LoadToCacheSetting toCache, + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag); + bool finished() const { return _finished; } @@ -163,6 +171,7 @@ protected: bool _paused = false; bool _autoLoading = false; + uint8 _cacheTag = 0; bool _inQueue = false; bool _finished = false; bool _cancelled = false; @@ -197,7 +206,8 @@ public: Data::FileOrigin origin, int32 size, LoadFromCloudSetting fromCloud, - bool autoLoading); + bool autoLoading, + uint8 cacheTag); mtpFileLoader( int32 dc, uint64 id, @@ -209,12 +219,14 @@ public: int32 size, LoadToCacheSetting toCache, LoadFromCloudSetting fromCloud, - bool autoLoading); + bool autoLoading, + uint8 cacheTag); mtpFileLoader( const WebFileLocation *location, int32 size, LoadFromCloudSetting fromCloud, - bool autoLoading); + bool autoLoading, + uint8 cacheTag); int32 currentOffset(bool includeSkipped = false) const override; Data::FileOrigin fileOrigin() const override; @@ -317,7 +329,8 @@ public: const QString &url, const QString &to, LoadFromCloudSetting fromCloud, - bool autoLoading); + bool autoLoading, + uint8 cacheTag); int32 currentOffset(bool includeSkipped = false) const override; diff --git a/Telegram/SourceFiles/storage/file_upload.cpp b/Telegram/SourceFiles/storage/file_upload.cpp index 215b43dde..b821ffc43 100644 --- a/Telegram/SourceFiles/storage/file_upload.cpp +++ b/Telegram/SourceFiles/storage/file_upload.cpp @@ -129,7 +129,9 @@ void Uploader::uploadMedia(const FullMsgId &msgId, const SendMediaReady &media) if (document->saveToCache()) { Auth().data().cache().put( document->cacheKey(), - base::duplicate(media.data)); + Storage::Cache::Database::TaggedValue( + base::duplicate(media.data), + document->cacheTag())); } } if (!media.file.isEmpty()) { @@ -156,7 +158,9 @@ void Uploader::upload( if (document->saveToCache()) { Auth().data().cache().put( document->cacheKey(), - base::duplicate(file->content)); + Storage::Cache::Database::TaggedValue( + base::duplicate(file->content), + document->cacheTag())); } } if (!file->filepath.isEmpty()) { diff --git a/Telegram/SourceFiles/ui/images.cpp b/Telegram/SourceFiles/ui/images.cpp index 7e78ec1a2..a82506afc 100644 --- a/Telegram/SourceFiles/ui/images.cpp +++ b/Telegram/SourceFiles/ui/images.cpp @@ -1024,7 +1024,9 @@ void RemoteImage::setImageBytes( if (!location.isNull() && !bytes.isEmpty()) { Auth().data().cache().putIfEmpty( Data::StorageCacheKey(location), - base::duplicate(bytes)); + Storage::Cache::Database::TaggedValue( + base::duplicate(bytes), + Data::kImageCacheTag)); } } @@ -1165,7 +1167,8 @@ FileLoader *StorageImage::createLoader( origin, _size, fromCloud, - autoLoading); + autoLoading, + Data::kImageCacheTag); } WebFileImage::WebFileImage( @@ -1220,7 +1223,8 @@ FileLoader *WebFileImage::createLoader( &_location, _size, fromCloud, - autoLoading); + autoLoading, + Data::kImageCacheTag); } DelayedStorageImage::DelayedStorageImage() : StorageImage(StorageImageLocation()) @@ -1359,7 +1363,12 @@ FileLoader *WebImage::createLoader( Data::FileOrigin origin, LoadFromCloudSetting fromCloud, bool autoLoading) { - return new webFileLoader(_url, QString(), fromCloud, autoLoading); + return new webFileLoader( + _url, + QString(), + fromCloud, + autoLoading, + Data::kImageCacheTag); } namespace internal {