2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkData.hpp"
|
|
|
|
|
|
|
|
#include "singletons/Paths.hpp"
|
2018-07-16 17:23:41 +02:00
|
|
|
#include "util/DebugCount.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-07-16 17:23:41 +02:00
|
|
|
NetworkData::NetworkData()
|
|
|
|
{
|
|
|
|
DebugCount::increase("NetworkData");
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkData::~NetworkData()
|
|
|
|
{
|
|
|
|
DebugCount::decrease("NetworkData");
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
QString NetworkData::getHash()
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->hash_.isEmpty())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
QByteArray bytes;
|
|
|
|
|
|
|
|
bytes.append(this->request_.url().toString());
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &header : this->request_.rawHeaderList())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
bytes.append(header);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QByteArray hashBytes(
|
|
|
|
QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
this->hash_ = hashBytes.toHex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkData::writeToCache(const QByteArray &bytes)
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->useQuickLoadCache_)
|
|
|
|
{
|
2018-08-19 16:20:14 +02:00
|
|
|
QFile cachedFile(getPaths()->cacheDirectory() + "/" + this->getHash());
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (cachedFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
cachedFile.write(bytes);
|
|
|
|
|
|
|
|
cachedFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|