32  QString hash = rangeFileName( request );
 
   33  QByteArray arr = readFile( hash );
 
 
   39  QDir dir( mCacheDir );
 
   40  return dir.exists( rangeFileName( request ) );
 
 
   45  QString hash = rangeFileName( request );
 
   46  writeFile( hash, data );
 
 
   52  QDir dir( mCacheDir );
 
   53  for ( QFileInfo info : dir.entryInfoList() )
 
   55    removeFile( info.filePath() );
 
 
   61  QString cachePath = path;
 
   62  if ( !cachePath.endsWith( QDir::separator() ) )
 
   64    cachePath.push_back( QDir::separator() );
 
   66  mCacheDir = cachePath;
 
   68  if ( !QDir().mkpath( mCacheDir ) )
 
   70    mError = QObject::tr( 
"Unable to create cache directory \"%1\"" ).arg( mCacheDir );
 
 
   78  mMaxDataSize = cacheSize;
 
 
   82QString QgsRangeRequestCache::rangeFileName( 
const QNetworkRequest &request )
 const 
   84  return mCacheDir + QStringLiteral( 
"%1-%2" ).arg( 
qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader( 
"Range" ) ) );
 
   87QByteArray QgsRangeRequestCache::readFile( 
const QString &fileName )
 
   89  QFile file( fileName );
 
   90  if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
 
   92    mError = QObject::tr( 
"Unable to read cache file \"%1\"" ).arg( fileName );
 
   95  return file.readAll();
 
   98bool QgsRangeRequestCache::writeFile( 
const QString &fileName, QByteArray data )
 
  100  QFile file( fileName );
 
  101  if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
 
  103    mError = QObject::tr( 
"Unable to open cache file \"%1\"" ).arg( fileName );
 
  106  qint64 written = file.write( data );
 
  108  if ( written != data.size() )
 
  110    mError = QObject::tr( 
"Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
 
  116bool QgsRangeRequestCache::removeFile( 
const QString &fileName )
 
  118  if ( fileName.isEmpty() )
 
  120  bool wasRemoved = QFile::remove( fileName );
 
  123    mError = QObject::tr( 
"Unable to remove cache file \"%1\"" ).arg( fileName );
 
  128void QgsRangeRequestCache::expire()
 
  130  QFileInfoList filesList = cacheEntries();
 
  131  qint64 totalSize = 0;
 
  132  for ( QFileInfo &info : filesList )
 
  134    totalSize += info.size();
 
  136  while ( totalSize > mMaxDataSize )
 
  138    QFileInfo info = filesList.back();
 
  139    filesList.pop_back();
 
  140    totalSize -= info.size();
 
  141    removeFile( info.filePath() );
 
  145QFileInfoList QgsRangeRequestCache::cacheEntries()
 
  147  QDir dir( mCacheDir );
 
  148  QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
 
  149  std::sort( filesList.begin(), filesList.end(), []( QFileInfo & f1, QFileInfo & f2 )
 
  151    QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
 
  153      t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
 
  154    QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
 
  156      t2 = f2.fileTime( QFile::FileTime::FileBirthTime );
 
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.
 
QgsRangeRequestCache()
Constructor.
 
void setCacheSize(qint64 maxBytes)
Sets the cache size.
 
uint qHash(const QVariant &variant)
Hash for QVariant.