Browse Source

Refactor

Merge statements
Use case-insensitive contains()
Add const
Use value(), this avoids inserting empty values.
Use range based for loop
adaptive-webui-19844
Chocobo1 7 years ago
parent
commit
72b0ba36ae
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 74
      src/webui/webapplication.cpp

74
src/webui/webapplication.cpp

@ -390,48 +390,42 @@ void WebApplication::action_command_shutdown()
void WebApplication::action_command_download() void WebApplication::action_command_download()
{ {
CHECK_URI(0); CHECK_URI(0);
QString urls = request().posts["urls"];
QStringList list = urls.split('\n'); const QString urls = request().posts.value("urls");
bool skipChecking = request().posts["skip_checking"] == "true"; const bool skipChecking = request().posts.value("skip_checking").contains("true", Qt::CaseInsensitive);
bool addPaused = request().posts["paused"] == "true"; const bool addPaused = request().posts.value("paused").contains("true", Qt::CaseInsensitive);
const QString rootFolder = request().posts["root_folder"]; const QString rootFolder = request().posts.value("root_folder");
QString savepath = request().posts["savepath"]; const QString savepath = request().posts.value("savepath").trimmed();
QString category = request().posts["category"]; const QString category = request().posts.value("category").trimmed();
QString cookie = request().posts["cookie"]; const QString cookie = request().posts.value("cookie");
QList<QNetworkCookie> cookies; QList<QNetworkCookie> cookies;
if (!cookie.isEmpty()) { if (!cookie.isEmpty()) {
const QStringList cookiesStr = cookie.split("; ");
QStringList cookiesStr = cookie.split("; "); for (QString cookieStr : cookiesStr) {
foreach (QString cookieStr, cookiesStr) {
cookieStr = cookieStr.trimmed(); cookieStr = cookieStr.trimmed();
int index = cookieStr.indexOf('='); int index = cookieStr.indexOf('=');
if (index > 1) { if (index > 1) {
QByteArray name = cookieStr.left(index).toLatin1(); QByteArray name = cookieStr.left(index).toLatin1();
QByteArray value = cookieStr.right(cookieStr.length() - index - 1).toLatin1(); QByteArray value = cookieStr.right(cookieStr.length() - index - 1).toLatin1();
QNetworkCookie c(name, value); cookies += QNetworkCookie(name, value);
cookies << c;
} }
} }
} }
savepath = savepath.trimmed();
category = category.trimmed();
BitTorrent::AddTorrentParams params; BitTorrent::AddTorrentParams params;
// TODO: Check if destination actually exists // TODO: Check if destination actually exists
params.skipChecking = skipChecking; params.skipChecking = skipChecking;
params.addPaused = TriStateBool(addPaused); params.addPaused = TriStateBool(addPaused);
params.savePath = savepath; params.savePath = savepath;
params.category = category; params.category = category;
if (rootFolder == "true") if (rootFolder.contains("true", Qt::CaseInsensitive))
params.createSubfolder = TriStateBool::True; params.createSubfolder = TriStateBool::True;
else if (rootFolder == "false") else if (rootFolder.contains("false", Qt::CaseInsensitive))
params.createSubfolder = TriStateBool::False; params.createSubfolder = TriStateBool::False;
bool partialSuccess = false; bool partialSuccess = false;
foreach (QString url, list) { for (QString url : urls.split('\n')) {
url = url.trimmed(); url = url.trimmed();
if (!url.isEmpty()) { if (!url.isEmpty()) {
Net::DownloadManager::instance()->setCookiesFromUrl(cookies, QUrl::fromEncoded(url.toUtf8())); Net::DownloadManager::instance()->setCookiesFromUrl(cookies, QUrl::fromEncoded(url.toUtf8()));
@ -447,38 +441,38 @@ void WebApplication::action_command_download()
void WebApplication::action_command_upload() void WebApplication::action_command_upload()
{ {
qDebug() << Q_FUNC_INFO;
CHECK_URI(0); CHECK_URI(0);
bool skipChecking = request().posts["skip_checking"] == "true";
bool addPaused = request().posts["paused"] == "true";
const QString rootFolder = request().posts["root_folder"];
QString savepath = request().posts["savepath"];
QString category = request().posts["category"];
savepath = savepath.trimmed(); const bool skipChecking = request().posts.value("skip_checking").contains("true", Qt::CaseInsensitive);
category = category.trimmed(); const bool addPaused = request().posts.value("paused").contains("true", Qt::CaseInsensitive);
const QString rootFolder = request().posts.value("root_folder");
const QString savepath = request().posts.value("savepath").trimmed();
const QString category = request().posts.value("category").trimmed();
foreach(const Http::UploadedFile& torrent, request().files) { for (const Http::UploadedFile &torrent : request().files) {
QString filePath = saveTmpFile(torrent.data); const QString filePath = saveTmpFile(torrent.data);
if (filePath.isEmpty()) {
qWarning() << "I/O Error: Could not create temporary file";
status(500, "Internal Server Error");
print(QObject::tr("I/O Error: Could not create temporary file."), Http::CONTENT_TYPE_TXT);
continue;
}
if (!filePath.isEmpty()) { const BitTorrent::TorrentInfo torrentInfo = BitTorrent::TorrentInfo::loadFromFile(filePath);
BitTorrent::TorrentInfo torrentInfo = BitTorrent::TorrentInfo::loadFromFile(filePath);
if (!torrentInfo.isValid()) { if (!torrentInfo.isValid()) {
status(415, "Unsupported Media Type"); status(415, "Unsupported Media Type");
print(QObject::tr("Error: '%1' is not a valid torrent file.\n").arg(torrent.filename), Http::CONTENT_TYPE_TXT); print(QObject::tr("Error: '%1' is not a valid torrent file.\n").arg(torrent.filename), Http::CONTENT_TYPE_TXT);
} }
else { else {
BitTorrent::AddTorrentParams params; BitTorrent::AddTorrentParams params;
// TODO: Check if destination actually exists // TODO: Check if destination actually exists
params.skipChecking = skipChecking; params.skipChecking = skipChecking;
params.addPaused = TriStateBool(addPaused); params.addPaused = TriStateBool(addPaused);
params.savePath = savepath; params.savePath = savepath;
params.category = category; params.category = category;
if (rootFolder == "true") if (rootFolder.contains("true", Qt::CaseInsensitive))
params.createSubfolder = TriStateBool::True; params.createSubfolder = TriStateBool::True;
else if (rootFolder == "false") else if (rootFolder.contains("false", Qt::CaseInsensitive))
params.createSubfolder = TriStateBool::False; params.createSubfolder = TriStateBool::False;
if (!BitTorrent::Session::instance()->addTorrent(torrentInfo, params)) { if (!BitTorrent::Session::instance()->addTorrent(torrentInfo, params)) {
@ -489,12 +483,6 @@ void WebApplication::action_command_upload()
// Clean up // Clean up
Utils::Fs::forceRemove(filePath); Utils::Fs::forceRemove(filePath);
} }
else {
qWarning() << "I/O Error: Could not create temporary file";
status(500, "Internal Server Error");
print(QObject::tr("I/O Error: Could not create temporary file."), Http::CONTENT_TYPE_TXT);
}
}
} }
void WebApplication::action_command_addTrackers() void WebApplication::action_command_addTrackers()

Loading…
Cancel
Save