/
qgsalgorithmfiledownloader.cpp
132 lines (109 loc) · 4.57 KB
/
qgsalgorithmfiledownloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
qgsalgorithmfiledownloader.cpp
---------------------
begin : October 2017
copyright : (C) 2017 by Etienne Trimaille
email : etienne at kartoza dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmfiledownloader.h"
#include "qgsfiledownloader.h"
#include <QEventLoop>
#include <QFileInfo>
#include <QTimer>
#include <QUrl>
///@cond PRIVATE
QString QgsFileDownloaderAlgorithm::name() const
{
return QStringLiteral( "filedownloader" );
}
QString QgsFileDownloaderAlgorithm::displayName() const
{
return tr( "File downloader" );
}
QStringList QgsFileDownloaderAlgorithm::tags() const
{
return tr( "file,downloader,internet,url,fetch,get,https" ).split( ',' );
}
QString QgsFileDownloaderAlgorithm::group() const
{
return tr( "File tools" );
}
QString QgsFileDownloaderAlgorithm::shortHelpString() const
{
return tr( "This algorithm downloads a URL on the file system." );
}
QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance() const
{
return new QgsFileDownloaderAlgorithm();
}
void QgsFileDownloaderAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "URL" ), tr( "URL" ), QVariant(), false, false ) );
addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ),
tr( "File destination" ), QObject::tr( "*.*" ), QVariant(), true ) );
addOutput( new QgsProcessingOutputFile( QStringLiteral( "OUTPUT" ), tr( "File destination" ) ) );
}
QVariantMap QgsFileDownloaderAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
mFeedback = feedback;
QString url = parameterAsString( parameters, QStringLiteral( "URL" ), context );
QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
QEventLoop loop;
QTimer timer;
QgsFileDownloader *downloader = new QgsFileDownloader( QUrl( url ), outputFile, QString(), true );
connect( mFeedback, &QgsFeedback::canceled, downloader, &QgsFileDownloader::cancelDownload );
connect( downloader, &QgsFileDownloader::downloadError, this, &QgsFileDownloaderAlgorithm::reportErrors );
connect( downloader, &QgsFileDownloader::downloadProgress, this, &QgsFileDownloaderAlgorithm::receiveProgressFromDownloader );
connect( downloader, &QgsFileDownloader::downloadExited, &loop, &QEventLoop::quit );
connect( &timer, &QTimer::timeout, this, &QgsFileDownloaderAlgorithm::sendProgressFeedback );
downloader->startDownload();
timer.start( 1000 );
loop.exec();
timer.stop();
if ( ! QFileInfo( outputFile ).exists() )
throw QgsProcessingException( tr( "Output file doesn't exist." ) );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
void QgsFileDownloaderAlgorithm::reportErrors( QStringList errors )
{
throw QgsProcessingException( errors.join( '\n' ) );
}
void QgsFileDownloaderAlgorithm::sendProgressFeedback()
{
if ( ! mReceived.isEmpty() && ! mTotal.isEmpty() )
mFeedback->pushInfo( tr( "%1 of %2 downloaded." ).arg( mReceived ).arg( mTotal ) );
}
void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
{
if ( bytesTotal != 0 )
{
if ( mTotal.isEmpty() )
mTotal = humanSize( bytesTotal );
mReceived = humanSize( bytesReceived );
mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );
}
}
QString QgsFileDownloaderAlgorithm::humanSize( qint64 &bytes )
{
QStringList list;
list << "KB" << "MB" << "GB" << "TB";
QStringListIterator i( list );
QString unit( "bytes" );
while ( bytes >= 1024.0 && i.hasNext() )
{
unit = i.next();
bytes /= 1024.0;
}
return QString( "%1 %2" ).arg( QString::number( bytes ) ).arg( unit );
}
///@endcond