Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Infer file extension from content-type header
  • Loading branch information
Djedouas authored and github-actions[bot] committed Jan 24, 2023
1 parent 2033f15 commit a5d78ef
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/core/network/qgsnetworkcontentfetcherregistry.cpp
Expand Up @@ -22,6 +22,8 @@
#include <QUrl>
#include <QFileInfo>
#include <QDir>
#include <QMimeType>
#include <QMimeDatabase>

QgsNetworkContentFetcherRegistry::~QgsNetworkContentFetcherRegistry()
{
Expand Down Expand Up @@ -167,9 +169,27 @@ void QgsFetchedContent::taskCompleted()
QNetworkReply *reply = mFetchingTask->reply();
if ( reply->error() == QNetworkReply::NoError )
{
// keep extension, it can be useful when guessing file content

// keep or guess extension, it can be useful when guessing file content
// (when loading this file in a Qt WebView for instance)
const QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();

// extension from file name
QString extension = QFileInfo( reply->request().url().fileName() ).completeSuffix();

// extension from contentType header if not found from file name
const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
if ( extension.isEmpty() && !contentType.isEmpty() )
{
QList<QMimeType> mimeTypes = QMimeDatabase().allMimeTypes();
auto it = std::find_if( mimeTypes.constBegin(), mimeTypes.constEnd(), [ = ]( QMimeType mimeType )
{
return mimeType.name() == contentType;
} );
if ( it != mimeTypes.end() )
{
extension = ( *it ).preferredSuffix();
}
}

QTemporaryFile *tf = new QTemporaryFile( extension.isEmpty() ? QString( "XXXXXX" ) :
QString( "%1/XXXXXX.%2" ).arg( QDir::tempPath(), extension ) );
Expand Down

0 comments on commit a5d78ef

Please sign in to comment.