2016-05-28 18:54:17 +01:00
|
|
|
#include "MetaCacheSink.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include "FileSystem.h"
|
2021-11-21 22:21:12 +00:00
|
|
|
#include "Application.h"
|
2016-05-28 18:54:17 +01:00
|
|
|
|
|
|
|
namespace Net {
|
|
|
|
|
|
|
|
MetaCacheSink::MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum)
|
2018-07-15 13:51:05 +01:00
|
|
|
:Net::FileSink(entry->getFullPath()), m_entry(entry), m_md5Node(md5sum)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
addValidator(md5sum);
|
2018-06-28 22:18:45 +01:00
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State MetaCacheSink::initCache(QNetworkRequest& request)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!m_entry->isStale())
|
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Succeeded;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// check if file exists, if it does, use its information for the request
|
|
|
|
QFile current(m_filename);
|
|
|
|
if(current.exists() && current.size() != 0)
|
|
|
|
{
|
|
|
|
if (m_entry->getRemoteChangedTimestamp().size())
|
|
|
|
{
|
|
|
|
request.setRawHeader(QString("If-Modified-Since").toLatin1(), m_entry->getRemoteChangedTimestamp().toLatin1());
|
|
|
|
}
|
|
|
|
if (m_entry->getETag().size())
|
|
|
|
{
|
|
|
|
request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1());
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
|
|
|
|
return Task::State::Running;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State MetaCacheSink::finalizeCache(QNetworkReply & reply)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo output_file_info(m_filename);
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if(wroteAnyData)
|
|
|
|
{
|
|
|
|
m_entry->setMD5Sum(m_md5Node->hash().toHex().constData());
|
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
m_entry->setETag(reply.rawHeader("ETag").constData());
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (reply.hasRawHeader("Last-Modified"))
|
|
|
|
{
|
|
|
|
m_entry->setRemoteChangedTimestamp(reply.rawHeader("Last-Modified").constData());
|
|
|
|
}
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch());
|
|
|
|
m_entry->setStale(false);
|
2021-11-21 22:21:12 +00:00
|
|
|
APPLICATION->metacache()->updateEntry(m_entry);
|
2022-04-22 02:12:14 +01:00
|
|
|
|
|
|
|
return Task::State::Succeeded;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
2016-10-28 01:19:19 +01:00
|
|
|
|
|
|
|
bool MetaCacheSink::hasLocalData()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo info(m_filename);
|
|
|
|
return info.exists() && info.size() != 0;
|
2016-10-28 01:19:19 +01:00
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|