storage_cloud_blob.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "mtproto/dedicated_file_loader.h"
  9. namespace tr {
  10. template <typename ...>
  11. struct phrase;
  12. } // namespace tr
  13. namespace Main {
  14. class Session;
  15. } // namespace Main
  16. namespace Storage::CloudBlob {
  17. constexpr auto kCloudLocationUsername = "tdhbcfiles"_cs;
  18. struct Blob {
  19. int id = 0;
  20. int postId = 0;
  21. int64 size = 0;
  22. QString name;
  23. };
  24. struct Available {
  25. int64 size = 0;
  26. inline bool operator<(const Available &other) const {
  27. return size < other.size;
  28. }
  29. inline bool operator==(const Available &other) const {
  30. return size == other.size;
  31. }
  32. };
  33. struct Ready {
  34. inline bool operator<(const Ready &other) const {
  35. return false;
  36. }
  37. inline bool operator==(const Ready &other) const {
  38. return true;
  39. }
  40. };
  41. struct Active {
  42. inline bool operator<(const Active &other) const {
  43. return false;
  44. }
  45. inline bool operator==(const Active &other) const {
  46. return true;
  47. }
  48. };
  49. struct Failed {
  50. inline bool operator<(const Failed &other) const {
  51. return false;
  52. }
  53. inline bool operator==(const Failed &other) const {
  54. return true;
  55. }
  56. };
  57. using Loading = MTP::DedicatedLoader::Progress;
  58. using BlobState = std::variant<
  59. Available,
  60. Ready,
  61. Active,
  62. Failed,
  63. Loading>;
  64. bool UnpackBlob(
  65. const QString &path,
  66. const QString &folder,
  67. Fn<bool(const QString &)> checkNameCallback);
  68. QString StateDescription(const BlobState &state, tr::phrase<> activeText);
  69. class BlobLoader : public QObject {
  70. public:
  71. BlobLoader(
  72. QObject *parent,
  73. not_null<Main::Session*> session,
  74. int id,
  75. MTP::DedicatedLoader::Location location,
  76. const QString &folder,
  77. int64 size);
  78. int id() const;
  79. rpl::producer<BlobState> state() const;
  80. virtual void destroy() = 0;
  81. virtual void unpack(const QString &path) = 0;
  82. protected:
  83. virtual void fail();
  84. const QString _folder;
  85. private:
  86. void setImplementation(std::unique_ptr<MTP::DedicatedLoader> loader);
  87. int _id = 0;
  88. rpl::variable<BlobState> _state;
  89. MTP::WeakInstance _mtproto;
  90. std::unique_ptr<MTP::DedicatedLoader> _implementation;
  91. };
  92. } // namespace Storage::CloudBlob