FileDownloader
Qualified name: mv::util::FileDownloader
-
class FileDownloader : public QObject
File downloader class.
Helper class for simplifying file download
- Author
Thomas Kroes
Public Static Functions
Get the shared network access manager instance.
- Returns:
Reference to the shared QNetworkAccessManager instance
-
static QFuture<QByteArray> downloadToByteArrayAsync(const QUrl &url, Task *task = nullptr)
Download file to byte array asynchronously.
- Parameters:
url – URL of the file to download
task – Optional task to associate with the download operation (must live in the main/GUI thread)
- Returns:
Future containing the downloaded data as byte array
-
static QFuture<QString> downloadToFileAsync(const QUrl &url, const QString &targetDirectory = "", Task *task = nullptr, bool overwriteAllowed = true)
Download file from
urltotargetDirectoryasynchronously.- Parameters:
url – URL of the file to download
targetDirectory – Directory where the file should be saved (OS temporary dir when empty)
task – Optional task to associate with the download operation (must live in the main/GUI thread)
overwriteAllowed – Whether it is ok to overwrite existing download
- Returns:
Future containing the path to the downloaded file
-
static QFuture<std::uint64_t> getDownloadSizeAsync(const QUrl &url)
Get the size of the file to be downloaded asynchronously.
- Parameters:
url – URL of the file to be downloaded
- Returns:
Size of the file to be downloaded in bytes, 0 if size cannot be determined
-
static QFuture<QDateTime> getLastModifiedAsync(const QUrl &url)
Get the last modified date of the file at
urlasynchronously.- Parameters:
url – URL of the file to check
- Returns:
Last modified date of the file, QDateTime() if it cannot be determined
-
static QFuture<QString> getFinalFileNameAsync(const QUrl &url)
Get the final file name of the file at
urlasynchronously (works with redirected URL)- Parameters:
url – URL of the file to check
- Returns:
QFuture that will contain the final file name of the file, or an empty string if it cannot be determined
-
class ByteArraySink : public mv::util::FileDownloader::ISink<QByteArray>
Saves the download to a byte array.
Public Functions
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) override
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
reply – Pointer to network reply (may not be nullptr)
url – Url to download
targetDirectory – Target directory where to put the download
overwriteAllowed – Whether it is allowed to overwrite an existing download
error – Pointer to error
- Returns:
Whether successfully opened
-
virtual bool write(const QByteArray &chunk, QString *error) override
Write
chunkwitherror.- Parameters:
chunk – Chunk of data
error – Pointer to error
- Returns:
Whether successfully written
-
virtual bool commit(QString *error) override
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
error – Pointer to error
- Returns:
Whether commit was successful
-
virtual void cancel() noexcept override
Discards partial output.
-
virtual QByteArray result() const override
Get the resulting value to emit into the promise.
- Returns:
The promise result
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) override
-
class Exception : public mv::util::BaseException
Exception class for file downloader issues.
Public Functions
-
explicit Exception(const QString &message)
Construct with
message.- Parameters:
message – Message describing the download issue
-
explicit Exception(const QString &message)
-
class FileSink : public mv::util::FileDownloader::ISink<QString>
Saves the download to a file on disk.
Public Functions
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) override
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
reply – Pointer to network reply (may not be nullptr)
url – Url to download
targetDirectory – Target directory where to put the download
overwriteAllowed – Whether it is allowed to overwrite an existing download
error – Pointer to error
- Returns:
Whether successfully opened
-
virtual bool write(const QByteArray &chunk, QString *error) override
Write
chunkwitherror.- Parameters:
chunk – Chunk of data
error – Pointer to error
- Returns:
Whether successfully written
-
virtual bool commit(QString *error) override
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
error – Pointer to error
- Returns:
Whether commit was successful
-
virtual void cancel() noexcept override
Discards partial output.
-
virtual QString result() const override
Get the resulting value to emit into the promise.
- Returns:
The promise result
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) override
-
template<typename T>
struct ISink Public Functions
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) = 0
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
reply – Pointer to network reply (may not be nullptr)
url – Url to download
targetDirectory – Target directory where to put the download
overwriteAllowed – Whether it is allowed to overwrite an existing download
error – Pointer to error
- Returns:
Whether successfully opened
-
virtual bool write(const QByteArray &chunk, QString *error) = 0
Write
chunkwitherror.- Parameters:
chunk – Chunk of data
error – Pointer to error
- Returns:
Whether successfully written
-
virtual bool commit(QString *error) = 0
Finalize successfully (e.g., commit QSaveFile)
- Parameters:
error – Pointer to error
- Returns:
Whether commit was successful
-
virtual void cancel() noexcept = 0
Discards partial output.
-
virtual Result result() const = 0
Get the resulting value to emit into the promise.
- Returns:
The promise result
-
virtual bool open(QNetworkReply *reply, const QUrl &url, const QString &targetDirectory, bool overwriteAllowed, QString *error) = 0