Skip to content

Commit

Permalink
Add option for DownloadManagers to auto-delete incomplete files
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Jul 1, 2024
1 parent c395e5c commit 83a4f49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/network/include/qx/network/qx-downloadmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject
bool mOverwrite;
bool mStopOnError;
bool mSkipEnumeration;
bool mDeletePartials;
// TODO: May also want to have option for removing ALL files upon any failure, even complete ones
QCryptographicHash::Algorithm mVerificationMethod;

// Status
Expand Down Expand Up @@ -154,6 +156,7 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject
bool isOverwrite() const;
bool isStopOnError() const;
bool isSkipEnumeration() const;
bool isDeletePartialDownloads() const;
QCryptographicHash::Algorithm verificationMethod() const;
int taskCount() const;
bool hasTasks() const;
Expand All @@ -166,6 +169,7 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject
void setOverwrite(bool overwrite);
void setStopOnError(bool stopOnError);
void setSkipEnumeration(bool skipEnumeration);
void setDeletePartialDownloads(bool deletePartialDownloads);
void setVerificationMethod(QCryptographicHash::Algorithm method);

// Tasks
Expand Down
42 changes: 33 additions & 9 deletions lib/network/src/qx-downloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ AsyncDownloadManager::AsyncDownloadManager(QObject* parent) :
mOverwrite(false),
mStopOnError(false),
mSkipEnumeration(false),
mDeletePartials(false),
mVerificationMethod(QCryptographicHash::Sha256),
mStatus(Status::Initial)
{
Expand Down Expand Up @@ -322,7 +323,7 @@ bool AsyncDownloadManager::isStopOnError() const { return mStopOnError; }

/*!
* Returns @c true if the manager is configured to query the size of all queued tasks before
* actually initiating any downloads; otherwise returns @c false.
* actually initiating any downloads; otherwise, returns @c false.
*
* If enumeration is disabled, total download progress reported by the manager will be limited
* in scope to only active and finished downloads, as the size of future download tasks cannot
Expand All @@ -341,6 +342,17 @@ bool AsyncDownloadManager::isStopOnError() const { return mStopOnError; }
*/
bool AsyncDownloadManager::isSkipEnumeration() const { return mSkipEnumeration; }


/*!
* Returns @c true if the manager is configured to remove any incomplete downloads after they
* fail or are aborted; otherwise, returns @c false.
*
* The default is @c false.
*
* @sa setDeletePartialDownloads().
*/
bool AsyncDownloadManager::isDeletePartialDownloads() const { return mDeletePartials; }

/*!
* Returns the hash algorithm used to verify downloads for tasks that include a checksum.
*
Expand Down Expand Up @@ -441,6 +453,14 @@ void AsyncDownloadManager::setStopOnError(bool stopOnError) { mStopOnError = sto
*/
void AsyncDownloadManager::setSkipEnumeration(bool skipEnumeration) { mSkipEnumeration = skipEnumeration; }

/*!
* Configures the manager to automatically remove incomplete files after a download fails or is aborted if
* @a deletePartialDownloads is @c true; otherwise, partial downloads are kept.
*
* @sa isDeletePartialDownloads().
*/
void AsyncDownloadManager::setDeletePartialDownloads(bool deletePartialDownloads) { mDeletePartials = deletePartialDownloads; }

/*!
* Sets the hash algorithm used to verify downloads for tasks that include a checksum.
*
Expand Down Expand Up @@ -539,7 +559,7 @@ void AsyncDownloadManager::readyReadHandler()

if(wr.isFailure())
{
// Close and delete file, finished handler will use this info to create correct report
// Close file, finished handler will use this state to create correct report
writer->close();
QFile::remove(writer->path());

Expand Down Expand Up @@ -701,19 +721,23 @@ void AsyncDownloadManager::downloadFinishedHandler(QNetworkReply* reply)
recordFinishedDownload(DownloadOpReport::failedDownload(task, reply->errorString()));
}

// Followup if needed
if(fail && mStopOnError && mStatus == Status::Downloading)
stopOnError();

// Cleanup writer
// Ensure writer is cleaned up
writer->close();

// Remove from active writers
mActiveWriters.remove(reply);

// Mark reply for deletion
reply->deleteLater();

// Followup on fail if needed
if(fail)
{
if(mDeletePartials)
QFile::remove(writer->path());

if(mStopOnError && mStatus == Status::Downloading)
stopOnError();
}

// Proceed on next loop iteration
QTimer::singleShot(0, this, &AsyncDownloadManager::pushDownloadsUntilFinished);
}
Expand Down

0 comments on commit 83a4f49

Please sign in to comment.