do not return a const file and add localFile helper

This commit is contained in:
Denis Rouzaud 2018-04-27 14:45:24 -04:00
parent 648562d2b7
commit 0d6dcb231e
4 changed files with 49 additions and 3 deletions

View File

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

View File

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

View File

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

View File

@ -70,8 +70,9 @@ class TestQgsNetworkContentFetcherTask(unittest.TestCase):
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 @@ class TestQgsNetworkContentFetcherTask(unittest.TestCase):
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 @@ class TestQgsNetworkContentFetcherTask(unittest.TestCase):
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()