28using namespace Qt::StringLiterals;
35 QString hash = rangeFileName( request );
36 QByteArray arr = readFile( hash );
42 QDir dir( mCacheDir );
43 return dir.exists( rangeFileName( request ) );
48 QString hash = rangeFileName( request );
49 writeFile( hash, data );
55 QDir dir( mCacheDir );
56 for ( QFileInfo info : dir.entryInfoList() )
58 removeFile( info.filePath() );
64 QString cachePath = path;
65 if ( !cachePath.endsWith( QDir::separator() ) )
67 cachePath.push_back( QDir::separator() );
69 mCacheDir = cachePath;
71 if ( !QDir().mkpath( mCacheDir ) )
73 mError = QObject::tr(
"Unable to create cache directory \"%1\"" ).arg( mCacheDir );
87 mMaxDataSize = cacheSize;
91QString QgsRangeRequestCache::rangeFileName(
const QNetworkRequest &request )
const
93 return mCacheDir + u
"%1-%2"_s.arg(
qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader(
"Range" ) ) );
96QByteArray QgsRangeRequestCache::readFile(
const QString &fileName )
98 QFile file( fileName );
99 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
101 mError = QObject::tr(
"Unable to read cache file \"%1\"" ).arg( fileName );
104 return file.readAll();
107bool QgsRangeRequestCache::writeFile(
const QString &fileName, QByteArray data )
109 QFile file( fileName );
110 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
112 mError = QObject::tr(
"Unable to open cache file \"%1\"" ).arg( fileName );
115 qint64 written = file.write( data );
117 if ( written != data.size() )
119 mError = QObject::tr(
"Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
125bool QgsRangeRequestCache::removeFile(
const QString &fileName )
127 if ( fileName.isEmpty() )
129 bool wasRemoved = QFile::remove( fileName );
132 mError = QObject::tr(
"Unable to remove cache file \"%1\"" ).arg( fileName );
137void QgsRangeRequestCache::expire()
139 QFileInfoList filesList = cacheEntries();
140 qint64 totalSize = 0;
141 for ( QFileInfo &info : filesList )
143 totalSize += info.size();
145 while ( totalSize > mMaxDataSize )
147 QFileInfo info = filesList.back();
148 filesList.pop_back();
149 totalSize -= info.size();
150 removeFile( info.filePath() );
154QFileInfoList QgsRangeRequestCache::cacheEntries()
156 QDir dir( mCacheDir );
157 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
158 std::sort( filesList.begin(), filesList.end(), []( QFileInfo &f1, QFileInfo &f2 ) {
159 QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
161 t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
162 QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
164 t2 = f2.fileTime( QFile::FileTime::FileBirthTime );
static qint64 smartCacheSize(const QString &path)
Returns a smart cache size, in bytes, based on available free space.
void clear()
Clears the cache removing all of the files.
bool hasEntry(const QNetworkRequest &request)
Checks whether the range request exists in the cache.
bool setCacheDirectory(const QString &path)
Set the Cache Directory object.
QByteArray entry(const QNetworkRequest &request)
Returns the range request data stored in the cache and an empty byte array if the data is not in the ...
void registerEntry(const QNetworkRequest &request, QByteArray data)
Adds the range request data into the cache.
void setCacheSize(qint64 maxBytes)
Sets the cache size.
uint qHash(const QVariant &variant)
Hash for QVariant.