28using namespace Qt::StringLiterals;
37 QString hash = rangeFileName( request );
38 QByteArray arr = readFile( hash );
44 QDir dir( mCacheDir );
45 return dir.exists( rangeFileName( request ) );
50 QString hash = rangeFileName( request );
51 writeFile( hash, data );
57 QDir dir( mCacheDir );
58 for ( QFileInfo info : dir.entryInfoList() )
60 removeFile( info.filePath() );
66 QString cachePath = path;
67 if ( !cachePath.endsWith( QDir::separator() ) )
69 cachePath.push_back( QDir::separator() );
71 mCacheDir = cachePath;
73 if ( !QDir().mkpath( mCacheDir ) )
75 mError = QObject::tr(
"Unable to create cache directory \"%1\"" ).arg( mCacheDir );
89 mMaxDataSize = cacheSize;
93QString QgsRangeRequestCache::rangeFileName(
const QNetworkRequest &request )
const
95 return mCacheDir + u
"%1-%2"_s.arg(
qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader(
"Range" ) ) );
98QByteArray QgsRangeRequestCache::readFile(
const QString &fileName )
100 QFile file( fileName );
101 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
103 mError = QObject::tr(
"Unable to read cache file \"%1\"" ).arg( fileName );
106 return file.readAll();
109bool QgsRangeRequestCache::writeFile(
const QString &fileName, QByteArray data )
111 QFile file( fileName );
112 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
114 mError = QObject::tr(
"Unable to open cache file \"%1\"" ).arg( fileName );
117 qint64 written = file.write( data );
119 if ( written != data.size() )
121 mError = QObject::tr(
"Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
127bool QgsRangeRequestCache::removeFile(
const QString &fileName )
129 if ( fileName.isEmpty() )
131 bool wasRemoved = QFile::remove( fileName );
134 mError = QObject::tr(
"Unable to remove cache file \"%1\"" ).arg( fileName );
139void QgsRangeRequestCache::expire()
141 QFileInfoList filesList = cacheEntries();
142 qint64 totalSize = 0;
143 for ( QFileInfo &info : filesList )
145 totalSize += info.size();
147 while ( totalSize > mMaxDataSize )
149 QFileInfo info = filesList.back();
150 filesList.pop_back();
151 totalSize -= info.size();
152 removeFile( info.filePath() );
156QFileInfoList QgsRangeRequestCache::cacheEntries()
158 QDir dir( mCacheDir );
159 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
160 std::sort( filesList.begin(), filesList.end(), []( QFileInfo & f1, QFileInfo & f2 )
162 QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
164 t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
165 QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
167 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.