Browse Source

Merge pull request #4015 from naikel/cookies

[WebUI] Add save path, cookies and labels when adding torrents
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
6f2eccd62f
  1. 5
      src/webui/extra_translations.h
  2. 48
      src/webui/webapplication.cpp
  3. 1
      src/webui/webapplication.h
  4. 40
      src/webui/www/public/download.html
  5. 34
      src/webui/www/public/scripts/download.js
  6. 8
      src/webui/www/public/scripts/mocha-init.js
  7. 89
      src/webui/www/public/upload.html

5
src/webui/extra_translations.h

@ -39,7 +39,7 @@ static const char *__TRANSLATIONS__[] = {
QT_TRANSLATE_NOOP("HttpServer", "Logout"), QT_TRANSLATE_NOOP("HttpServer", "Logout"),
QT_TRANSLATE_NOOP("HttpServer", "Download Torrents from their URL or Magnet link"), QT_TRANSLATE_NOOP("HttpServer", "Download Torrents from their URL or Magnet link"),
QT_TRANSLATE_NOOP("HttpServer", "Only one link per line"), QT_TRANSLATE_NOOP("HttpServer", "Only one link per line"),
QT_TRANSLATE_NOOP("HttpServer", "Download local torrent"), QT_TRANSLATE_NOOP("HttpServer", "Upload local torrent"),
QT_TRANSLATE_NOOP("HttpServer", "Download"), QT_TRANSLATE_NOOP("HttpServer", "Download"),
QT_TRANSLATE_NOOP("HttpServer", "Are you sure you want to delete the selected torrents from the transfer list?"), QT_TRANSLATE_NOOP("HttpServer", "Are you sure you want to delete the selected torrents from the transfer list?"),
QT_TRANSLATE_NOOP("HttpServer", "Global upload rate limit must be greater than 0 or disabled."), QT_TRANSLATE_NOOP("HttpServer", "Global upload rate limit must be greater than 0 or disabled."),
@ -84,6 +84,9 @@ static const char *__TRANSLATIONS__[] = {
QT_TRANSLATE_NOOP("HttpServer", "Paused"), QT_TRANSLATE_NOOP("HttpServer", "Paused"),
QT_TRANSLATE_NOOP("HttpServer", "Active"), QT_TRANSLATE_NOOP("HttpServer", "Active"),
QT_TRANSLATE_NOOP("HttpServer", "Inactive") QT_TRANSLATE_NOOP("HttpServer", "Inactive")
QT_TRANSLATE_NOOP("HttpServer", "Save files to location:")
QT_TRANSLATE_NOOP("HttpServer", "Label:")
QT_TRANSLATE_NOOP("HttpServer", "Cookie:")
}; };
static const struct { const char *source; const char *comment; } __COMMENTED_TRANSLATIONS__[] = { static const struct { const char *source; const char *comment; } __COMMENTED_TRANSLATIONS__[] = {

48
src/webui/webapplication.cpp

@ -44,6 +44,7 @@
#include "core/bittorrent/trackerentry.h" #include "core/bittorrent/trackerentry.h"
#include "core/bittorrent/torrentinfo.h" #include "core/bittorrent/torrentinfo.h"
#include "core/bittorrent/torrenthandle.h" #include "core/bittorrent/torrenthandle.h"
#include "core/net/downloadmanager.h"
#include "websessiondata.h" #include "websessiondata.h"
#include "webapplication.h" #include "webapplication.h"
@ -112,6 +113,7 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize
ADD_ACTION(command, bottomPrio); ADD_ACTION(command, bottomPrio);
ADD_ACTION(command, recheck); ADD_ACTION(command, recheck);
ADD_ACTION(command, setLabel); ADD_ACTION(command, setLabel);
ADD_ACTION(command, getSavePath);
ADD_ACTION(version, api); ADD_ACTION(version, api);
ADD_ACTION(version, api_min); ADD_ACTION(version, api_min);
ADD_ACTION(version, qbittorrent); ADD_ACTION(version, qbittorrent);
@ -306,9 +308,33 @@ void WebApplication::action_command_shutdown()
void WebApplication::action_command_download() void WebApplication::action_command_download()
{ {
CHECK_URI(0); CHECK_URI(0);
CHECK_PARAMETERS("urls");
QString urls = request().posts["urls"]; QString urls = request().posts["urls"];
QStringList list = urls.split('\n'); QStringList list = urls.split('\n');
QString savepath = request().posts["savepath"];
QString label = request().posts["label"];
QString cookie = request().posts["cookie"];
QList<QNetworkCookie> cookies;
if (!cookie.isEmpty()) {
QStringList cookiesStr = cookie.split("; ");
foreach (QString cookieStr, cookiesStr) {
cookieStr = cookieStr.trimmed();
int index = cookieStr.indexOf('=');
if (index > 1) {
QByteArray name = cookieStr.left(index).toLatin1();
QByteArray value = cookieStr.right(cookieStr.length() - index - 1).toLatin1();
QNetworkCookie c(name, value);
cookies << c;
}
}
}
savepath = savepath.trimmed();
label = label.trimmed();
BitTorrent::AddTorrentParams params;
params.savePath = savepath;
params.label = label;
foreach (QString url, list) { foreach (QString url, list) {
url = url.trimmed(); url = url.trimmed();
@ -320,7 +346,9 @@ void WebApplication::action_command_download()
if ((url.size() == 40 && !url.contains(QRegExp("[^0-9A-Fa-f]"))) if ((url.size() == 40 && !url.contains(QRegExp("[^0-9A-Fa-f]")))
|| (url.size() == 32 && !url.contains(QRegExp("[^2-7A-Za-z]")))) || (url.size() == 32 && !url.contains(QRegExp("[^2-7A-Za-z]"))))
url = "magnet:?xt=urn:btih:" + url; url = "magnet:?xt=urn:btih:" + url;
BitTorrent::Session::instance()->addTorrent(url);
Net::DownloadManager::instance()->setCookiesFromUrl(cookies, QUrl::fromEncoded(url.toUtf8()));
BitTorrent::Session::instance()->addTorrent(url, params);
} }
} }
} }
@ -329,6 +357,11 @@ void WebApplication::action_command_upload()
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
CHECK_URI(0); CHECK_URI(0);
QString savepath = request().posts["savepath"];
QString label = request().posts["label"];
savepath = savepath.trimmed();
label = label.trimmed();
foreach(const Http::UploadedFile& torrent, request().files) { foreach(const Http::UploadedFile& torrent, request().files) {
QString filePath = saveTmpFile(torrent.data); QString filePath = saveTmpFile(torrent.data);
@ -340,7 +373,10 @@ void WebApplication::action_command_upload()
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 {
if (!BitTorrent::Session::instance()->addTorrent(torrentInfo)) { BitTorrent::AddTorrentParams params;
params.savePath = savepath;
params.label = label;
if (!BitTorrent::Session::instance()->addTorrent(torrentInfo, params)) {
status(500, "Internal Server Error"); status(500, "Internal Server Error");
print(QObject::tr("Error: Could not add torrent to session."), Http::CONTENT_TYPE_TXT); print(QObject::tr("Error: Could not add torrent to session."), Http::CONTENT_TYPE_TXT);
} }
@ -685,6 +721,12 @@ void WebApplication::action_command_setLabel()
} }
} }
void WebApplication::action_command_getSavePath()
{
CHECK_URI(0);
print(Preferences::instance()->getSavePath());
}
bool WebApplication::isPublicScope() bool WebApplication::isPublicScope()
{ {
return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO); return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO);

1
src/webui/webapplication.h

@ -87,6 +87,7 @@ private:
void action_command_bottomPrio(); void action_command_bottomPrio();
void action_command_recheck(); void action_command_recheck();
void action_command_setLabel(); void action_command_setLabel();
void action_command_getSavePath();
void action_version_api(); void action_version_api();
void action_version_api_min(); void action_version_api_min();
void action_version_qbittorrent(); void action_version_qbittorrent();

40
src/webui/www/public/download.html

@ -4,16 +4,50 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QBT_TR(Add Torrent Link)QBT_TR</title> <title>QBT_TR(Add Torrent Link)QBT_TR</title>
<link rel="stylesheet" href="css/style.css" type="text/css" /> <link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/Window.css" type="text/css" />
<script type="text/javascript" src="scripts/mootools-1.2-core-yc.js" charset="utf-8"></script> <script type="text/javascript" src="scripts/mootools-1.2-core-yc.js" charset="utf-8"></script>
<script type="text/javascript" src="scripts/download.js" charset="utf-8"></script> <script type="text/javascript" src="scripts/download.js" charset="utf-8"></script>
</head> </head>
<body> <body>
<iframe id="download_frame" name="download_frame" class="invisible" src="javascript:false;"></iframe>
<form action="command/download" enctype="multipart/form-data" method="post" id="downloadForm" style="text-align: center;" target="download_frame">
<center> <center>
<br/> <br/>
<h2 class="vcenter">QBT_TR(Download Torrents from their URLs or Magnet links)QBT_TR</h2> <h2 class="vcenter">QBT_TR(Download Torrents from their URLs or Magnet links)QBT_TR</h2>
<textarea id="urls" rows="10"></textarea> <textarea id="urls" rows="10" name="urls"></textarea>
<p>QBT_TR(Only one link per line)QBT_TR</p> <p>QBT_TR(Only one link per line)QBT_TR</p>
<input type="button" value="QBT_TR(Download)QBT_TR" id="downButton"/> <fieldset class="settings" style="border: 0; text-align: left;">
</center> <div class="formRow" style="margin-top: 6px;">
<label for="savepath" class="leftLabelLarge">QBT_TR(Save files to location:)QBT_TR</label>
<input type="text" id="savepath" name="savepath" style="width: 16em;"/>
</div>
<div class="formRow">
<label for="cookie" class="leftLabelLarge">QBT_TR(Cookie:)QBT_TR</label>
<input type="text" id="cookie" name="cookie" style="width: 16em;"/>
</div>
<div class="formRow">
<label for="label" class="leftLabelLarge">QBT_TR(Label:)QBT_TR</label>
<input type="text" id="label" name="label" style="width: 16em;"/>
</div>
<div id="submitbutton" style="margin-top: 12px; text-align: center;">
<button type="submit" id="submitButton">QBT_TR(Download)QBT_TR</button>
</div>
</center>
</form>
<script type="text/javascript">
var submitted = false;
$('downloadForm').addEventListener("submit", function() {
$('download_spinner').style.display = "block";
submitted = true;
});
$('download_frame').addEventListener("load", function() {
if (submitted)
window.parent.closeWindows();
});
</script>
<div id="download_spinner" class="mochaSpinner"></div>
</body> </body>
</html> </html>

34
src/webui/www/public/scripts/download.js

@ -20,19 +20,23 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
window.addEvent('domready', function() {
$('urls').focus(); getSavePath = function() {
$('downButton').addEvent('click', function(e) { var req = new Request({
new Event(e).stop(); url: 'command/getSavePath',
new Request({ method: 'get',
url: 'command/download', noCache: true,
method: 'post', onFailure: function() {
data: { alert("Could not contact qBittorrent");
urls: $('urls').value },
}, onSuccess: function(data) {
onComplete: function() { if (data) {
window.parent.document.getElementById('downloadPage').parentNode.removeChild(window.parent.document.getElementById('downloadPage')); $('savepath').setProperty('value', data);
} }
}
}).send(); }).send();
}); }
});
$(window).addEventListener("load", function() {
getSavePath();
});

8
src/webui/www/public/scripts/mocha-init.js

@ -57,7 +57,7 @@ initializeWindows = function() {
paddingVertical: 0, paddingVertical: 0,
paddingHorizontal: 0, paddingHorizontal: 0,
width: 500, width: 500,
height: 300 height: 360
}); });
updateMainData(); updateMainData();
}); });
@ -88,7 +88,7 @@ initializeWindows = function() {
new Event(e).stop(); new Event(e).stop();
new MochaUI.Window({ new MochaUI.Window({
id: 'uploadPage', id: 'uploadPage',
title: "QBT_TR(Download local torrent)QBT_TR", title: "QBT_TR(Upload local torrent)QBT_TR",
loadMethod: 'iframe', loadMethod: 'iframe',
contentURL: 'upload.html', contentURL: 'upload.html',
scrollbars: true, scrollbars: true,
@ -96,8 +96,8 @@ initializeWindows = function() {
maximizable: false, maximizable: false,
paddingVertical: 0, paddingVertical: 0,
paddingHorizontal: 0, paddingHorizontal: 0,
width: 600, width: 500,
height: 130 height: 200
}); });
updateMainData(); updateMainData();
}); });

89
src/webui/www/public/upload.html

@ -2,73 +2,48 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QBT_TR(Download local torrent)QBT_TR</title> <title>QBT_TR(Upload local torrent)QBT_TR</title>
<link rel="stylesheet" href="css/style.css" type="text/css" /> <link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/Window.css" type="text/css" /> <link rel="stylesheet" href="css/Window.css" type="text/css" />
<script type="text/javascript" src="scripts/mootools-1.2-core-yc.js" charset="utf-8"></script> <script type="text/javascript" src="scripts/mootools-1.2-core-yc.js" charset="utf-8"></script>
<script type="text/javascript"> <script type="text/javascript" src="scripts/download.js" charset="utf-8"></script>
function stateChangeHandler() {
if (this.readyState == this.DONE) {
if (this.status == 200) {
window.parent.closeWindows();
} else {
if (this.responseText != "") {
alert(this.responseText);
} else {
alert("QBT_TR(Upload Failed!)QBT_TR");
}
}
$('upload_spinner').style.display = "none";
}
}
function uploadFiles(files) {
var xhr = new XMLHttpRequest();
if (xhr.upload) {
// start upload
var formData = new FormData();
for (var i = 0, file; file = files[i]; ++i)
formData.append(file.name, file);
xhr.onreadystatechange = stateChangeHandler;
xhr.open("POST", "command/upload");
// Bypass cache
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send(formData);
}
}
function fileHandler(e) {
e.preventDefault();
$('upload_spinner').style.display = "block";
// fetch FileList object
var files = $('fileselect').files
// process all File objects
uploadFiles(files);
}
window.addEvent('load', function() {
// is XHR2 available?
var xhr = new XMLHttpRequest();
if (xhr.upload) {
$('uploadForm').addEvent('submit', fileHandler);
} else {
$('upload_frame').addEvent('load', function() { window.parent.closeWindows(); });
$('uploadForm').addEvent('submit', function() { $('upload_spinner').style.display = "block"; });
}
});
</script>
</head> </head>
<body> <body>
<iframe id="upload_frame" name="upload_frame" class="invisible" src="javascript:false;"></iframe> <iframe id="upload_frame" name="upload_frame" class="invisible" src="javascript:false;"></iframe>
<form action="command/upload" enctype="multipart/form-data" method="post" id="uploadForm" target="upload_frame" style="text-align: center;"> <form action="command/upload" enctype="multipart/form-data" method="post" id="uploadForm" style="text-align: center;" target="upload_frame">
<div style="margin-top: 25px; display: inline-block; border: 1px solid lightgrey; border-radius: 4px;"> <p>
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple" /> <div style="margin-top: 25px; display: inline-block; border: 1px solid lightgrey; border-radius: 4px;">
<input type="file" id="fileselect" name="fileselect[]" multiple="multiple"/>
</div>
</p>
<fieldset class="settings" style="border: 0; text-align: left;">
<div class="formRow" style="margin-top: 12px;">
<label for="savepath" class="leftLabelLarge">QBT_TR(Save files to location:)QBT_TR</label>
<input type="text" id="savepath" name="savepath" style="width: 16em;"/>
</div>
<div class="formRow">
<label for="label" class="leftLabelLarge">QBT_TR(Label:)QBT_TR</label>
<input type="text" id="label" name="label"/ style="width: 16em;"/>
</div> </div>
<div id="submitbutton" style="margin-top: 30px;"> <div id="submitbutton" style="margin-top: 30px; text-align: center;">
<button type="submit" style="font-size: 1em;">QBT_TR(Upload Torrents)QBT_TR</button> <button type="submit" style="font-size: 1em;">QBT_TR(Upload Torrents)QBT_TR</button>
</div> </div>
</fieldset>
</form> </form>
<script type="text/javascript">
var submitted = false;
$('uploadForm').addEventListener("submit", function() {
$('upload_spinner').style.display = "block";
submitted = true;
});
$('upload_frame').addEventListener("load", function() {
if (submitted)
window.parent.closeWindows();
});
</script>
<div id="upload_spinner" class="mochaSpinner"></div> <div id="upload_spinner" class="mochaSpinner"></div>
</body> </body>
</html> </html>

Loading…
Cancel
Save