Skip to content

Commit 98bd6d6

Browse files
committed
Fix path resolver with extra URL-like stuff
Like for CSVs: see #21150
1 parent ed2254a commit 98bd6d6

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

python/core/auto_generated/qgsdefaultvalue.sip.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The QgsDefaultValue class provides a container for managing client
1717
side default values for fields.
1818

1919
A QgsDefaultValue consists of an expression string that will be evaluated
20-
on the client when a defeault field value needs to be generated.
20+
on the client when a default field value needs to be generated.
2121

2222
Usual values for such an expression are
2323

src/core/qgspathresolver.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgis.h"
1919

2020
#include <QFileInfo>
21+
#include <QUrl>
2122

2223

2324
QgsPathResolver::QgsPathResolver( const QString &baseFileName )
@@ -163,9 +164,20 @@ QString QgsPathResolver::writePath( const QString &src ) const
163164
return src;
164165
}
165166

166-
// Strip "file://"
167-
QFileInfo srcFileInfo( src.startsWith( QStringLiteral( "file://" ) ) ? src.mid( 7 ) : src );
168-
QString srcPath = srcFileInfo.exists() ? srcFileInfo.canonicalFilePath() : src;
167+
// Check if it is a plublicSource uri and clean it
168+
QUrl url { src };
169+
QString srcPath { src };
170+
QString urlQuery;
171+
172+
if ( url.isLocalFile( ) )
173+
{
174+
srcPath = url.path();
175+
urlQuery = url.query();
176+
}
177+
178+
QFileInfo srcFileInfo( srcPath );
179+
if ( srcFileInfo.exists() )
180+
srcPath = srcFileInfo.canonicalFilePath();
169181

170182
// if this is a VSIFILE, remove the VSI prefix and append to final result
171183
QString vsiPrefix = qgsVsiPrefix( src );
@@ -233,5 +245,12 @@ QString QgsPathResolver::writePath( const QString &src ) const
233245
srcElems.insert( 0, QStringLiteral( "." ) );
234246
}
235247

236-
return vsiPrefix + srcElems.join( QStringLiteral( "/" ) );
248+
// Append url query if any
249+
QString returnPath { vsiPrefix + srcElems.join( QStringLiteral( "/" ) ) };
250+
if ( ! urlQuery.isEmpty() )
251+
{
252+
returnPath.append( '?' );
253+
returnPath.append( urlQuery );
254+
}
255+
return returnPath;
237256
}

tests/src/core/testqgsproject.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestQgsProject : public QObject
4242
void variablesChanged();
4343
void testLayerFlags();
4444
void testLocalFiles();
45+
void testLocalUrlFiles();
4546
};
4647

4748
void TestQgsProject::init()
@@ -406,6 +407,26 @@ void TestQgsProject::testLocalFiles()
406407

407408
}
408409

410+
void TestQgsProject::testLocalUrlFiles()
411+
{
412+
QTemporaryFile f;
413+
QVERIFY( f.open() );
414+
f.close();
415+
QgsProject prj;
416+
QFileInfo info( f.fileName() );
417+
prj.setFileName( f.fileName() );
418+
prj.write();
419+
QString shpPath = info.dir().path() + '/' + info.baseName() + ".shp";
420+
QString extraStuff {"?someVar=someValue&someOtherVar=someOtherValue" };
421+
QString layerPath = "file://" + shpPath + extraStuff;
422+
QFile f2( shpPath );
423+
QVERIFY( f2.open( QFile::ReadWrite ) );
424+
f2.close();
425+
QgsPathResolver resolver( f.fileName( ) );
426+
QCOMPARE( resolver.writePath( layerPath ), QString( "./" + info.baseName() + ".shp" + extraStuff ) ) ;
427+
428+
}
429+
409430

410431
QGSTEST_MAIN( TestQgsProject )
411432
#include "testqgsproject.moc"

0 commit comments

Comments
 (0)