Skip to content
Permalink
Browse files

Fix path resolver with extra URL-like stuff

Like for CSVs: see #21150
  • Loading branch information
elpaso committed Feb 25, 2019
1 parent ed2254a commit 98bd6d651b40bcf096997bd0383eec68fb3ec04b
@@ -17,7 +17,7 @@ The QgsDefaultValue class provides a container for managing client
side default values for fields.

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

Usual values for such an expression are

@@ -18,6 +18,7 @@
#include "qgis.h"

#include <QFileInfo>
#include <QUrl>


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

// Strip "file://"
QFileInfo srcFileInfo( src.startsWith( QStringLiteral( "file://" ) ) ? src.mid( 7 ) : src );
QString srcPath = srcFileInfo.exists() ? srcFileInfo.canonicalFilePath() : src;
// Check if it is a plublicSource uri and clean it
QUrl url { src };
QString srcPath { src };
QString urlQuery;

if ( url.isLocalFile( ) )
{
srcPath = url.path();
urlQuery = url.query();
}

QFileInfo srcFileInfo( srcPath );
if ( srcFileInfo.exists() )
srcPath = srcFileInfo.canonicalFilePath();

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

return vsiPrefix + srcElems.join( QStringLiteral( "/" ) );
// Append url query if any
QString returnPath { vsiPrefix + srcElems.join( QStringLiteral( "/" ) ) };
if ( ! urlQuery.isEmpty() )
{
returnPath.append( '?' );
returnPath.append( urlQuery );
}
return returnPath;
}
@@ -42,6 +42,7 @@ class TestQgsProject : public QObject
void variablesChanged();
void testLayerFlags();
void testLocalFiles();
void testLocalUrlFiles();
};

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

}

void TestQgsProject::testLocalUrlFiles()
{
QTemporaryFile f;
QVERIFY( f.open() );
f.close();
QgsProject prj;
QFileInfo info( f.fileName() );
prj.setFileName( f.fileName() );
prj.write();
QString shpPath = info.dir().path() + '/' + info.baseName() + ".shp";
QString extraStuff {"?someVar=someValue&someOtherVar=someOtherValue" };
QString layerPath = "file://" + shpPath + extraStuff;
QFile f2( shpPath );
QVERIFY( f2.open( QFile::ReadWrite ) );
f2.close();
QgsPathResolver resolver( f.fileName( ) );
QCOMPARE( resolver.writePath( layerPath ), QString( "./" + info.baseName() + ".shp" + extraStuff ) ) ;

}


QGSTEST_MAIN( TestQgsProject )
#include "testqgsproject.moc"

0 comments on commit 98bd6d6

Please sign in to comment.
You can’t perform that action at this time.