This repository has been archived by the owner on May 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#48 Add IFileDownloader and FileDownloader implementation
- Loading branch information
1 parent
387900e
commit 2f13455
Showing
9 changed files
with
170 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "IFileDownloader.hpp" | ||
|
||
MellowPlayer::Application::IFileDownloader::~IFileDownloader() = default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <QtCore/QString> | ||
#include <QtCore/QObject> | ||
|
||
namespace MellowPlayer::Application { | ||
|
||
class IFileDownloader: public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
virtual ~IFileDownloader(); | ||
|
||
virtual void download(const QString& urlToDownload, const QString& filePath) = 0; | ||
virtual double getProgress() const = 0; | ||
virtual bool isDownloading() const = 0; | ||
|
||
signals: | ||
void progressChanged(); | ||
void finished(bool success); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,4 @@ namespace MellowPlayer::Application { | |
virtual void show() = 0; | ||
virtual void hide() = 0; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <iostream> | ||
#include "FileDownloader.hpp" | ||
|
||
using namespace MellowPlayer::Application; | ||
using namespace MellowPlayer::Infrastructure; | ||
|
||
|
||
FileDownloader::FileDownloader() | ||
{ | ||
connect(&networkAccessManager_, &QNetworkAccessManager::finished, this, &FileDownloader::onDownloadFinished); | ||
} | ||
|
||
void FileDownloader::download(const QString& urlToDownload, const QString& filePath) | ||
{ | ||
if (!isDownloading()) { | ||
progress_ = 0; | ||
destinationPath_ = QFileInfo(filePath); | ||
currentReply = networkAccessManager_.get(QNetworkRequest(QUrl(urlToDownload))); | ||
connect(currentReply, &QNetworkReply::downloadProgress, this, &FileDownloader::onDownloadProgress); | ||
} | ||
} | ||
|
||
double FileDownloader::getProgress() const | ||
{ | ||
return progress_; | ||
} | ||
|
||
bool FileDownloader::isDownloading() const | ||
{ | ||
return currentReply != nullptr; | ||
} | ||
|
||
void FileDownloader::onDownloadFinished(QNetworkReply* reply) | ||
{ | ||
bool success = false; | ||
|
||
currentReply = nullptr; | ||
|
||
if (reply->error() == QNetworkReply::NoError) | ||
{ | ||
QString redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString(); | ||
|
||
if (!redirectUrl.isEmpty()) { | ||
download(redirectUrl, destinationPath_.absoluteFilePath()); | ||
return; | ||
} | ||
|
||
QByteArray replyData = reply->readAll(); | ||
QFile file(destinationPath_.absoluteFilePath()); | ||
if (file.open(QIODevice::WriteOnly)) { | ||
file.write(replyData); | ||
success = true; | ||
} | ||
} | ||
|
||
emit finished(success); | ||
} | ||
|
||
void FileDownloader::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { | ||
|
||
double ratio = 0; | ||
if (bytesTotal > bytesReceived) | ||
ratio = static_cast<double>(bytesReceived) / bytesTotal; | ||
double progress = (ratio * 100); | ||
|
||
std::cerr << bytesReceived << " / " << bytesTotal << " = " << progress << std::endl; | ||
|
||
if (progress_ != progress) { | ||
progress_ = progress; | ||
emit progressChanged(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <QtCore/QFileInfo> | ||
#include <QtNetwork/QNetworkAccessManager> | ||
#include <QtNetwork/QNetworkReply> | ||
#include <MellowPlayer/Application/IFileDownloader.hpp> | ||
|
||
namespace MellowPlayer::Infrastructure { | ||
|
||
class FileDownloader: public Application::IFileDownloader | ||
{ | ||
Q_OBJECT | ||
public: | ||
FileDownloader(); | ||
|
||
void download(const QString& urlToDownload, const QString& filePath) override; | ||
double getProgress() const override; | ||
bool isDownloading() const override; | ||
|
||
private slots: | ||
void onDownloadFinished(QNetworkReply* reply); | ||
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); | ||
|
||
private: | ||
QNetworkAccessManager networkAccessManager_; | ||
QFileInfo destinationPath_; | ||
QNetworkReply* currentReply = nullptr; | ||
double progress_ = 0; | ||
}; | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/IntegrationTests/Infrastructure/FileDownloaderTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <catch.hpp> | ||
#include <MellowPlayer/Infrastructure/FileDownloader.hpp> | ||
#include <QtCore/QTemporaryDir> | ||
#include <QtTest/qtestsystem.h> | ||
|
||
using namespace MellowPlayer::Infrastructure; | ||
|
||
SCENARIO("FileDownloader can download a release source archive") { | ||
FileDownloader downloader; | ||
QTemporaryDir dir; | ||
QString destination = dir.path() + "/MellowPlayer.zip"; | ||
REQUIRE(!QFileInfo::exists(destination)); | ||
|
||
WHEN("downloading MellowPlayer.zip from github") { | ||
downloader.download("https://github.com/ColinDuquesnoy/MellowPlayer/archive/2.95.0.zip", destination); | ||
|
||
THEN("progress is updated regularly until download has finished") { | ||
double latestProgress = -1; | ||
while(downloader.isDownloading()) { | ||
QTest::qWait(100); | ||
bool validProgressUpdate = downloader.getProgress() >= latestProgress || downloader.getProgress() == 0; | ||
REQUIRE(validProgressUpdate); | ||
latestProgress = downloader.getProgress(); | ||
} | ||
|
||
AND_THEN("destination file exists") { | ||
REQUIRE(QFileInfo::exists(destination)); | ||
REQUIRE(QFileInfo(destination).size() == 20396818); | ||
} | ||
} | ||
} | ||
} |