Skip to content

Commit 0d6dcb2

Browse files
committedMay 7, 2018
do not return a const file and add localFile helper
1 parent 648562d commit 0d6dcb2

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed
 

‎python/core/qgsnetworkcontentfetcherregistry.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ Create the registry for temporary downloaded files
120120
If the download starts immediately, it will not redownload any already fetched or currently fetching file.
121121
%End
122122

123+
QString localPath( const QString &filePathOrUrl );
124+
123125
};
124126

125127
/************************************************************************

‎src/core/qgsnetworkcontentfetcherregistry.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,32 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
101101
return content;
102102
}
103103

104+
QString QgsNetworkContentFetcherRegistry::localPath( const QString &filePathOrUrl )
105+
{
106+
QString path = filePathOrUrl;
107+
108+
if ( !QUrl::fromUserInput( filePathOrUrl ).isLocalFile() )
109+
{
110+
if ( mFileRegistry.contains( QUrl( path ) ) )
111+
{
112+
const QgsFetchedContent *content = mFileRegistry.value( QUrl( path ) );
113+
if ( content->status() == QgsFetchedContent::Finished && !content->filePath().isEmpty() )
114+
{
115+
path = content->filePath();
116+
}
117+
else
118+
{
119+
// if the file is not downloaded yet or has failed, return empty string
120+
path = QStringLiteral();
121+
}
122+
}
123+
else
124+
{
125+
// if registry doesn't contain the URL, keep path unchanged
126+
}
127+
}
128+
return path;
129+
}
130+
104131

105132

‎src/core/qgsnetworkcontentfetcherregistry.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CORE_EXPORT QgsFetchedContent : public QObject
5959

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

6565
//! 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
139139
*/
140140
const QgsFetchedContent *fetch( const QUrl &url, const FetchingMode &fetchingMode = DownloadLater );
141141

142+
QString localPath( const QString &filePathOrUrl );
143+
142144
private:
143145
QMap<QUrl, QgsFetchedContent *> mFileRegistry;
144146

‎tests/src/python/test_qgsnetworkcontentfetcherregistry.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ def check_reply():
7070
app.processEvents()
7171

7272
def testFetchGoodUrl(self):
73+
url = 'http://localhost:' + str(self.port) + '/qgis_local_server/index.html'
7374
registry = QgsApplication.networkContentFetcherRegistry()
74-
content = registry.fetch(QUrl('http://localhost:' + str(self.port) + '/qgis_local_server/index.html'))
75+
content = registry.fetch(QUrl(url))
7576
self.loaded = False
7677

7778
def check_reply():
@@ -85,8 +86,10 @@ def check_reply():
8586
while not self.loaded:
8687
app.processEvents()
8788

89+
self.assertEqual(registry.localPath(url), content.filePath())
90+
8891
# create new content with same URL
89-
contentV2 = registry.fetch(QUrl('http://localhost:' + str(self.port) + '/qgis_local_server/index.html'))
92+
contentV2 = registry.fetch(QUrl(url))
9093
self.assertEqual(contentV2.status(), QgsFetchedContent.Finished)
9194

9295
def testFetchReloadUrl(self):
@@ -124,6 +127,18 @@ def check_reply():
124127

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

130+
def testLocalPath(self):
131+
registry = QgsApplication.networkContentFetcherRegistry()
132+
filePath = 'qgis_local_server/index.html'
133+
self.assertEqual(registry.localPath(filePath), filePath)
134+
135+
# a non existent download shall return untouched the path
136+
self.assertEqual(registry.localPath('xxxx'), 'xxxx')
137+
138+
# an existent but unfinished download should return an empty path
139+
content = registry.fetch(QUrl('xxxx'))
140+
self.assertEqual(registry.localPath('xxxx'), '')
141+
127142

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

0 commit comments

Comments
 (0)
Please sign in to comment.