2015-04-19 18:17:47 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2010 Christophe Dumez
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
2016-04-28 14:02:38 +03:00
|
|
|
#include "torrentcreatorthread.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <libtorrent/bencode.hpp>
|
2016-04-28 14:02:38 +03:00
|
|
|
#include <libtorrent/create_torrent.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <libtorrent/torrent_info.hpp>
|
|
|
|
#include <libtorrent/storage.hpp>
|
|
|
|
|
2016-04-28 14:02:38 +03:00
|
|
|
#include <QFile>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/fs.h"
|
|
|
|
#include "base/utils/misc.h"
|
|
|
|
#include "base/utils/string.h"
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
namespace libt = libtorrent;
|
|
|
|
using namespace BitTorrent;
|
|
|
|
|
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
|
|
|
bool fileFilter(const std::string &f)
|
|
|
|
{
|
2017-03-07 14:41:38 +03:00
|
|
|
return !Utils::Fs::fileName(QString::fromStdString(f)).startsWith('.');
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TorrentCreatorThread::TorrentCreatorThread(QObject *parent)
|
|
|
|
: QThread(parent)
|
2016-01-15 09:44:10 +03:00
|
|
|
, m_private(false)
|
|
|
|
, m_pieceSize(0)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentCreatorThread::~TorrentCreatorThread()
|
|
|
|
{
|
2017-05-15 10:53:58 +03:00
|
|
|
requestInterruption();
|
|
|
|
wait(1000);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::create(const QString &inputPath, const QString &savePath, const QStringList &trackers,
|
|
|
|
const QStringList &urlSeeds, const QString &comment, bool isPrivate, int pieceSize)
|
|
|
|
{
|
2015-05-06 14:53:27 +03:00
|
|
|
m_inputPath = Utils::Fs::fromNativePath(inputPath);
|
|
|
|
m_savePath = Utils::Fs::fromNativePath(savePath);
|
2015-04-19 18:17:47 +03:00
|
|
|
if (QFile(m_savePath).exists())
|
2015-05-06 14:53:27 +03:00
|
|
|
Utils::Fs::forceRemove(m_savePath);
|
2015-04-19 18:17:47 +03:00
|
|
|
m_trackers = trackers;
|
|
|
|
m_urlSeeds = urlSeeds;
|
|
|
|
m_comment = comment;
|
|
|
|
m_private = isPrivate;
|
|
|
|
m_pieceSize = pieceSize;
|
|
|
|
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::sendProgressSignal(int numHashes, int numPieces)
|
|
|
|
{
|
|
|
|
emit updateProgress(static_cast<int>((numHashes * 100.) / numPieces));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::run()
|
|
|
|
{
|
|
|
|
emit updateProgress(0);
|
|
|
|
|
2017-03-03 11:42:13 +03:00
|
|
|
QString creator_str("qBittorrent " QBT_VERSION);
|
2015-04-19 18:17:47 +03:00
|
|
|
try {
|
|
|
|
libt::file_storage fs;
|
|
|
|
// Adding files to the torrent
|
2017-03-06 18:51:35 +03:00
|
|
|
libt::add_files(fs, Utils::Fs::toNativePath(m_inputPath).toStdString(), fileFilter);
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
libt::create_torrent t(fs, m_pieceSize);
|
|
|
|
|
|
|
|
// Add url seeds
|
|
|
|
foreach (const QString &seed, m_urlSeeds)
|
2017-03-06 18:51:35 +03:00
|
|
|
t.add_url_seed(seed.trimmed().toStdString());
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
int tier = 0;
|
|
|
|
bool newline = false;
|
|
|
|
foreach (const QString &tracker, m_trackers) {
|
|
|
|
if (tracker.isEmpty()) {
|
|
|
|
if (newline)
|
|
|
|
continue;
|
|
|
|
++tier;
|
|
|
|
newline = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-06 18:51:35 +03:00
|
|
|
t.add_tracker(tracker.trimmed().toStdString(), tier);
|
2015-04-19 18:17:47 +03:00
|
|
|
newline = false;
|
|
|
|
}
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2015-05-06 14:53:27 +03:00
|
|
|
const QString parentPath = Utils::Fs::branchPath(m_inputPath) + "/";
|
2017-03-06 18:51:35 +03:00
|
|
|
libt::set_piece_hashes(t, Utils::Fs::toNativePath(parentPath).toStdString(), boost::bind(&TorrentCreatorThread::sendProgressSignal, this, _1, t.num_pieces()));
|
2015-04-19 18:17:47 +03:00
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
|
|
|
t.set_creator(creator_str.toUtf8().constData());
|
|
|
|
t.set_comment(m_comment.toUtf8().constData());
|
|
|
|
// Is private ?
|
|
|
|
t.set_priv(m_private);
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
// create the torrent and print it to out
|
|
|
|
qDebug("Saving to %s", qPrintable(m_savePath));
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
wchar_t *savePathW = new wchar_t[m_savePath.length() + 1];
|
2015-05-06 14:53:27 +03:00
|
|
|
int len = Utils::Fs::toNativePath(m_savePath).toWCharArray(savePathW);
|
2015-04-19 18:17:47 +03:00
|
|
|
savePathW[len] = L'\0';
|
|
|
|
std::ofstream outfile(savePathW, std::ios_base::out | std::ios_base::binary);
|
|
|
|
delete[] savePathW;
|
|
|
|
#else
|
2015-05-06 14:53:27 +03:00
|
|
|
std::ofstream outfile(Utils::Fs::toNativePath(m_savePath).toLocal8Bit().constData(), std::ios_base::out | std::ios_base::binary);
|
2015-04-19 18:17:47 +03:00
|
|
|
#endif
|
|
|
|
if (outfile.fail())
|
|
|
|
throw std::exception();
|
|
|
|
|
2017-05-15 10:53:58 +03:00
|
|
|
if (isInterruptionRequested()) return;
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
libt::bencode(std::ostream_iterator<char>(outfile), t.generate());
|
|
|
|
outfile.close();
|
|
|
|
|
|
|
|
emit updateProgress(100);
|
|
|
|
emit creationSuccess(m_savePath, parentPath);
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2017-03-07 14:41:38 +03:00
|
|
|
emit creationFailure(QString::fromStdString(e.what()));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
}
|