@ -32,6 +32,7 @@
@@ -32,6 +32,7 @@
# include <QTemporaryFile>
# include <QUrl>
# include "base/3rdparty/expected.hpp"
# include "base/utils/fs.h"
# include "base/utils/gzip.h"
# include "base/utils/io.h"
@ -41,18 +42,14 @@ const int MAX_REDIRECTIONS = 20; // the common value for web browsers
@@ -41,18 +42,14 @@ const int MAX_REDIRECTIONS = 20; // the common value for web browsers
namespace
{
bool saveToFile ( const QByteArray & replyData , QString & filePath )
nonstd : : expected < QString , QString > saveToTempFile ( const QByteArray & data )
{
if ( ! filePath . isEmpty ( ) )
return Utils : : IO : : saveToFile ( filePath , replyData ) . has_value ( ) ;
QTemporaryFile file { Utils : : Fs : : tempPath ( ) } ;
if ( ! file . open ( ) | | ( file . write ( replyD ata) ! = replyD ata. length ( ) ) | | ! file . flush ( ) )
return false ;
if ( ! file . open ( ) | | ( file . write ( data ) ! = data . length ( ) ) | | ! file . flush ( ) )
return nonstd : : make_unexpected ( file . errorString ( ) ) ;
file . setAutoRemove ( false ) ;
filePath = file . fileName ( ) ;
return true ;
return file . fileName ( ) ;
}
}
@ -130,11 +127,23 @@ void DownloadHandlerImpl::processFinishedDownload()
@@ -130,11 +127,23 @@ void DownloadHandlerImpl::processFinishedDownload()
if ( m_downloadRequest . saveToFile ( ) )
{
QString filePath { m_downloadRequest . destFileName ( ) } ;
if ( saveToFile ( m_result . data , filePath ) )
m_result . filePath = filePath ;
const QString destinationPath = m_downloadRequest . destFileName ( ) ;
if ( destinationPath . isEmpty ( ) )
{
const nonstd : : expected < QString , QString > result = saveToTempFile ( m_result . data ) ;
if ( result )
m_result . filePath = result . value ( ) ;
else
setError ( tr ( " I/O Error: %1 " ) . arg ( result . error ( ) ) ) ;
}
else
setError ( tr ( " I/O Error " ) ) ;
{
const nonstd : : expected < void , QString > result = Utils : : IO : : saveToFile ( destinationPath , m_result . data ) ;
if ( result )
m_result . filePath = destinationPath ;
else
setError ( tr ( " I/O Error: %1 " ) . arg ( result . error ( ) ) ) ;
}
}
finish ( ) ;