MultiMC5/launcher/net/FileSink.cpp

115 lines
2.8 KiB
C++
Raw Normal View History

#include "FileSink.h"
#include <QFile>
#include <QFileInfo>
#include "FileSystem.h"
namespace Net {
FileSink::FileSink(QString filename)
2018-07-15 12:51:05 +00:00
:m_filename(filename)
{
2018-07-15 12:51:05 +00:00
// nil
}
FileSink::~FileSink()
{
2018-07-15 12:51:05 +00:00
// nil
}
JobStatus FileSink::init(QNetworkRequest& request)
{
2018-07-15 12:51:05 +00:00
auto result = initCache(request);
if(result != Job_InProgress)
{
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;
return Job_Failed;
}
wroteAnyData = false;
m_output_file.reset(new QSaveFile(m_filename));
if (!m_output_file->open(QIODevice::WriteOnly))
{
qCritical() << "Could not open " + m_filename + " for writing";
return Job_Failed;
}
2018-07-15 12:51:05 +00:00
if(initAllValidators(request))
return Job_InProgress;
return Job_Failed;
}
JobStatus FileSink::initCache(QNetworkRequest &)
{
2018-07-15 12:51:05 +00:00
return Job_InProgress;
}
JobStatus FileSink::write(QByteArray& data)
{
2018-07-15 12:51:05 +00: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;
return Job_Failed;
}
wroteAnyData = true;
return Job_InProgress;
}
JobStatus FileSink::abort()
{
2018-07-15 12:51:05 +00:00
m_output_file->cancelWriting();
failAllValidators();
return Job_Failed;
}
JobStatus FileSink::finalize(QNetworkReply& reply)
{
2018-07-15 12:51:05 +00: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;
}
// if we wrote any data to the save file, we try to commit the data to the real file.
// if it actually got a proper file, we write it even if it was empty
if (gotFile || wroteAnyData)
{
// ask validators for data consistency
// we only do this for actual downloads, not 'your data is still the same' cache hits
if(!finalizeAllValidators(reply))
return Job_Failed;
// nothing went wrong...
if (!m_output_file->commit())
{
qCritical() << "Failed to commit changes to " << m_filename;
m_output_file->cancelWriting();
return Job_Failed;
}
}
// then get rid of the save file
m_output_file.reset();
2018-07-15 12:51:05 +00:00
return finalizeCache(reply);
}
JobStatus FileSink::finalizeCache(QNetworkReply &)
{
2018-07-15 12:51:05 +00:00
return Job_Finished;
}
bool FileSink::hasLocalData()
{
2018-07-15 12:51:05 +00:00
QFileInfo info(m_filename);
return info.exists() && info.size() != 0;
}
}