Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import URL eating tons of memory on large file downloads #5826

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/core/appinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QFileInfo>
#include <QImageReader>
#include <QQuickItem>
#include <QTemporaryFile>
#include <qgsapplication.h>
#include <qgsmessagelog.h>
#include <qgsproject.h>
Expand Down Expand Up @@ -278,6 +279,15 @@ void AppInterface::importUrl( const QString &url )
sanitizedUrl = QStringLiteral( "https://%1" ).arg( sanitizedUrl );
}

const QString applicationDirectory = PlatformUtilities::instance()->applicationDirectory();
if ( applicationDirectory.isEmpty() )
return;

QTemporaryFile *temporaryFile = new QTemporaryFile();
temporaryFile->setFileTemplate( QStringLiteral( "%1/XXXXXXXXXXXX" ).arg( applicationDirectory ) );
temporaryFile->setAutoRemove( false );
temporaryFile->open();

QgsNetworkAccessManager *manager = QgsNetworkAccessManager::instance();
QNetworkRequest request( ( QUrl( sanitizedUrl ) ) );
request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy );
Expand All @@ -286,15 +296,18 @@ void AppInterface::importUrl( const QString &url )

QNetworkReply *reply = manager->get( request );
connect( reply, &QNetworkReply::downloadProgress, this, [=]( int bytesReceived, int bytesTotal ) {
temporaryFile->write( reply->readAll() );
if ( bytesTotal != 0 )
{
emit importProgress( static_cast<double>( bytesReceived ) / bytesTotal );
}
} );

connect( reply, &QNetworkReply::finished, this, [=]() {
m-kuhn marked this conversation as resolved.
Show resolved Hide resolved
const QString applicationDirectory = PlatformUtilities::instance()->applicationDirectory();
if ( !applicationDirectory.isEmpty() && reply->error() == QNetworkReply::NoError )
std::unique_ptr<QTemporaryFile> tmpFile;
tmpFile.reset( temporaryFile );

if ( reply->error() == QNetworkReply::NoError )
{
QString fileName = reply->url().fileName();
QString contentDisposition = reply->header( QNetworkRequest::ContentDispositionHeader ).toString();
Expand Down Expand Up @@ -322,13 +335,10 @@ void AppInterface::importUrl( const QString &url )
}
QDir( QFileInfo( filePath ).absolutePath() ).mkpath( "." );

QFile file( filePath );
if ( file.open( QIODevice::WriteOnly ) )
tmpFile->write( reply->readAll() );
tmpFile->close();
if ( tmpFile->rename( filePath ) )
{
const QByteArray data = reply->readAll();
file.write( data );
file.close();

if ( fileSuffix == QLatin1String( "zip" ) )
{
// Check if this is a compressed project and handle accordingly
Expand All @@ -353,26 +363,32 @@ void AppInterface::importUrl( const QString &url )

if ( QgsZipUtils::unzip( filePath, zipDirectory, zipFiles, false ) )
{
file.remove();
tmpFile->remove();
// Project archive successfully imported
emit importEnded( zipDirectory );
return;
}
else
{
// Broken archive, bail out
// Broken project archive, bail out
QDir dir( zipDirectory );
dir.removeRecursively();
file.remove();
tmpFile->remove();
filePath.clear();
emit importEnded();
return;
}
}
}

// Dataset successfully imported
emit importEnded( QFileInfo( filePath ).absolutePath() );
return;
}
}

// cppcheck-suppress nullPointer
tmpFile->remove();
emit importEnded();
} );
}
Loading