2016-05-28 18:54:17 +01:00
|
|
|
#include "FileSink.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include "Env.h"
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
namespace Net {
|
|
|
|
|
|
|
|
FileSink::FileSink(QString filename)
|
|
|
|
:m_filename(filename)
|
|
|
|
{
|
|
|
|
// nil
|
|
|
|
};
|
|
|
|
|
|
|
|
FileSink::~FileSink()
|
|
|
|
{
|
|
|
|
// nil
|
|
|
|
};
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::init(QNetworkRequest& request)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
|
|
|
auto result = initCache(request);
|
2017-05-03 22:11:52 +01:00
|
|
|
if(result != Job_InProgress)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// create a new save file and open it for writing
|
|
|
|
if (!FS::ensureFilePathExists(m_filename))
|
|
|
|
{
|
|
|
|
qCritical() << "Could not create folder for " + m_filename;
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
2016-06-13 20:56:22 +01:00
|
|
|
wroteAnyData = false;
|
2016-05-28 18:54:17 +01:00
|
|
|
m_output_file.reset(new QSaveFile(m_filename));
|
|
|
|
if (!m_output_file->open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
qCritical() << "Could not open " + m_filename + " for writing";
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(initAllValidators(request))
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_InProgress;
|
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::initCache(QNetworkRequest &)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_InProgress;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::write(QByteArray& data)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
|
|
|
if (!writeAllValidators(data) || m_output_file->write(data) != data.size())
|
|
|
|
{
|
|
|
|
qCritical() << "Failed writing into " + m_filename;
|
|
|
|
m_output_file->cancelWriting();
|
|
|
|
m_output_file.reset();
|
|
|
|
wroteAnyData = false;
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
wroteAnyData = true;
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_InProgress;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::abort()
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
|
|
|
m_output_file->cancelWriting();
|
|
|
|
failAllValidators();
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::finalize(QNetworkReply& reply)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2017-05-09 02:08:38 +01:00
|
|
|
bool gotFile = false;
|
|
|
|
QVariant statusCodeV = reply.attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
bool validStatus = false;
|
|
|
|
int statusCode = statusCodeV.toInt(&validStatus);
|
|
|
|
if(validStatus)
|
|
|
|
{
|
|
|
|
// this leaves out 304 Not Modified
|
|
|
|
gotFile = statusCode == 200 || statusCode == 203;
|
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
// if we wrote any data to the save file, we try to commit the data to the real file.
|
2017-05-09 02:08:38 +01:00
|
|
|
// if it actually got a proper file, we write it even if it was empty
|
|
|
|
if (gotFile || wroteAnyData)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
|
|
|
// ask validators for data consistency
|
|
|
|
// we only do this for actual downloads, not 'your data is still the same' cache hits
|
|
|
|
if(!finalizeAllValidators(reply))
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
// nothing went wrong...
|
|
|
|
if (!m_output_file->commit())
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to commit changes to " << m_filename;
|
|
|
|
m_output_file->cancelWriting();
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// then get rid of the save file
|
|
|
|
m_output_file.reset();
|
|
|
|
|
|
|
|
return finalizeCache(reply);
|
|
|
|
}
|
|
|
|
|
2017-05-03 22:11:52 +01:00
|
|
|
JobStatus FileSink::finalizeCache(QNetworkReply &)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2017-05-03 22:11:52 +01:00
|
|
|
return Job_Finished;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 01:19:19 +01:00
|
|
|
bool FileSink::hasLocalData()
|
|
|
|
{
|
|
|
|
QFileInfo info(m_filename);
|
|
|
|
return info.exists() && info.size() != 0;
|
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|