image_location.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 "data/data_peer_id.h"
  9. class FileLoader;
  10. namespace Storage {
  11. namespace Cache {
  12. struct Key;
  13. } // namespace Cache
  14. } // namespace Storage
  15. namespace Data {
  16. struct UpdatedFileReferences;
  17. } // namespace Data
  18. enum LoadFromCloudSetting {
  19. LoadFromCloudOrLocal,
  20. LoadFromLocalOnly,
  21. };
  22. enum LoadToCacheSetting {
  23. LoadToFileOnly,
  24. LoadToCacheAsWell,
  25. };
  26. using InMemoryKey = std::pair<uint64, uint64>;
  27. namespace std {
  28. template<>
  29. struct hash<InMemoryKey> {
  30. size_t operator()(InMemoryKey value) const {
  31. auto seed = hash<uint64>()(value.first);
  32. seed ^= hash<uint64>()(value.second)
  33. + std::size_t(0x9e3779b9)
  34. + (seed << 6) + (seed >> 2);
  35. return seed;
  36. }
  37. };
  38. } // namespace std
  39. class StorageFileLocation {
  40. public:
  41. // Those are used in serialization, don't change.
  42. enum class Type : uint8 {
  43. Legacy = 0x00,
  44. Encrypted = 0x01,
  45. Document = 0x02,
  46. Secure = 0x03,
  47. Takeout = 0x04,
  48. Photo = 0x05,
  49. PeerPhoto = 0x06,
  50. StickerSetThumb = 0x07,
  51. GroupCallStream = 0x08,
  52. };
  53. StorageFileLocation() = default;
  54. StorageFileLocation(
  55. int32 dcId,
  56. UserId self,
  57. const MTPInputFileLocation &tl);
  58. [[nodiscard]] StorageFileLocation convertToModernPeerPhoto(
  59. uint64 id,
  60. uint64 accessHash,
  61. uint64 photoId) const;
  62. [[nodiscard]] int32 dcId() const;
  63. [[nodiscard]] uint64 objectId() const;
  64. [[nodiscard]] MTPInputFileLocation tl(UserId self) const;
  65. [[nodiscard]] QByteArray serialize() const;
  66. [[nodiscard]] int serializeSize() const;
  67. [[nodiscard]] static std::optional<StorageFileLocation> FromSerialized(
  68. const QByteArray &serialized);
  69. [[nodiscard]] Type type() const;
  70. [[nodiscard]] bool valid() const;
  71. [[nodiscard]] bool isLegacy() const;
  72. [[nodiscard]] Storage::Cache::Key cacheKey() const;
  73. [[nodiscard]] Storage::Cache::Key bigFileBaseCacheKey() const;
  74. // We have to allow checking this because of a serialization bug.
  75. [[nodiscard]] bool isDocumentThumbnail() const;
  76. [[nodiscard]] QByteArray fileReference() const;
  77. bool refreshFileReference(const Data::UpdatedFileReferences &updates);
  78. bool refreshFileReference(const QByteArray &data);
  79. [[nodiscard]] static const StorageFileLocation &Invalid();
  80. friend bool operator==(
  81. const StorageFileLocation &a,
  82. const StorageFileLocation &b);
  83. friend bool operator<(
  84. const StorageFileLocation &a,
  85. const StorageFileLocation &b);
  86. private:
  87. uint16 _dcId = 0;
  88. Type _type = Type::Legacy;
  89. uint8 _sizeLetter = 0;
  90. int32 _localId = 0;
  91. uint64 _id = 0;
  92. uint64 _accessHash = 0;
  93. uint64 _volumeId = 0;
  94. PeerId _inMessagePeerId = 0;
  95. uint32 _inMessageId = 0;
  96. QByteArray _fileReference;
  97. };
  98. inline bool operator!=(
  99. const StorageFileLocation &a,
  100. const StorageFileLocation &b) {
  101. return !(a == b);
  102. }
  103. inline bool operator>(
  104. const StorageFileLocation &a,
  105. const StorageFileLocation &b) {
  106. return (b < a);
  107. }
  108. inline bool operator<=(
  109. const StorageFileLocation &a,
  110. const StorageFileLocation &b) {
  111. return !(b < a);
  112. }
  113. inline bool operator>=(
  114. const StorageFileLocation &a,
  115. const StorageFileLocation &b) {
  116. return !(a < b);
  117. }
  118. class StorageImageLocation {
  119. public:
  120. StorageImageLocation() = default;
  121. StorageImageLocation(
  122. const StorageFileLocation &file,
  123. int width,
  124. int height);
  125. [[nodiscard]] QByteArray serialize() const;
  126. [[nodiscard]] int serializeSize() const;
  127. [[nodiscard]] static std::optional<StorageImageLocation> FromSerialized(
  128. const QByteArray &serialized);
  129. [[nodiscard]] StorageImageLocation convertToModernPeerPhoto(
  130. uint64 id,
  131. uint64 accessHash,
  132. uint64 photoId) const {
  133. return StorageImageLocation(
  134. _file.convertToModernPeerPhoto(id, accessHash, photoId),
  135. _width,
  136. _height);
  137. }
  138. [[nodiscard]] const StorageFileLocation &file() const {
  139. return _file;
  140. }
  141. [[nodiscard]] int width() const {
  142. return _width;
  143. }
  144. [[nodiscard]] int height() const {
  145. return _height;
  146. }
  147. void setSize(int width, int height) {
  148. _width = width;
  149. _height = height;
  150. }
  151. [[nodiscard]] StorageFileLocation::Type type() const {
  152. return _file.type();
  153. }
  154. [[nodiscard]] bool valid() const {
  155. return _file.valid();
  156. }
  157. [[nodiscard]] bool isLegacy() const {
  158. return _file.isLegacy();
  159. }
  160. [[nodiscard]] QByteArray fileReference() const {
  161. return _file.fileReference();
  162. }
  163. bool refreshFileReference(const QByteArray &data) {
  164. return _file.refreshFileReference(data);
  165. }
  166. bool refreshFileReference(const Data::UpdatedFileReferences &updates) {
  167. return _file.refreshFileReference(updates);
  168. }
  169. [[nodiscard]] static const StorageImageLocation &Invalid() {
  170. static auto result = StorageImageLocation();
  171. return result;
  172. }
  173. friend inline bool operator==(
  174. const StorageImageLocation &a,
  175. const StorageImageLocation &b) {
  176. return (a._file == b._file);
  177. }
  178. friend inline bool operator<(
  179. const StorageImageLocation &a,
  180. const StorageImageLocation &b) {
  181. return (a._file < b._file);
  182. }
  183. private:
  184. StorageFileLocation _file;
  185. int _width = 0;
  186. int _height = 0;
  187. };
  188. inline bool operator!=(
  189. const StorageImageLocation &a,
  190. const StorageImageLocation &b) {
  191. return !(a == b);
  192. }
  193. inline bool operator>(
  194. const StorageImageLocation &a,
  195. const StorageImageLocation &b) {
  196. return (b < a);
  197. }
  198. inline bool operator<=(
  199. const StorageImageLocation &a,
  200. const StorageImageLocation &b) {
  201. return !(b < a);
  202. }
  203. inline bool operator>=(
  204. const StorageImageLocation &a,
  205. const StorageImageLocation &b) {
  206. return !(a < b);
  207. }
  208. class WebFileLocation {
  209. public:
  210. WebFileLocation() = default;
  211. WebFileLocation(const QByteArray &url, uint64 accessHash)
  212. : _accessHash(accessHash)
  213. , _url(url) {
  214. }
  215. bool isNull() const {
  216. return _url.isEmpty();
  217. }
  218. uint64 accessHash() const {
  219. return _accessHash;
  220. }
  221. const QByteArray &url() const {
  222. return _url;
  223. }
  224. static WebFileLocation Null;
  225. private:
  226. uint64 _accessHash = 0;
  227. QByteArray _url;
  228. friend inline bool operator==(
  229. const WebFileLocation &a,
  230. const WebFileLocation &b) {
  231. return (a._accessHash == b._accessHash)
  232. && (a._url == b._url);
  233. }
  234. friend inline bool operator<(
  235. const WebFileLocation &a,
  236. const WebFileLocation &b) {
  237. return std::tie(a._accessHash, a._url)
  238. < std::tie(b._accessHash, b._url);
  239. }
  240. };
  241. inline bool operator!=(const WebFileLocation &a, const WebFileLocation &b) {
  242. return !(a == b);
  243. }
  244. inline bool operator>(const WebFileLocation &a, const WebFileLocation &b) {
  245. return (b < a);
  246. }
  247. inline bool operator<=(const WebFileLocation &a, const WebFileLocation &b) {
  248. return !(b < a);
  249. }
  250. inline bool operator>=(const WebFileLocation &a, const WebFileLocation &b) {
  251. return !(a < b);
  252. }
  253. struct GeoPointLocation {
  254. float64 lat = 0.;
  255. float64 lon = 0.;
  256. uint64 access = 0;
  257. int32 width = 0;
  258. int32 height = 0;
  259. int32 zoom = 0;
  260. int32 scale = 0;
  261. };
  262. inline bool operator==(
  263. const GeoPointLocation &a,
  264. const GeoPointLocation &b) {
  265. return (a.lat == b.lat)
  266. && (a.lon == b.lon)
  267. && (a.access == b.access)
  268. && (a.width == b.width)
  269. && (a.height == b.height)
  270. && (a.zoom == b.zoom)
  271. && (a.scale == b.scale);
  272. }
  273. inline bool operator<(
  274. const GeoPointLocation &a,
  275. const GeoPointLocation &b) {
  276. return std::tie(
  277. a.access,
  278. a.lat,
  279. a.lon,
  280. a.width,
  281. a.height,
  282. a.zoom,
  283. a.scale)
  284. < std::tie(
  285. b.access,
  286. b.lat,
  287. b.lon,
  288. b.width,
  289. b.height,
  290. b.zoom,
  291. b.scale);
  292. }
  293. inline bool operator!=(
  294. const GeoPointLocation &a,
  295. const GeoPointLocation &b) {
  296. return !(a == b);
  297. }
  298. inline bool operator>(
  299. const GeoPointLocation &a,
  300. const GeoPointLocation &b) {
  301. return (b < a);
  302. }
  303. inline bool operator<=(
  304. const GeoPointLocation &a,
  305. const GeoPointLocation &b) {
  306. return !(b < a);
  307. }
  308. inline bool operator>=(
  309. const GeoPointLocation &a,
  310. const GeoPointLocation &b) {
  311. return !(a < b);
  312. }
  313. struct PlainUrlLocation {
  314. QString url;
  315. friend inline bool operator==(
  316. const PlainUrlLocation &a,
  317. const PlainUrlLocation &b) {
  318. return (a.url == b.url);
  319. }
  320. friend inline bool operator<(
  321. const PlainUrlLocation &a,
  322. const PlainUrlLocation &b) {
  323. return (a.url < b.url);
  324. }
  325. };
  326. inline bool operator!=(
  327. const PlainUrlLocation &a,
  328. const PlainUrlLocation &b) {
  329. return !(a == b);
  330. }
  331. inline bool operator>(
  332. const PlainUrlLocation &a,
  333. const PlainUrlLocation &b) {
  334. return (b < a);
  335. }
  336. inline bool operator<=(
  337. const PlainUrlLocation &a,
  338. const PlainUrlLocation &b) {
  339. return !(b < a);
  340. }
  341. inline bool operator>=(
  342. const PlainUrlLocation &a,
  343. const PlainUrlLocation &b) {
  344. return !(a < b);
  345. }
  346. struct AudioAlbumThumbLocation {
  347. uint64 documentId = 0;
  348. friend inline auto operator<=>(
  349. AudioAlbumThumbLocation,
  350. AudioAlbumThumbLocation) = default;
  351. };
  352. struct InMemoryLocation {
  353. QByteArray bytes;
  354. friend inline bool operator==(
  355. const InMemoryLocation &a,
  356. const InMemoryLocation &b) {
  357. return (a.bytes == b.bytes);
  358. }
  359. friend inline bool operator<(
  360. const InMemoryLocation &a,
  361. const InMemoryLocation &b) {
  362. return (a.bytes < b.bytes);
  363. }
  364. };
  365. inline bool operator!=(
  366. const InMemoryLocation &a,
  367. const InMemoryLocation &b) {
  368. return !(a == b);
  369. }
  370. inline bool operator>(
  371. const InMemoryLocation &a,
  372. const InMemoryLocation &b) {
  373. return (b < a);
  374. }
  375. inline bool operator<=(
  376. const InMemoryLocation &a,
  377. const InMemoryLocation &b) {
  378. return !(b < a);
  379. }
  380. inline bool operator>=(
  381. const InMemoryLocation &a,
  382. const InMemoryLocation &b) {
  383. return !(a < b);
  384. }
  385. class DownloadLocation {
  386. public:
  387. std::variant<
  388. StorageFileLocation,
  389. WebFileLocation,
  390. GeoPointLocation,
  391. PlainUrlLocation,
  392. AudioAlbumThumbLocation,
  393. InMemoryLocation> data;
  394. [[nodiscard]] QByteArray serialize() const;
  395. [[nodiscard]] int serializeSize() const;
  396. [[nodiscard]] static std::optional<DownloadLocation> FromSerialized(
  397. const QByteArray &serialized);
  398. [[nodiscard]] DownloadLocation convertToModernPeerPhoto(
  399. uint64 id,
  400. uint64 accessHash,
  401. uint64 photoId) const;
  402. [[nodiscard]] Storage::Cache::Key cacheKey() const;
  403. [[nodiscard]] Storage::Cache::Key bigFileBaseCacheKey() const;
  404. [[nodiscard]] bool valid() const;
  405. [[nodiscard]] bool isLegacy() const;
  406. [[nodiscard]] QByteArray fileReference() const;
  407. bool refreshFileReference(const QByteArray &data);
  408. bool refreshFileReference(const Data::UpdatedFileReferences &updates);
  409. friend inline bool operator==(
  410. const DownloadLocation &a,
  411. const DownloadLocation &b) {
  412. return (a.data == b.data);
  413. }
  414. friend inline bool operator<(
  415. const DownloadLocation &a,
  416. const DownloadLocation &b) {
  417. return (a.data < b.data);
  418. }
  419. };
  420. inline bool operator!=(
  421. const DownloadLocation &a,
  422. const DownloadLocation &b) {
  423. return !(a == b);
  424. }
  425. inline bool operator>(
  426. const DownloadLocation &a,
  427. const DownloadLocation &b) {
  428. return (b < a);
  429. }
  430. inline bool operator<=(
  431. const DownloadLocation &a,
  432. const DownloadLocation &b) {
  433. return !(b < a);
  434. }
  435. inline bool operator>=(
  436. const DownloadLocation &a,
  437. const DownloadLocation &b) {
  438. return !(a < b);
  439. }
  440. class ImageLocation {
  441. public:
  442. ImageLocation() = default;
  443. ImageLocation(
  444. const DownloadLocation &file,
  445. int width,
  446. int height);
  447. [[nodiscard]] QByteArray serialize() const;
  448. [[nodiscard]] int serializeSize() const;
  449. [[nodiscard]] static std::optional<ImageLocation> FromSerialized(
  450. const QByteArray &serialized);
  451. [[nodiscard]] ImageLocation convertToModernPeerPhoto(
  452. uint64 id,
  453. uint64 accessHash,
  454. uint64 photoId) const {
  455. return ImageLocation(
  456. _file.convertToModernPeerPhoto(id, accessHash, photoId),
  457. _width,
  458. _height);
  459. }
  460. [[nodiscard]] const DownloadLocation &file() const {
  461. return _file;
  462. }
  463. [[nodiscard]] int width() const {
  464. return _width;
  465. }
  466. [[nodiscard]] int height() const {
  467. return _height;
  468. }
  469. void setSize(int width, int height) {
  470. _width = width;
  471. _height = height;
  472. }
  473. [[nodiscard]] bool valid() const {
  474. return _file.valid();
  475. }
  476. [[nodiscard]] bool isLegacy() const {
  477. return _file.isLegacy();
  478. }
  479. [[nodiscard]] QByteArray fileReference() const {
  480. return _file.fileReference();
  481. }
  482. bool refreshFileReference(const QByteArray &data) {
  483. return _file.refreshFileReference(data);
  484. }
  485. bool refreshFileReference(const Data::UpdatedFileReferences &updates) {
  486. return _file.refreshFileReference(updates);
  487. }
  488. [[nodiscard]] static const ImageLocation &Invalid() {
  489. static auto result = ImageLocation();
  490. return result;
  491. }
  492. friend inline bool operator==(
  493. const ImageLocation &a,
  494. const ImageLocation &b) {
  495. return (a._file == b._file);
  496. }
  497. friend inline bool operator<(
  498. const ImageLocation &a,
  499. const ImageLocation &b) {
  500. return (a._file < b._file);
  501. }
  502. private:
  503. DownloadLocation _file;
  504. int _width = 0;
  505. int _height = 0;
  506. };
  507. inline bool operator!=(
  508. const ImageLocation &a,
  509. const ImageLocation &b) {
  510. return !(a == b);
  511. }
  512. inline bool operator>(
  513. const ImageLocation &a,
  514. const ImageLocation &b) {
  515. return (b < a);
  516. }
  517. inline bool operator<=(
  518. const ImageLocation &a,
  519. const ImageLocation &b) {
  520. return !(b < a);
  521. }
  522. inline bool operator>=(
  523. const ImageLocation &a,
  524. const ImageLocation &b) {
  525. return !(a < b);
  526. }
  527. struct ImageWithLocation {
  528. ImageLocation location;
  529. QByteArray bytes;
  530. QImage preloaded;
  531. int bytesCount = 0;
  532. int progressivePartSize = 0;
  533. };
  534. struct InlineImageLocation {
  535. QByteArray bytes;
  536. bool isPath = false;
  537. };
  538. InMemoryKey inMemoryKey(const StorageFileLocation &location);
  539. inline InMemoryKey inMemoryKey(const StorageImageLocation &location) {
  540. return inMemoryKey(location.file());
  541. }
  542. InMemoryKey inMemoryKey(const WebFileLocation &location);
  543. InMemoryKey inMemoryKey(const GeoPointLocation &location);
  544. InMemoryKey inMemoryKey(const PlainUrlLocation &location);
  545. InMemoryKey inMemoryKey(const InMemoryLocation &location);
  546. InMemoryKey inMemoryKey(const DownloadLocation &location);
  547. inline InMemoryKey inMemoryKey(const ImageLocation &location) {
  548. return inMemoryKey(location.file());
  549. }
  550. inline QSize shrinkToKeepAspect(int32 width, int32 height, int32 towidth, int32 toheight) {
  551. int32 w = qMax(width, 1), h = qMax(height, 1);
  552. if (w * toheight > h * towidth) {
  553. h = qRound(h * towidth / float64(w));
  554. w = towidth;
  555. } else {
  556. w = qRound(w * toheight / float64(h));
  557. h = toheight;
  558. }
  559. return QSize(qMax(w, 1), qMax(h, 1));
  560. }