Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qgroundcontrol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
5824e346
Commit
5824e346
authored
Sep 30, 2016
by
Don Gagne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle redirects in file downloads
parent
d7e1770e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
93 deletions
+35
-93
QGCFileDownload.cc
src/QGCFileDownload.cc
+17
-4
QGCFileDownload.h
src/QGCFileDownload.h
+4
-1
FirmwareUpgradeController.cc
src/VehicleSetup/FirmwareUpgradeController.cc
+11
-85
FirmwareUpgradeController.h
src/VehicleSetup/FirmwareUpgradeController.h
+3
-3
No files found.
src/QGCFileDownload.cc
View file @
5824e346
...
...
@@ -20,8 +20,12 @@ QGCFileDownload::QGCFileDownload(QObject* parent)
}
bool
QGCFileDownload
::
download
(
const
QString
&
remoteFile
)
bool
QGCFileDownload
::
download
(
const
QString
&
remoteFile
,
bool
redirect
)
{
if
(
!
redirect
)
{
_originalRemoteFile
=
remoteFile
;
}
if
(
remoteFile
.
isEmpty
())
{
qWarning
()
<<
"downloadFile empty"
;
return
false
;
...
...
@@ -70,7 +74,7 @@ bool QGCFileDownload::download(const QString& remoteFile)
// Store local file location in user attribute so we can retrieve when the download finishes
networkRequest
.
setAttribute
(
QNetworkRequest
::
User
,
localFile
);
QNetworkReply
*
networkReply
=
get
(
networkRequest
);
if
(
!
networkReply
)
{
qWarning
()
<<
"QNetworkAccessManager::get failed"
;
...
...
@@ -94,7 +98,16 @@ void QGCFileDownload::_downloadFinished(void)
if
(
reply
->
error
()
!=
QNetworkReply
::
NoError
)
{
return
;
}
// Check for redirection
QVariant
redirectionTarget
=
reply
->
attribute
(
QNetworkRequest
::
RedirectionTargetAttribute
);
if
(
!
redirectionTarget
.
isNull
())
{
QUrl
redirectUrl
=
reply
->
url
().
resolved
(
redirectionTarget
.
toUrl
());
download
(
redirectUrl
.
toString
(),
true
/* redirect */
);
reply
->
deleteLater
();
return
;
}
// Download file location is in user attribute
QString
downloadFilename
=
reply
->
request
().
attribute
(
QNetworkRequest
::
User
).
toString
();
Q_ASSERT
(
!
downloadFilename
.
isEmpty
());
...
...
@@ -109,7 +122,7 @@ void QGCFileDownload::_downloadFinished(void)
file
.
write
(
reply
->
readAll
());
file
.
close
();
emit
downloadFinished
(
reply
->
url
().
toString
()
,
downloadFilename
);
emit
downloadFinished
(
_originalRemoteFile
,
downloadFilename
);
}
/// @brief Called when an error occurs during download
...
...
src/QGCFileDownload.h
View file @
5824e346
...
...
@@ -22,8 +22,9 @@ public:
/// Download the specified remote file.
/// @param remoteFile File to download. Can be http address or file system path.
/// @param redirect true: call is internal due to redirect
/// @return true: Asynchronous download has started, false: Download initialization failed
bool
download
(
const
QString
&
remoteFile
);
bool
download
(
const
QString
&
remoteFile
,
bool
redirect
=
false
);
signals:
void
downloadProgress
(
qint64
curr
,
qint64
total
);
...
...
@@ -33,6 +34,8 @@ signals:
private:
void
_downloadFinished
(
void
);
void
_downloadError
(
QNetworkReply
::
NetworkError
code
);
QString
_originalRemoteFile
;
};
#endif
src/VehicleSetup/FirmwareUpgradeController.cc
View file @
5824e346
...
...
@@ -503,52 +503,15 @@ void FirmwareUpgradeController::_downloadFirmware(void)
_appendStatusLog
(
"Downloading firmware..."
);
_appendStatusLog
(
QString
(
" From: %1"
).
arg
(
_firmwareFilename
));
// Split out filename from path
QString
firmwareFilename
=
QFileInfo
(
_firmwareFilename
).
fileName
();
Q_ASSERT
(
!
firmwareFilename
.
isEmpty
());
// Determine location to download file to
QString
downloadFile
=
QStandardPaths
::
writableLocation
(
QStandardPaths
::
TempLocation
);
if
(
downloadFile
.
isEmpty
())
{
downloadFile
=
QStandardPaths
::
writableLocation
(
QStandardPaths
::
DownloadLocation
);
if
(
downloadFile
.
isEmpty
())
{
_errorCancel
(
"Unabled to find writable download location. Tried downloads and temp directory."
);
return
;
}
}
Q_ASSERT
(
!
downloadFile
.
isEmpty
());
downloadFile
+=
"/"
+
firmwareFilename
;
QUrl
firmwareUrl
;
if
(
_firmwareFilename
.
startsWith
(
"http:"
))
{
firmwareUrl
.
setUrl
(
_firmwareFilename
);
}
else
{
firmwareUrl
=
QUrl
::
fromLocalFile
(
_firmwareFilename
);
}
Q_ASSERT
(
firmwareUrl
.
isValid
());
QNetworkRequest
networkRequest
(
firmwareUrl
);
// Store download file location in user attribute so we can retrieve when the download finishes
networkRequest
.
setAttribute
(
QNetworkRequest
::
User
,
downloadFile
);
_downloadManager
=
new
QNetworkAccessManager
(
this
);
Q_CHECK_PTR
(
_downloadManager
);
QNetworkProxy
tProxy
;
tProxy
.
setType
(
QNetworkProxy
::
DefaultProxy
);
_downloadManager
->
setProxy
(
tProxy
);
_downloadNetworkReply
=
_downloadManager
->
get
(
networkRequest
);
Q_ASSERT
(
_downloadNetworkReply
);
connect
(
_downloadNetworkReply
,
&
QNetworkReply
::
downloadProgress
,
this
,
&
FirmwareUpgradeController
::
_downloadProgress
);
connect
(
_downloadNetworkReply
,
&
QNetworkReply
::
finished
,
this
,
&
FirmwareUpgradeController
::
_downloadFinished
);
connect
(
_downloadNetworkReply
,
static_cast
<
void
(
QNetworkReply
::*
)(
QNetworkReply
::
NetworkError
)
>
(
&
QNetworkReply
::
error
),
this
,
&
FirmwareUpgradeController
::
_downloadError
);
QGCFileDownload
*
downloader
=
new
QGCFileDownload
(
this
);
connect
(
downloader
,
&
QGCFileDownload
::
downloadFinished
,
this
,
&
FirmwareUpgradeController
::
_firmwareDownloadFinished
);
connect
(
downloader
,
&
QGCFileDownload
::
downloadProgress
,
this
,
&
FirmwareUpgradeController
::
_firmwareDownloadProgress
);
connect
(
downloader
,
&
QGCFileDownload
::
error
,
this
,
&
FirmwareUpgradeController
::
_firmwareDownloadError
);
downloader
->
download
(
_firmwareFilename
);
}
/// @brief Updates the progress indicator while downloading
void
FirmwareUpgradeController
::
_
d
ownloadProgress
(
qint64
curr
,
qint64
total
)
void
FirmwareUpgradeController
::
_
firmwareD
ownloadProgress
(
qint64
curr
,
qint64
total
)
{
// Take care of cases where 0 / 0 is emitted as error return value
if
(
total
>
0
)
{
...
...
@@ -557,43 +520,18 @@ void FirmwareUpgradeController::_downloadProgress(qint64 curr, qint64 total)
}
/// @brief Called when the firmware download completes.
void
FirmwareUpgradeController
::
_
downloadFinished
(
void
)
void
FirmwareUpgradeController
::
_
firmwareDownloadFinished
(
QString
remoteFile
,
QString
localFile
)
{
Q_UNUSED
(
remoteFile
);
_appendStatusLog
(
"Download complete"
);
QNetworkReply
*
reply
=
qobject_cast
<
QNetworkReply
*>
(
QObject
::
sender
());
Q_ASSERT
(
reply
);
Q_ASSERT
(
_downloadNetworkReply
==
reply
);
_downloadManager
->
deleteLater
();
_downloadManager
=
NULL
;
// When an error occurs or the user cancels the download, we still end up here. So bail out in
// those cases.
if
(
reply
->
error
()
!=
QNetworkReply
::
NoError
)
{
return
;
}
// Download file location is in user attribute
QString
downloadFilename
=
reply
->
request
().
attribute
(
QNetworkRequest
::
User
).
toString
();
Q_ASSERT
(
!
downloadFilename
.
isEmpty
());
// Store downloaded file in download location
QFile
file
(
downloadFilename
);
if
(
!
file
.
open
(
QIODevice
::
WriteOnly
))
{
_errorCancel
(
QString
(
"Could not save downloaded file to %1. Error: %2"
).
arg
(
downloadFilename
).
arg
(
file
.
errorString
()));
return
;
}
file
.
write
(
reply
->
readAll
());
file
.
close
();
FirmwareImage
*
image
=
new
FirmwareImage
(
this
);
connect
(
image
,
&
FirmwareImage
::
statusMessage
,
this
,
&
FirmwareUpgradeController
::
_status
);
connect
(
image
,
&
FirmwareImage
::
errorMessage
,
this
,
&
FirmwareUpgradeController
::
_error
);
if
(
!
image
->
load
(
downloadFilenam
e
,
_bootloaderBoardID
))
{
if
(
!
image
->
load
(
localFil
e
,
_bootloaderBoardID
))
{
_errorCancel
(
"Image load failed"
);
return
;
}
...
...
@@ -613,20 +551,8 @@ void FirmwareUpgradeController::_downloadFinished(void)
}
/// @brief Called when an error occurs during download
void
FirmwareUpgradeController
::
_
downloadError
(
QNetworkReply
::
NetworkError
code
)
void
FirmwareUpgradeController
::
_
firmwareDownloadError
(
QString
errorMsg
)
{
QString
errorMsg
;
if
(
code
==
QNetworkReply
::
OperationCanceledError
)
{
errorMsg
=
"Download cancelled"
;
}
else
if
(
code
==
QNetworkReply
::
ContentNotFoundError
)
{
errorMsg
=
QString
(
"Error: File Not Found. Please check %1 firmware version is available."
)
.
arg
(
firmwareTypeAsString
(
_selectedFirmwareType
));
}
else
{
errorMsg
=
QString
(
"Error during download. Error: %1"
).
arg
(
code
);
}
_errorCancel
(
errorMsg
);
}
...
...
src/VehicleSetup/FirmwareUpgradeController.h
View file @
5824e346
...
...
@@ -155,9 +155,9 @@ signals:
void
px4BetaVersionChanged
(
const
QString
&
px4BetaVersion
);
private
slots
:
void
_
d
ownloadProgress
(
qint64
curr
,
qint64
total
);
void
_
downloadFinished
(
void
);
void
_
downloadError
(
QNetworkReply
::
NetworkError
code
);
void
_
firmwareD
ownloadProgress
(
qint64
curr
,
qint64
total
);
void
_
firmwareDownloadFinished
(
QString
remoteFile
,
QString
localFile
);
void
_
firmwareDownloadError
(
QString
errorMsg
);
void
_foundBoard
(
bool
firstAttempt
,
const
QSerialPortInfo
&
portInfo
,
int
boardType
);
void
_noBoardFound
(
void
);
void
_boardGone
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment