2016-05-28 23:24:17 +05:30
|
|
|
#include "FileSink.h"
|
2022-04-22 06:42:14 +05:30
|
|
|
|
2016-05-28 23:24:17 +05:30
|
|
|
#include <QFile>
|
2022-04-22 06:42:14 +05:30
|
|
|
|
2016-05-28 23:24:17 +05:30
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
namespace Net {
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::init(QNetworkRequest& request)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto result = initCache(request);
|
2022-04-22 06:42:14 +05:30
|
|
|
if(result != Task::State::Running)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
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;
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
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";
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
if(initAllValidators(request))
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::initCache(QNetworkRequest &)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Running;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::write(QByteArray& data)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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;
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
wroteAnyData = true;
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Running;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::abort()
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_output_file->cancelWriting();
|
|
|
|
failAllValidators();
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::finalize(QNetworkReply& reply)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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))
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
// nothing went wrong...
|
|
|
|
if (!m_output_file->commit())
|
|
|
|
{
|
|
|
|
qCritical() << "Failed to commit changes to " << m_filename;
|
|
|
|
m_output_file->cancelWriting();
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
// then get rid of the save file
|
|
|
|
m_output_file.reset();
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
return finalizeCache(reply);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
Task::State FileSink::finalizeCache(QNetworkReply &)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Succeeded;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:19 +05:30
|
|
|
bool FileSink::hasLocalData()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QFileInfo info(m_filename);
|
|
|
|
return info.exists() && info.size() != 0;
|
2016-10-28 05:49:19 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|