From 173108a9cbad22d66ecaa1f3f70ba128290da7fa Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Thu, 5 Jan 2023 17:32:51 +0400 Subject: [PATCH] Differ file download failure reasons ..and uncomment the code for handling incorrect permissions --- Telegram/Resources/langs/lang.strings | 2 +- Telegram/SourceFiles/data/data_cloud_file.cpp | 4 +-- Telegram/SourceFiles/data/data_document.cpp | 31 +++++++++---------- .../passport/passport_form_controller.cpp | 2 +- .../SourceFiles/storage/file_download.cpp | 20 ++++++------ Telegram/SourceFiles/storage/file_download.h | 17 ++++++++-- .../storage/file_download_mtproto.cpp | 4 +-- .../SourceFiles/storage/file_download_web.cpp | 2 +- .../storage/streamed_file_downloader.cpp | 2 +- 9 files changed, 47 insertions(+), 37 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index a4b642b26..49a552003 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -762,7 +762,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_download_path_dir_radio" = "Custom folder, cleared only manually"; "lng_download_path_choose" = "Choose download path"; "lng_sure_clear_downloads" = "Do you want to remove all downloaded files from temp folder? It is done automatically on logout or program uninstall."; -"lng_download_path_failed" = "File download could not be started.\n\nThis might be because the download location you've selected is invalid. Try changing the \"Download path\" in Settings."; +"lng_download_path_failed" = "File download could not be started.\n\nThe default download location will be used now. You can always change it in Settings > Advanced > Download Path.\n\nPlease try once again."; "lng_download_path_settings" = "Settings"; "lng_download_finish_failed" = "File download could not be finished.\n\nWould you like to try again?"; "lng_download_path_clearing" = "Clearing..."; diff --git a/Telegram/SourceFiles/data/data_cloud_file.cpp b/Telegram/SourceFiles/data/data_cloud_file.cpp index 6e35f2a8d..bee90358a 100644 --- a/Telegram/SourceFiles/data/data_cloud_file.cpp +++ b/Telegram/SourceFiles/data/data_cloud_file.cpp @@ -279,11 +279,11 @@ void LoadCloudFile( if (const auto onstack = progress) { onstack(); } - }, [=, &file](bool started) { + }, [=, &file](FileLoader::Error error) { finish(file); file.flags |= CloudFile::Flag::Failed; if (const auto onstack = fail) { - onstack(started); + onstack(error.started); } }, [=, &file] { finish(file); diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 84219bc0a..3d69f414b 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -988,8 +988,9 @@ void DocumentData::handleLoaderUpdates() { _loader->updates( ) | rpl::start_with_next_error_done([=] { _owner->documentLoadProgress(this); - }, [=](bool started) { - if (started && _loader) { + }, [=](FileLoader::Error error) { + using FailureReason = FileLoader::FailureReason; + if (error.started && _loader) { const auto origin = _loader->fileOrigin(); const auto failedFileName = _loader->fileName(); const auto retry = [=] { @@ -1000,23 +1001,21 @@ void DocumentData::handleLoaderUpdates() { tr::lng_download_finish_failed(), crl::guard(&session(), retry) })); - } else { - // Sometimes we have LOCATION_INVALID error in documents / stickers. - // Sometimes FILE_REFERENCE_EXPIRED could not be handled. - // - //const auto openSettings = [=] { - // Core::App().settings().etDownloadPathBookmark(QByteArray()); - // Core::App().settings().setDownloadPath(QString()); - // Ui::show(Box()); - //}; - //Ui::show(Box( - // tr::lng_download_path_failed(tr::now), - // tr::lng_download_path_settings(tr::now), - // crl::guard(&session(), openSettings))); + } else if (error.failureReason == FailureReason::FileWriteFailure) { + if (!Core::App().settings().downloadPath().isEmpty()) { + Core::App().settings().setDownloadPathBookmark(QByteArray()); + Core::App().settings().setDownloadPath(QString()); + Core::App().saveSettingsDelayed(); + InvokeQueued(qApp, [] { + Ui::show( + Ui::MakeInformBox( + tr::lng_download_path_failed(tr::now))); + }); + } } finishLoad(); status = FileDownloadFailed; - _owner->documentLoadFail(this, started); + _owner->documentLoadFail(this, error.started); }, [=] { finishLoad(); _owner->documentLoadDone(this); diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index 47df675c7..3b1598d15 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -1816,7 +1816,7 @@ void FormController::loadFile(File &file) { loader->updates( ) | rpl::start_with_next_error_done([=] { fileLoadProgress(key, loader->currentOffset()); - }, [=](bool started) { + }, [=](FileLoader::Error error) { fileLoadFail(key); }, [=] { fileLoadDone(key, loader->bytes()); diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 6e56f549c..3cd422269 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -128,12 +128,12 @@ void FileLoader::finishWithBytes(const QByteArray &data) { if (!_filename.isEmpty() && _toCache == LoadToCacheAsWell) { if (!_fileIsOpen) _fileIsOpen = _file.open(QIODevice::WriteOnly); if (!_fileIsOpen) { - cancel(true); + cancel(FailureReason::FileWriteFailure); return; } _file.seek(0); if (_file.write(_data) != qint64(_data.size())) { - cancel(true); + cancel(FailureReason::FileWriteFailure); return; } } @@ -258,7 +258,7 @@ bool FileLoader::checkForOpen() { if (_fileIsOpen) { return true; } - cancel(true); + cancel(FailureReason::FileWriteFailure); return false; } @@ -329,10 +329,10 @@ bool FileLoader::tryLoadLocal() { } void FileLoader::cancel() { - cancel(false); + cancel(FailureReason::NoFailure); } -void FileLoader::cancel(bool fail) { +void FileLoader::cancel(FailureReason fail) { const auto started = (currentOffset() > 0); cancelHook(); @@ -347,8 +347,8 @@ void FileLoader::cancel(bool fail) { _data = QByteArray(); const auto weak = base::make_weak(this); - if (fail) { - _updates.fire_error_copy(started); + if (fail != FailureReason::NoFailure) { + _updates.fire_error_copy({ fail, started }); } else { _updates.fire_done(); } @@ -377,7 +377,7 @@ bool FileLoader::writeResultPart(int64 offset, bytes::const_span buffer) { } _file.seek(offset); if (_file.write(reinterpret_cast(buffer.data()), buffer.size()) != qint64(buffer.size())) { - cancel(true); + cancel(FailureReason::FileWriteFailure); return false; } return true; @@ -410,7 +410,7 @@ QByteArray FileLoader::readLoadedPartBack(int64 offset, int size) { _file.close(); _fileIsOpen = _file.open(QIODevice::ReadWrite); if (!_fileIsOpen) { - cancel(true); + cancel(FailureReason::FileWriteFailure); return QByteArray(); } } @@ -434,7 +434,7 @@ bool FileLoader::finalizeResult() { } _file.seek(0); if (!_fileIsOpen || _file.write(_data) != qint64(_data.size())) { - cancel(true); + cancel(FailureReason::FileWriteFailure); return false; } } diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h index ece3fef25..d4f9908f7 100644 --- a/Telegram/SourceFiles/storage/file_download.h +++ b/Telegram/SourceFiles/storage/file_download.h @@ -52,6 +52,17 @@ struct StorageImageSaved { class FileLoader : public base::has_weak_ptr { public: + enum class FailureReason { + NoFailure, + FileWriteFailure, + OtherFailure, + }; + + struct Error { + FailureReason failureReason = FailureReason::NoFailure; + bool started = false; + }; + FileLoader( not_null session, const QString &toFile, @@ -113,7 +124,7 @@ public: const QByteArray &imageFormat, const QImage &imageData); - [[nodiscard]] rpl::producer updates() const { + [[nodiscard]] rpl::producer updates() const { return _updates.events(); } @@ -142,7 +153,7 @@ protected: startLoading(); } - void cancel(bool failed); + void cancel(FailureReason failed); void notifyAboutProgress(); @@ -177,7 +188,7 @@ protected: mutable QImage _imageData; rpl::lifetime _lifetime; - rpl::event_stream _updates; + rpl::event_stream _updates; }; diff --git a/Telegram/SourceFiles/storage/file_download_mtproto.cpp b/Telegram/SourceFiles/storage/file_download_mtproto.cpp index 5e96a54fe..18c4ca3a9 100644 --- a/Telegram/SourceFiles/storage/file_download_mtproto.cpp +++ b/Telegram/SourceFiles/storage/file_download_mtproto.cpp @@ -164,7 +164,7 @@ bool mtpFileLoader::feedPart(int64 offset, const QByteArray &bytes) { } void mtpFileLoader::cancelOnFail() { - cancel(true); + cancel(FailureReason::OtherFailure); } bool mtpFileLoader::setWebFileSizeHook(int64 size) { @@ -176,7 +176,7 @@ bool mtpFileLoader::setWebFileSizeHook(int64 size) { "Bad size provided by bot for webDocument: %1, real: %2" ).arg(_fullSize ).arg(size)); - cancel(true); + cancel(FailureReason::OtherFailure); return false; } diff --git a/Telegram/SourceFiles/storage/file_download_web.cpp b/Telegram/SourceFiles/storage/file_download_web.cpp index c9fd48878..33dda245f 100644 --- a/Telegram/SourceFiles/storage/file_download_web.cpp +++ b/Telegram/SourceFiles/storage/file_download_web.cpp @@ -506,7 +506,7 @@ void webFileLoader::loadFinished(const QByteArray &data) { } void webFileLoader::loadFailed() { - cancel(true); + cancel(FailureReason::OtherFailure); } Storage::Cache::Key webFileLoader::cacheKey() const { diff --git a/Telegram/SourceFiles/storage/streamed_file_downloader.cpp b/Telegram/SourceFiles/storage/streamed_file_downloader.cpp index ed9e074d1..990e0fd5a 100644 --- a/Telegram/SourceFiles/storage/streamed_file_downloader.cpp +++ b/Telegram/SourceFiles/storage/streamed_file_downloader.cpp @@ -59,7 +59,7 @@ StreamedFileDownloader::StreamedFileDownloader( _reader->partsForDownloader( ) | rpl::start_with_next([=](const LoadedPart &part) { if (part.offset == LoadedPart::kFailedOffset) { - cancel(true); + cancel(FailureReason::OtherFailure); } else { savePart(std::move(part)); }