summaryrefslogtreecommitdiffstats
path: root/logic/net
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2014-01-10 22:08:00 +0100
committerPetr Mrázek <peterix@gmail.com>2014-01-10 22:08:00 +0100
commit8e286c2b5c432e972df2e94b01481bbe094e464c (patch)
tree2fcf2151cd65057efd3ac215e8c944f00959e6ea /logic/net
parent9ddf2aec31e33e8e0fa450d0084f115471401ad4 (diff)
downloadMultiMC-8e286c2b5c432e972df2e94b01481bbe094e464c.tar
MultiMC-8e286c2b5c432e972df2e94b01481bbe094e464c.tar.gz
MultiMC-8e286c2b5c432e972df2e94b01481bbe094e464c.tar.lz
MultiMC-8e286c2b5c432e972df2e94b01481bbe094e464c.tar.xz
MultiMC-8e286c2b5c432e972df2e94b01481bbe094e464c.zip
Make CacheDownload use QSaveFile
Diffstat (limited to 'logic/net')
-rw-r--r--logic/net/CacheDownload.cpp48
-rw-r--r--logic/net/CacheDownload.h6
2 files changed, 24 insertions, 30 deletions
diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp
index 6eadae39..17fd7f2a 100644
--- a/logic/net/CacheDownload.cpp
+++ b/logic/net/CacheDownload.cpp
@@ -42,6 +42,13 @@ void CacheDownload::start()
// if there already is a file and md5 checking is in effect and it can be opened
if (!ensureFilePathExists(m_target_path))
{
+ QLOG_ERROR() << "Could not create folder for " + m_target_path;
+ emit failed(m_index_within_job);
+ return;
+ }
+ if(!m_output_file.open(QIODevice::WriteOnly))
+ {
+ QLOG_ERROR() << "Could not open " + m_target_path + " for writing";
emit failed(m_index_within_job);
return;
}
@@ -85,26 +92,21 @@ void CacheDownload::downloadFinished()
// if the download succeeded
if (m_status != Job_Failed)
{
-
// nothing went wrong...
m_status = Job_Finished;
- if (m_output_file.isOpen())
+ if (m_output_file.commit())
{
- // save the data to the downloadable if we aren't saving to file
- m_output_file.close();
m_entry->md5sum = md5sum.result().toHex().constData();
}
else
{
- if (m_output_file.open(QIODevice::ReadOnly))
- {
- m_entry->md5sum =
- QCryptographicHash::hash(m_output_file.readAll(), QCryptographicHash::Md5)
- .toHex()
- .constData();
- m_output_file.close();
- }
+ QLOG_ERROR() << "Failed to commit changes to " << m_target_path;
+ m_output_file.cancelWriting();
+ m_reply.reset();
+ emit failed(m_index_within_job);
+ return;
}
+
QFileInfo output_file_info(m_target_path);
m_entry->etag = m_reply->rawHeader("ETag").constData();
@@ -124,8 +126,7 @@ void CacheDownload::downloadFinished()
// else the download failed
else
{
- m_output_file.close();
- m_output_file.remove();
+ m_output_file.cancelWriting();
m_reply.reset();
emit failed(m_index_within_job);
return;
@@ -134,19 +135,12 @@ void CacheDownload::downloadFinished()
void CacheDownload::downloadReadyRead()
{
- if (!m_output_file.isOpen())
- {
- if (!m_output_file.open(QIODevice::WriteOnly))
- {
- /*
- * Can't open the file... the job failed
- */
- m_reply->abort();
- emit failed(m_index_within_job);
- return;
- }
- }
QByteArray ba = m_reply->readAll();
md5sum.addData(ba);
- m_output_file.write(ba);
+ if(m_output_file.write(ba) != ba.size())
+ {
+ QLOG_ERROR() << "Failed writing into " + m_target_path;
+ m_reply->abort();
+ emit failed(m_index_within_job);
+ }
}
diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h
index e25aecd2..921e231b 100644
--- a/logic/net/CacheDownload.h
+++ b/logic/net/CacheDownload.h
@@ -17,8 +17,8 @@
#include "NetAction.h"
#include "HttpMetaCache.h"
-#include <QFile>
-#include <qcryptographichash.h>
+#include <QCryptographicHash>
+#include <QSaveFile>
typedef std::shared_ptr<class CacheDownload> CacheDownloadPtr;
class CacheDownload : public NetAction
@@ -29,7 +29,7 @@ public:
/// if saving to file, use the one specified in this string
QString m_target_path;
/// this is the output file, if any
- QFile m_output_file;
+ QSaveFile m_output_file;
/// the hash-as-you-download
QCryptographicHash md5sum;