file_location.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "core/file_location.h"
  8. #include "platform/platform_file_bookmark.h"
  9. #include "logs.h"
  10. #include <QtCore/QFileInfo>
  11. namespace Core {
  12. namespace {
  13. const auto kInMediaCacheLocation = u"*media_cache*"_q;
  14. constexpr auto kMaxFileSize = 4000 * int64(1024 * 1024);
  15. } // namespace
  16. ReadAccessEnabler::ReadAccessEnabler(const Platform::FileBookmark *bookmark)
  17. : _bookmark(bookmark)
  18. , _failed(_bookmark ? !_bookmark->enable() : false) {
  19. }
  20. ReadAccessEnabler::ReadAccessEnabler(
  21. const std::shared_ptr<Platform::FileBookmark> &bookmark)
  22. : _bookmark(bookmark.get())
  23. , _failed(_bookmark ? !_bookmark->enable() : false) {
  24. }
  25. ReadAccessEnabler::~ReadAccessEnabler() {
  26. if (_bookmark && !_failed) _bookmark->disable();
  27. }
  28. FileLocation::FileLocation(const QString &name) : fname(name) {
  29. if (fname.isEmpty() || fname == kInMediaCacheLocation) {
  30. size = 0;
  31. } else {
  32. setBookmark(Platform::PathBookmark(name));
  33. resolveFromInfo(QFileInfo(name));
  34. }
  35. }
  36. FileLocation::FileLocation(const QFileInfo &info) : fname(info.filePath()) {
  37. if (fname.isEmpty()) {
  38. size = 0;
  39. } else {
  40. setBookmark(Platform::PathBookmark(fname));
  41. resolveFromInfo(info);
  42. }
  43. }
  44. void FileLocation::resolveFromInfo(const QFileInfo &info) {
  45. if (info.exists()) {
  46. const auto s = info.size();
  47. if (s > kMaxFileSize) {
  48. fname = QString();
  49. _bookmark = nullptr;
  50. size = 0;
  51. } else {
  52. modified = info.lastModified();
  53. size = s;
  54. }
  55. } else {
  56. fname = QString();
  57. _bookmark = nullptr;
  58. size = 0;
  59. }
  60. }
  61. FileLocation FileLocation::InMediaCacheLocation() {
  62. return FileLocation(kInMediaCacheLocation);
  63. }
  64. bool FileLocation::check() const {
  65. if (fname.isEmpty() || fname == kInMediaCacheLocation) {
  66. return false;
  67. }
  68. ReadAccessEnabler enabler(_bookmark);
  69. if (enabler.failed()) {
  70. const_cast<FileLocation*>(this)->_bookmark = nullptr;
  71. }
  72. QFileInfo f(name());
  73. if (!f.isReadable()) return false;
  74. quint64 s = f.size();
  75. if (s > kMaxFileSize) {
  76. DEBUG_LOG(("File location check: Wrong size %1").arg(s));
  77. return false;
  78. }
  79. if (s != size) {
  80. DEBUG_LOG(("File location check: Wrong size %1 when should be %2").arg(s).arg(size));
  81. return false;
  82. }
  83. auto realModified = f.lastModified();
  84. if (realModified != modified) {
  85. DEBUG_LOG(("File location check: Wrong last modified time %1 when should be %2").arg(realModified.toMSecsSinceEpoch()).arg(modified.toMSecsSinceEpoch()));
  86. return false;
  87. }
  88. return true;
  89. }
  90. const QString &FileLocation::name() const {
  91. return _bookmark ? _bookmark->name(fname) : fname;
  92. }
  93. QByteArray FileLocation::bookmark() const {
  94. return _bookmark ? _bookmark->bookmark() : QByteArray();
  95. }
  96. bool FileLocation::inMediaCache() const {
  97. return (fname == kInMediaCacheLocation);
  98. }
  99. void FileLocation::setBookmark(const QByteArray &bm) {
  100. _bookmark.reset(bm.isEmpty() ? nullptr : new Platform::FileBookmark(bm));
  101. }
  102. bool FileLocation::accessEnable() const {
  103. return isEmpty() ? false : (_bookmark ? _bookmark->enable() : true);
  104. }
  105. void FileLocation::accessDisable() const {
  106. return _bookmark ? _bookmark->disable() : (void)0;
  107. }
  108. } // namespace Core