From 08cd7450ffb3f27a13dab87588f3b4f342ba9872 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 5 Feb 2020 13:29:27 +0300 Subject: [PATCH] Added storage/storage_cloud_blob. - This file is needed to store same code parts related to management of dictionaries and emoji sets. - Moved extracting of zip files to storage_cloud_blob. --- Telegram/CMakeLists.txt | 2 + .../chat_helpers/emoji_sets_manager.cpp | 46 +------------ .../chat_helpers/spellchecker_common.cpp | 46 +------------ .../storage/storage_cloud_blob.cpp | 67 +++++++++++++++++++ .../SourceFiles/storage/storage_cloud_blob.h | 17 +++++ 5 files changed, 91 insertions(+), 87 deletions(-) create mode 100644 Telegram/SourceFiles/storage/storage_cloud_blob.cpp create mode 100644 Telegram/SourceFiles/storage/storage_cloud_blob.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 5c59b357b0..2125bd45a2 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -856,6 +856,8 @@ PRIVATE storage/serialize_common.h storage/serialize_document.cpp storage/serialize_document.h + storage/storage_cloud_blob.cpp + storage/storage_cloud_blob.h storage/storage_facade.cpp storage/storage_facade.h # storage/storage_feed_messages.cpp diff --git a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp index 03af314838..eb2d555261 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_account.h" #include "mainwidget.h" #include "app.h" +#include "storage/storage_cloud_blob.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" #include "styles/style_chat_helpers.h" @@ -197,26 +198,6 @@ QString StateDescription(const SetState &state) { }); } -QByteArray ReadFinalFile(const QString &path) { - constexpr auto kMaxZipSize = 10 * 1024 * 1024; - auto file = QFile(path); - if (file.size() > kMaxZipSize || !file.open(QIODevice::ReadOnly)) { - return QByteArray(); - } - return file.readAll(); -} - -bool ExtractZipFile(zlib::FileToRead &zip, const QString path) { - constexpr auto kMaxSize = 10 * 1024 * 1024; - const auto content = zip.readCurrentFileContent(kMaxSize); - if (content.isEmpty() || zip.error() != UNZ_OK) { - return false; - } - auto file = QFile(path); - return file.open(QIODevice::WriteOnly) - && (file.write(content) == content.size()); -} - bool GoodSetPartName(const QString &name) { return (name == qstr("config.json")) || (name.startsWith(qstr("emoji_")) @@ -224,30 +205,7 @@ bool GoodSetPartName(const QString &name) { } bool UnpackSet(const QString &path, const QString &folder) { - const auto bytes = ReadFinalFile(path); - if (bytes.isEmpty()) { - return false; - } - - auto zip = zlib::FileToRead(bytes); - if (zip.goToFirstFile() != UNZ_OK) { - return false; - } - do { - const auto name = zip.getCurrentFileName(); - const auto path = folder + '/' + name; - if (GoodSetPartName(name) && !ExtractZipFile(zip, path)) { - return false; - } - - const auto jump = zip.goToNextFile(); - if (jump == UNZ_END_OF_LIST_OF_FILE) { - break; - } else if (jump != UNZ_OK) { - return false; - } - } while (true); - return true; + return Storage::UnpackBlob(path, folder, GoodSetPartName); } Loader::Loader(QObject *parent, int id) diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp index cf149b5777..fce0a12d92 100644 --- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp +++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp @@ -9,6 +9,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #ifndef TDESKTOP_DISABLE_SPELLCHECK +#include "storage/storage_cloud_blob.h" + #include "base/zlib_help.h" namespace Spellchecker { @@ -84,26 +86,6 @@ void EnsurePath() { } } -QByteArray ReadFinalFile(const QString &path) { - constexpr auto kMaxZipSize = 10 * 1024 * 1024; //12 - auto file = QFile(path); - if (file.size() > kMaxZipSize || !file.open(QIODevice::ReadOnly)) { - return QByteArray(); - } - return file.readAll(); -} - -bool ExtractZipFile(zlib::FileToRead &zip, const QString path) { - constexpr auto kMaxSize = 10 * 1024 * 1024; - const auto content = zip.readCurrentFileContent(kMaxSize); - if (content.isEmpty() || zip.error() != UNZ_OK) { - return false; - } - auto file = QFile(path); - return file.open(QIODevice::WriteOnly) - && (file.write(content) == content.size()); -} - } // namespace std::initializer_list Dictionaries() { @@ -128,29 +110,7 @@ QString DictionariesPath() { bool UnpackDictionary(const QString &path, int langId) { const auto folder = DictPathByLangId(langId); - const auto bytes = ReadFinalFile(path); - if (bytes.isEmpty()) { - return false; - } - auto zip = zlib::FileToRead(bytes); - if (zip.goToFirstFile() != UNZ_OK) { - return false; - } - do { - const auto name = zip.getCurrentFileName(); - const auto path = folder + '/' + name; - if (IsGoodPartName(name) && !ExtractZipFile(zip, path)) { - return false; - } - - const auto jump = zip.goToNextFile(); - if (jump == UNZ_END_OF_LIST_OF_FILE) { - break; - } else if (jump != UNZ_OK) { - return false; - } - } while (true); - return true; + return Storage::UnpackBlob(path, folder, IsGoodPartName); } bool DictionaryExists(int langId) { diff --git a/Telegram/SourceFiles/storage/storage_cloud_blob.cpp b/Telegram/SourceFiles/storage/storage_cloud_blob.cpp new file mode 100644 index 0000000000..c394271bb6 --- /dev/null +++ b/Telegram/SourceFiles/storage/storage_cloud_blob.cpp @@ -0,0 +1,67 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "storage/storage_cloud_blob.h" + +#include "base/zlib_help.h" + +namespace Storage { + +namespace { + +QByteArray ReadFinalFile(const QString &path) { + constexpr auto kMaxZipSize = 10 * 1024 * 1024; + auto file = QFile(path); + if (file.size() > kMaxZipSize || !file.open(QIODevice::ReadOnly)) { + return QByteArray(); + } + return file.readAll(); +} + +bool ExtractZipFile(zlib::FileToRead &zip, const QString path) { + constexpr auto kMaxSize = 25 * 1024 * 1024; + const auto content = zip.readCurrentFileContent(kMaxSize); + if (content.isEmpty() || zip.error() != UNZ_OK) { + return false; + } + auto file = QFile(path); + return file.open(QIODevice::WriteOnly) + && (file.write(content) == content.size()); +} + +} // namespace + +bool UnpackBlob( + const QString &path, + const QString &folder, + Fn checkNameCallback) { + const auto bytes = ReadFinalFile(path); + if (bytes.isEmpty()) { + return false; + } + auto zip = zlib::FileToRead(bytes); + if (zip.goToFirstFile() != UNZ_OK) { + return false; + } + do { + const auto name = zip.getCurrentFileName(); + const auto path = folder + '/' + name; + if (checkNameCallback(name) && !ExtractZipFile(zip, path)) { + return false; + } + + const auto jump = zip.goToNextFile(); + if (jump == UNZ_END_OF_LIST_OF_FILE) { + break; + } else if (jump != UNZ_OK) { + return false; + } + } while (true); + return true; +} + +} // namespace Storage diff --git a/Telegram/SourceFiles/storage/storage_cloud_blob.h b/Telegram/SourceFiles/storage/storage_cloud_blob.h new file mode 100644 index 0000000000..5728803d18 --- /dev/null +++ b/Telegram/SourceFiles/storage/storage_cloud_blob.h @@ -0,0 +1,17 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Storage { + +bool UnpackBlob( + const QString &path, + const QString &folder, + Fn checkNameCallback); + +} // namespace Storage