Skip to content

Commit

Permalink
Avoid temporary destination filename has "." inside. Fixes #36353
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed May 12, 2020
1 parent c6f1add commit b3d168f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -5016,13 +5016,19 @@ QString QgsProcessingDestinationParameter::asPythonString( const QgsProcessing::

QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
{
// sanitize name to avoid multiple . in the filename. E.g. when name() contain
// backend command name having a "." inside as in case of grass commands
QRegularExpression rx( QStringLiteral( "[.]" ) );
QString sanitizedName = name();
sanitizedName.replace( rx, QStringLiteral( "_" ) );

if ( defaultFileExtension().isEmpty() )
{
return QgsProcessingUtils::generateTempFilename( name() );
return QgsProcessingUtils::generateTempFilename( sanitizedName );
}
else
{
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
}
}

Expand Down
45 changes: 45 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QObject>
#include <QtTest/QSignalSpy>
#include <QList>
#include <QFileInfo>
#include "qgis.h"
#include "qgstest.h"
#include "qgsrasterlayer.h"
Expand Down Expand Up @@ -527,6 +528,7 @@ class TestQgsProcessing: public QObject
void features();
void uniqueValues();
void createIndex();
void generateTemporaryDestination();
void parseDestinationString();
void createFeatureSink();
void parameters();
Expand Down Expand Up @@ -1520,6 +1522,49 @@ void TestQgsProcessing::createIndex()
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );
}

void TestQgsProcessing::generateTemporaryDestination()
{
// setup a context
QgsProject p;
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
QgsProcessingContext context;
context.setProject( &p );

// destination vector with "." in it's name
std::unique_ptr< QgsProcessingParameterVectorDestination > def( new QgsProcessingParameterVectorDestination( "with.inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "gpkg"
QFileInfo f = QFileInfo( def->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );

// destination raster with "." in it's name
std::unique_ptr< QgsProcessingParameterRasterDestination > def2( new QgsProcessingParameterRasterDestination( "with.inside", QString(), QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def2->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def2->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "tif"
f = QFileInfo( def2->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "tif" ) );

// destination vector without "." in it's name
std::unique_ptr< QgsProcessingParameterVectorDestination > def3( new QgsProcessingParameterVectorDestination( "without_inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def3->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def3->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "gpkg"
f = QFileInfo( def3->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );

}

void TestQgsProcessing::parseDestinationString()
{
QString providerKey;
Expand Down

0 comments on commit b3d168f

Please sign in to comment.