mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-13 00:33:09 -07:00
Improve DownloadManager.
Now we can use downloaded data directly without saving to file. We also can disable redirection to Magnet URI handling (useful for non-torrent files downloading).
This commit is contained in:
parent
336519b7b5
commit
c702a7e426
11 changed files with 45 additions and 27 deletions
|
@ -46,11 +46,13 @@ static QString errorCodeToString(QNetworkReply::NetworkError status);
|
|||
|
||||
using namespace Net;
|
||||
|
||||
DownloadHandler::DownloadHandler(QNetworkReply *reply, DownloadManager *manager, qint64 limit)
|
||||
DownloadHandler::DownloadHandler(QNetworkReply *reply, DownloadManager *manager, bool saveToFile, qint64 limit, bool handleRedirectToMagnet)
|
||||
: QObject(manager)
|
||||
, m_reply(reply)
|
||||
, m_manager(manager)
|
||||
, m_saveToFile(saveToFile)
|
||||
, m_sizeLimit(limit)
|
||||
, m_handleRedirectToMagnet(handleRedirectToMagnet)
|
||||
, m_url(reply->url().toString())
|
||||
{
|
||||
init();
|
||||
|
@ -88,11 +90,23 @@ void DownloadHandler::processFinishedDownload()
|
|||
}
|
||||
else {
|
||||
// Success
|
||||
QString filePath;
|
||||
if (saveToFile(filePath))
|
||||
emit downloadFinished(m_url, filePath);
|
||||
else
|
||||
emit downloadFailed(m_url, tr("I/O Error"));
|
||||
QByteArray replyData = m_reply->readAll();
|
||||
if (m_reply->rawHeader("Content-Encoding") == "gzip") {
|
||||
// uncompress gzip reply
|
||||
Utils::Gzip::uncompress(replyData, replyData);
|
||||
}
|
||||
|
||||
if (m_saveToFile) {
|
||||
QString filePath;
|
||||
if (saveToFile(replyData, filePath))
|
||||
emit downloadFinished(m_url, filePath);
|
||||
else
|
||||
emit downloadFailed(m_url, tr("I/O Error"));
|
||||
}
|
||||
else {
|
||||
emit downloadFinished(m_url, replyData);
|
||||
}
|
||||
|
||||
this->deleteLater();
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +140,7 @@ void DownloadHandler::init()
|
|||
connect(m_reply, SIGNAL(finished()), this, SLOT(processFinishedDownload()));
|
||||
}
|
||||
|
||||
bool DownloadHandler::saveToFile(QString &filePath)
|
||||
bool DownloadHandler::saveToFile(const QByteArray &replyData, QString &filePath)
|
||||
{
|
||||
QTemporaryFile *tmpfile = new QTemporaryFile;
|
||||
if (!tmpfile->open()) {
|
||||
|
@ -138,11 +152,6 @@ bool DownloadHandler::saveToFile(QString &filePath)
|
|||
filePath = tmpfile->fileName();
|
||||
qDebug("Temporary filename is: %s", qPrintable(filePath));
|
||||
if (m_reply->isOpen() || m_reply->open(QIODevice::ReadOnly)) {
|
||||
QByteArray replyData = m_reply->readAll();
|
||||
if (m_reply->rawHeader("Content-Encoding") == "gzip") {
|
||||
// uncompress gzip reply
|
||||
Utils::Gzip::uncompress(replyData, replyData);
|
||||
}
|
||||
tmpfile->write(replyData);
|
||||
tmpfile->close();
|
||||
// XXX: tmpfile needs to be deleted on Windows before using the file
|
||||
|
@ -171,14 +180,20 @@ void DownloadHandler::handleRedirection(QUrl newUrl)
|
|||
if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
|
||||
qDebug("Magnet redirect detected.");
|
||||
m_reply->abort();
|
||||
emit redirectedToMagnet(m_url, newUrlString);
|
||||
if (m_handleRedirectToMagnet)
|
||||
emit redirectedToMagnet(m_url, newUrlString);
|
||||
else
|
||||
emit downloadFailed(m_url, tr("Unexpected redirect to magnet URI."));
|
||||
|
||||
this->deleteLater();
|
||||
}
|
||||
else {
|
||||
DownloadHandler *tmp = m_manager->downloadUrl(newUrlString, m_sizeLimit);
|
||||
m_reply->deleteLater();
|
||||
m_reply = tmp->m_reply;
|
||||
m_saveToFile = tmp->m_saveToFile;
|
||||
m_sizeLimit = tmp->m_sizeLimit;
|
||||
m_handleRedirectToMagnet = tmp->m_handleRedirectToMagnet;
|
||||
init();
|
||||
tmp->m_reply = 0;
|
||||
delete tmp;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue