Skip to content

Commit

Permalink
do not return a const file and add localFile helper
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 7, 2018
1 parent 648562d commit 0d6dcb2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/qgsnetworkcontentfetcherregistry.sip.in
Expand Up @@ -120,6 +120,8 @@ Create the registry for temporary downloaded files
If the download starts immediately, it will not redownload any already fetched or currently fetching file.
%End

QString localPath( const QString &filePathOrUrl );

};

/************************************************************************
Expand Down
27 changes: 27 additions & 0 deletions src/core/qgsnetworkcontentfetcherregistry.cpp
Expand Up @@ -101,5 +101,32 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
return content;
}

QString QgsNetworkContentFetcherRegistry::localPath( const QString &filePathOrUrl )
{
QString path = filePathOrUrl;

if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
{
if ( mFileRegistry.contains( QUrl( path ) ) )
{
const QgsFetchedContent *content = mFileRegistry.value( QUrl( path ) );
if ( content->status() == QgsFetchedContent::Finished && !content->filePath().isEmpty() )
{
path = content->filePath();
}
else
{
// if the file is not downloaded yet or has failed, return empty string
path = QStringLiteral();
}
}
else
{
// if registry doesn't contain the URL, keep path unchanged
}
}
return path;
}



4 changes: 3 additions & 1 deletion src/core/qgsnetworkcontentfetcherregistry.h
Expand Up @@ -59,7 +59,7 @@ class CORE_EXPORT QgsFetchedContent : public QObject

#ifndef SIP_RUN
//! Return a pointer to the local file, a null pointer if the file is not accessible yet.
const QFile *file() const {return mFile;}
QFile *file() const {return mFile;}
#endif

//! Return the path to the local file, an empty string if the file is not accessible yet.
Expand Down Expand Up @@ -139,6 +139,8 @@ class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
*/
const QgsFetchedContent *fetch( const QUrl &url, const FetchingMode &fetchingMode = DownloadLater );

QString localPath( const QString &filePathOrUrl );

private:
QMap<QUrl, QgsFetchedContent *> mFileRegistry;

Expand Down
19 changes: 17 additions & 2 deletions tests/src/python/test_qgsnetworkcontentfetcherregistry.py
Expand Up @@ -70,8 +70,9 @@ def check_reply():
app.processEvents()

def testFetchGoodUrl(self):
url = 'http://localhost:' + str(self.port) + '/qgis_local_server/index.html'
registry = QgsApplication.networkContentFetcherRegistry()
content = registry.fetch(QUrl('http://localhost:' + str(self.port) + '/qgis_local_server/index.html'))
content = registry.fetch(QUrl(url))
self.loaded = False

def check_reply():
Expand All @@ -85,8 +86,10 @@ def check_reply():
while not self.loaded:
app.processEvents()

self.assertEqual(registry.localPath(url), content.filePath())

# create new content with same URL
contentV2 = registry.fetch(QUrl('http://localhost:' + str(self.port) + '/qgis_local_server/index.html'))
contentV2 = registry.fetch(QUrl(url))
self.assertEqual(contentV2.status(), QgsFetchedContent.Finished)

def testFetchReloadUrl(self):
Expand Down Expand Up @@ -124,6 +127,18 @@ def check_reply():

os.remove('qgis_local_server/simple_content.txt')

def testLocalPath(self):
registry = QgsApplication.networkContentFetcherRegistry()
filePath = 'qgis_local_server/index.html'
self.assertEqual(registry.localPath(filePath), filePath)

# a non existent download shall return untouched the path
self.assertEqual(registry.localPath('xxxx'), 'xxxx')

# an existent but unfinished download should return an empty path
content = registry.fetch(QUrl('xxxx'))
self.assertEqual(registry.localPath('xxxx'), '')


if __name__ == "__main__":
unittest.main()

0 comments on commit 0d6dcb2

Please sign in to comment.