Skip to content

Commit b3d168f

Browse files
github-actions[bot]nyalldawson
authored andcommittedMay 12, 2020
Avoid temporary destination filename has "." inside. Fixes #36353
1 parent c6f1add commit b3d168f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed
 

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,13 +5016,19 @@ QString QgsProcessingDestinationParameter::asPythonString( const QgsProcessing::
50165016

50175017
QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
50185018
{
5019+
// sanitize name to avoid multiple . in the filename. E.g. when name() contain
5020+
// backend command name having a "." inside as in case of grass commands
5021+
QRegularExpression rx( QStringLiteral( "[.]" ) );
5022+
QString sanitizedName = name();
5023+
sanitizedName.replace( rx, QStringLiteral( "_" ) );
5024+
50195025
if ( defaultFileExtension().isEmpty() )
50205026
{
5021-
return QgsProcessingUtils::generateTempFilename( name() );
5027+
return QgsProcessingUtils::generateTempFilename( sanitizedName );
50225028
}
50235029
else
50245030
{
5025-
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
5031+
return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
50265032
}
50275033
}
50285034

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QObject>
2727
#include <QtTest/QSignalSpy>
2828
#include <QList>
29+
#include <QFileInfo>
2930
#include "qgis.h"
3031
#include "qgstest.h"
3132
#include "qgsrasterlayer.h"
@@ -527,6 +528,7 @@ class TestQgsProcessing: public QObject
527528
void features();
528529
void uniqueValues();
529530
void createIndex();
531+
void generateTemporaryDestination();
530532
void parseDestinationString();
531533
void createFeatureSink();
532534
void parameters();
@@ -1520,6 +1522,49 @@ void TestQgsProcessing::createIndex()
15201522
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );
15211523
}
15221524

1525+
void TestQgsProcessing::generateTemporaryDestination()
1526+
{
1527+
// setup a context
1528+
QgsProject p;
1529+
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
1530+
QgsProcessingContext context;
1531+
context.setProject( &p );
1532+
1533+
// destination vector with "." in it's name
1534+
std::unique_ptr< QgsProcessingParameterVectorDestination > def( new QgsProcessingParameterVectorDestination( "with.inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );
1535+
1536+
// check that temporary destination does not have dot at the end when there is no extension
1537+
QVERIFY( !def->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
1538+
// check that temporary destination starts with tempFolder
1539+
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
1540+
// check that extension with QFileInfo::completeSuffix is "gpkg"
1541+
QFileInfo f = QFileInfo( def->generateTemporaryDestination() );
1542+
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );
1543+
1544+
// destination raster with "." in it's name
1545+
std::unique_ptr< QgsProcessingParameterRasterDestination > def2( new QgsProcessingParameterRasterDestination( "with.inside", QString(), QString(), false ) );
1546+
1547+
// check that temporary destination does not have dot at the end when there is no extension
1548+
QVERIFY( !def2->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
1549+
// check that temporary destination starts with tempFolder
1550+
QVERIFY( def2->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
1551+
// check that extension with QFileInfo::completeSuffix is "tif"
1552+
f = QFileInfo( def2->generateTemporaryDestination() );
1553+
QCOMPARE( f.completeSuffix(), QString( "tif" ) );
1554+
1555+
// destination vector without "." in it's name
1556+
std::unique_ptr< QgsProcessingParameterVectorDestination > def3( new QgsProcessingParameterVectorDestination( "without_inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );
1557+
1558+
// check that temporary destination does not have dot at the end when there is no extension
1559+
QVERIFY( !def3->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
1560+
// check that temporary destination starts with tempFolder
1561+
QVERIFY( def3->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
1562+
// check that extension with QFileInfo::completeSuffix is "gpkg"
1563+
f = QFileInfo( def3->generateTemporaryDestination() );
1564+
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );
1565+
1566+
}
1567+
15231568
void TestQgsProcessing::parseDestinationString()
15241569
{
15251570
QString providerKey;

0 commit comments

Comments
 (0)