33 QString hash = rangeFileName( request );
34 QByteArray arr = readFile( hash );
40 QDir dir( mCacheDir );
41 return dir.exists( rangeFileName( request ) );
46 QString hash = rangeFileName( request );
47 writeFile( hash, data );
53 QDir dir( mCacheDir );
54 for ( QFileInfo info : dir.entryInfoList() )
56 removeFile( info.filePath() );
62 QString cachePath = path;
63 if ( !cachePath.endsWith( QDir::separator() ) )
65 cachePath.push_back( QDir::separator() );
67 mCacheDir = cachePath;
69 if ( !QDir().mkpath( mCacheDir ) )
71 mError = QObject::tr(
"Unable to create cache directory \"%1\"" ).arg( mCacheDir );
85 mMaxDataSize = cacheSize;
89QString QgsRangeRequestCache::rangeFileName(
const QNetworkRequest &request )
const
91 return mCacheDir + QStringLiteral(
"%1-%2" ).arg(
qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader(
"Range" ) ) );
94QByteArray QgsRangeRequestCache::readFile(
const QString &fileName )
96 QFile file( fileName );
97 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
99 mError = QObject::tr(
"Unable to read cache file \"%1\"" ).arg( fileName );
102 return file.readAll();
105bool QgsRangeRequestCache::writeFile(
const QString &fileName, QByteArray data )
107 QFile file( fileName );
108 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
110 mError = QObject::tr(
"Unable to open cache file \"%1\"" ).arg( fileName );
113 qint64 written = file.write( data );
115 if ( written != data.size() )
117 mError = QObject::tr(
"Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
123bool QgsRangeRequestCache::removeFile(
const QString &fileName )
125 if ( fileName.isEmpty() )
127 bool wasRemoved = QFile::remove( fileName );
130 mError = QObject::tr(
"Unable to remove cache file \"%1\"" ).arg( fileName );
135void QgsRangeRequestCache::expire()
137 QFileInfoList filesList = cacheEntries();
138 qint64 totalSize = 0;
139 for ( QFileInfo &info : filesList )
141 totalSize += info.size();
143 while ( totalSize > mMaxDataSize )
145 QFileInfo info = filesList.back();
146 filesList.pop_back();
147 totalSize -= info.size();
148 removeFile( info.filePath() );
152QFileInfoList QgsRangeRequestCache::cacheEntries()
154 QDir dir( mCacheDir );
155 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
156 std::sort( filesList.begin(), filesList.end(), []( QFileInfo & f1, QFileInfo & f2 )
158 QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
160 t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
161 QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
163 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.