Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Feature: mime_type expression function
Adds a mime_type( bytes ) expression function

Funded by: Today I didn't go skiing.
  • Loading branch information
elpaso authored and nyalldawson committed Feb 19, 2021
1 parent 7f0a9a9 commit 9489afe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions resources/function_help/json/mime_type
@@ -0,0 +1,9 @@
{
"name": "mime_type",
"type": "function",
"groups": ["General"],
"description": "Returns the mime type of the binary data.",
"arguments": [ {"arg":"bytes","description":"the binary data"}],
"examples": [ { "expression":"mime_type('<html><body></body></html>')", "returns":"text/html"},
{ "expression":"mime_type(from_base64('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAIAOw=='))", "returns":"image/gif"} ]
}
12 changes: 12 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -61,6 +61,7 @@
#include "qgsunittypes.h"
#include "qgsspatialindex.h"


typedef QList<QgsExpressionFunction *> ExpressionFunctionList;

Q_GLOBAL_STATIC( ExpressionFunctionList, sOwnedFunctions )
Expand Down Expand Up @@ -5075,6 +5076,13 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
return result;
}

static QVariant fcnMimeType( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * )
{
const QVariant data = values.at( 0 );
const QMimeDatabase db;
return db.mimeTypeForData( data.toByteArray() ).name();
}

static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
Expand Down Expand Up @@ -6955,6 +6963,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "part" ), true ),
fcnDecodeUri, QStringLiteral( "Map Layers" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "mime_type" ),
QgsExpressionFunction::ParameterList()
<< QgsExpressionFunction::Parameter( QStringLiteral( "binary_data" ) ),
fcnMimeType, QStringLiteral( "General" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "raster_statistic" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "band" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "statistic" ) ), fcnGetRasterBandStat, QStringLiteral( "Rasters" ) );
Expand Down
7 changes: 7 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1608,6 +1608,13 @@ class TestQgsExpression: public QObject

QTest::newRow( "decode_uri shp path" ) << QStringLiteral( "array_last(string_to_array(replace(decode_uri('%1', 'path'), '\\\\', '/'), '/'))" ).arg( mPointsLayer->name() ) << false << QVariant( "points.shp" );

// Mime type
QTest::newRow( "mime_type empty" ) << QStringLiteral( "mime_type('')" ) << false << QVariant( "application/x-zerosize" );
QTest::newRow( "mime_type ascii" ) << QStringLiteral( "mime_type('TEXT')" ) << false << QVariant( "text/plain" );
QTest::newRow( "mime_type gif" ) << QStringLiteral( "mime_type(from_base64('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAIAOw=='))" ) << false << QVariant( "image/gif" );
QTest::newRow( "mime_type pdf" ) << QStringLiteral( "mime_type(from_base64('JVBERi0xLgp0cmFpbGVyPDwvUm9vdDw8L1BhZ2VzPDwvS2lkc1s8PC9NZWRpYUJveFswIDAgMyAzXT4+XT4+Pj4+Pg=='))" ) << false << QVariant( "application/pdf" );
QTest::newRow( "mime_type html" ) << QStringLiteral( "mime_type('<html><body></body></html>')" ) << false << QVariant( "text/html" );

// raster_statistic tests
QTest::newRow( "raster_statistic no layer" ) << "raster_statistic('',1,'min')" << false << QVariant();
QTest::newRow( "raster_statistic bad layer" ) << "raster_statistic('bad',1,'min')" << false << QVariant();
Expand Down

0 comments on commit 9489afe

Please sign in to comment.