34 QString hash = rangeFileName( request );
35 QByteArray arr = readFile( hash );
41 QDir dir( mCacheDir );
42 return dir.exists( rangeFileName( request ) );
47 QString hash = rangeFileName( request );
48 writeFile( hash, data );
54 QDir dir( mCacheDir );
55 for ( QFileInfo info : dir.entryInfoList() )
57 removeFile( info.filePath() );
63 QString cachePath = path;
64 if ( !cachePath.endsWith( QDir::separator() ) )
66 cachePath.push_back( QDir::separator() );
68 mCacheDir = cachePath;
70 if ( !QDir().mkpath( mCacheDir ) )
72 mError = QObject::tr(
"Unable to create cache directory \"%1\"" ).arg( mCacheDir );
86 mMaxDataSize = cacheSize;
90QString QgsRangeRequestCache::rangeFileName(
const QNetworkRequest &request )
const
92 return mCacheDir + QStringLiteral(
"%1-%2" ).arg(
qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader(
"Range" ) ) );
95QByteArray QgsRangeRequestCache::readFile(
const QString &fileName )
97 QFile file( fileName );
98 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
100 mError = QObject::tr(
"Unable to read cache file \"%1\"" ).arg( fileName );
103 return file.readAll();
106bool QgsRangeRequestCache::writeFile(
const QString &fileName, QByteArray data )
108 QFile file( fileName );
109 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
111 mError = QObject::tr(
"Unable to open cache file \"%1\"" ).arg( fileName );
114 qint64 written = file.write( data );
116 if ( written != data.size() )
118 mError = QObject::tr(
"Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
124bool QgsRangeRequestCache::removeFile(
const QString &fileName )
126 if ( fileName.isEmpty() )
128 bool wasRemoved = QFile::remove( fileName );
131 mError = QObject::tr(
"Unable to remove cache file \"%1\"" ).arg( fileName );
136void QgsRangeRequestCache::expire()
138 QFileInfoList filesList = cacheEntries();
139 qint64 totalSize = 0;
140 for ( QFileInfo &info : filesList )
142 totalSize += info.size();
144 while ( totalSize > mMaxDataSize )
146 QFileInfo info = filesList.back();
147 filesList.pop_back();
148 totalSize -= info.size();
149 removeFile( info.filePath() );
153QFileInfoList QgsRangeRequestCache::cacheEntries()
155 QDir dir( mCacheDir );
156 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
157 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.