Skip to content
Permalink
Browse files

do not return a const file and add localFile helper

  • Loading branch information
3nids committed Apr 27, 2018
1 parent 648562d commit 0d6dcb231e943903ae83a7c04745e5d15a28a6e0
@@ -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 );

};

/************************************************************************
@@ -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;
}



@@ -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.
@@ -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;

@@ -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():
@@ -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):
@@ -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.
You can’t perform that action at this time.