2015-04-19 18:17:47 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-05-08 07:11:00 +03:00
|
|
|
#include "torrentinfo.h"
|
|
|
|
|
2020-02-19 13:19:51 +03:00
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include <libtorrent/create_torrent.hpp>
|
2018-05-08 07:11:00 +03:00
|
|
|
#include <libtorrent/error_code.hpp>
|
2020-10-25 08:44:02 +03:00
|
|
|
#include <libtorrent/version.hpp>
|
2018-05-08 07:11:00 +03:00
|
|
|
|
2019-03-02 08:22:13 +03:00
|
|
|
#include <QByteArray>
|
2018-04-14 22:53:45 +03:00
|
|
|
#include <QDateTime>
|
2016-02-29 02:44:08 +03:00
|
|
|
#include <QDebug>
|
2019-03-02 08:22:13 +03:00
|
|
|
#include <QDir>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QString>
|
2019-03-02 08:22:13 +03:00
|
|
|
#include <QStringList>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QUrl>
|
2020-12-09 08:48:59 +03:00
|
|
|
#include <QVector>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2020-02-19 13:19:51 +03:00
|
|
|
#include "base/exceptions.h"
|
2019-04-08 20:09:16 +03:00
|
|
|
#include "base/global.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/fs.h"
|
2020-04-01 09:25:00 +03:00
|
|
|
#include "base/utils/io.h"
|
2019-04-08 20:12:52 +03:00
|
|
|
#include "base/utils/misc.h"
|
2015-04-19 18:17:47 +03:00
|
|
|
#include "infohash.h"
|
|
|
|
#include "trackerentry.h"
|
|
|
|
|
|
|
|
using namespace BitTorrent;
|
|
|
|
|
2020-12-10 09:54:27 +03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
QString getRootFolder(const QStringList &filePaths)
|
|
|
|
{
|
|
|
|
QString rootFolder;
|
|
|
|
for (const QString &filePath : filePaths)
|
|
|
|
{
|
|
|
|
if (QDir::isAbsolutePath(filePath)) continue;
|
|
|
|
|
|
|
|
const auto filePathElements = filePath.splitRef('/');
|
|
|
|
// if at least one file has no root folder, no common root folder exists
|
|
|
|
if (filePathElements.count() <= 1) return {};
|
|
|
|
|
|
|
|
if (rootFolder.isEmpty())
|
|
|
|
rootFolder = filePathElements.at(0).toString();
|
|
|
|
else if (rootFolder != filePathElements.at(0))
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootFolder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:05:09 +03:00
|
|
|
TorrentInfo::TorrentInfo(std::shared_ptr<const lt::torrent_info> nativeInfo)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2019-05-07 06:22:39 +03:00
|
|
|
m_nativeInfo = std::const_pointer_cast<lt::torrent_info>(nativeInfo);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo::TorrentInfo(const TorrentInfo &other)
|
|
|
|
: m_nativeInfo(other.m_nativeInfo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other)
|
|
|
|
{
|
|
|
|
m_nativeInfo = other.m_nativeInfo;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:20:55 +03:00
|
|
|
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2018-06-25 20:42:07 +03:00
|
|
|
// 2-step construction to overcome default limits of `depth_limit` & `token_limit` which are
|
|
|
|
// used in `torrent_info()` constructor
|
|
|
|
const int depthLimit = 100;
|
|
|
|
const int tokenLimit = 10000000;
|
|
|
|
|
2019-11-28 21:46:48 +03:00
|
|
|
lt::error_code ec;
|
|
|
|
const lt::bdecode_node node = lt::bdecode(data, ec
|
|
|
|
, nullptr, depthLimit, tokenLimit);
|
2020-11-16 10:02:11 +03:00
|
|
|
if (ec)
|
|
|
|
{
|
2018-06-25 20:42:07 +03:00
|
|
|
if (error)
|
2017-12-13 17:20:55 +03:00
|
|
|
*error = QString::fromStdString(ec.message());
|
2018-06-25 20:42:07 +03:00
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:05:09 +03:00
|
|
|
TorrentInfo info {std::shared_ptr<lt::torrent_info>(new lt::torrent_info(node, ec))};
|
2020-11-16 10:02:11 +03:00
|
|
|
if (ec)
|
|
|
|
{
|
2018-06-25 20:42:07 +03:00
|
|
|
if (error)
|
|
|
|
*error = QString::fromStdString(ec.message());
|
|
|
|
return TorrentInfo();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:20:55 +03:00
|
|
|
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2018-02-19 20:02:57 +03:00
|
|
|
if (error)
|
|
|
|
error->clear();
|
|
|
|
|
|
|
|
QFile file {path};
|
2020-11-16 10:02:11 +03:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
2018-02-19 20:02:57 +03:00
|
|
|
if (error)
|
|
|
|
*error = file.errorString();
|
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if (file.size() > MAX_TORRENT_SIZE)
|
|
|
|
{
|
2018-02-19 20:02:57 +03:00
|
|
|
if (error)
|
2019-04-08 20:12:52 +03:00
|
|
|
*error = tr("File size exceeds max limit %1").arg(Utils::Misc::friendlyUnit(MAX_TORRENT_SIZE));
|
2018-02-19 20:02:57 +03:00
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
2018-08-04 06:20:01 +03:00
|
|
|
QByteArray data;
|
2020-11-16 10:02:11 +03:00
|
|
|
try
|
|
|
|
{
|
2018-08-04 06:20:01 +03:00
|
|
|
data = file.readAll();
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
catch (const std::bad_alloc &e)
|
|
|
|
{
|
2018-08-04 06:20:01 +03:00
|
|
|
if (error)
|
|
|
|
*error = tr("Torrent file read error: %1").arg(e.what());
|
|
|
|
return TorrentInfo();
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
if (data.size() != file.size())
|
|
|
|
{
|
2018-02-19 20:02:57 +03:00
|
|
|
if (error)
|
2018-08-04 06:20:01 +03:00
|
|
|
*error = tr("Torrent file read error: size mismatch");
|
2018-02-19 20:02:57 +03:00
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
2018-06-25 20:42:07 +03:00
|
|
|
return load(data, error);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 13:19:51 +03:00
|
|
|
void TorrentInfo::saveToFile(const QString &path) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
throw RuntimeError {tr("Invalid metadata.")};
|
|
|
|
|
|
|
|
const lt::create_torrent torrentCreator = lt::create_torrent(*(nativeInfo()));
|
|
|
|
const lt::entry torrentEntry = torrentCreator.generate();
|
|
|
|
|
2020-04-01 09:25:00 +03:00
|
|
|
QFile torrentFile {path};
|
|
|
|
if (!torrentFile.open(QIODevice::WriteOnly))
|
|
|
|
throw RuntimeError {torrentFile.errorString()};
|
2020-02-19 13:19:51 +03:00
|
|
|
|
2020-04-01 09:25:00 +03:00
|
|
|
lt::bencode(Utils::IO::FileDeviceOutputIterator {torrentFile}, torrentEntry);
|
|
|
|
if (torrentFile.error() != QFileDevice::NoError)
|
2020-02-19 13:19:51 +03:00
|
|
|
throw RuntimeError {torrentFile.errorString()};
|
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
bool TorrentInfo::isValid() const
|
|
|
|
{
|
|
|
|
return (m_nativeInfo && m_nativeInfo->is_valid() && (m_nativeInfo->num_files() > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoHash TorrentInfo::hash() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2015-04-19 18:17:47 +03:00
|
|
|
return m_nativeInfo->info_hash();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::name() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2020-12-10 09:42:32 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->orig_files().name());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime TorrentInfo::creationDate() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2019-04-01 17:40:05 +03:00
|
|
|
|
|
|
|
const std::time_t date = m_nativeInfo->creation_date();
|
|
|
|
return ((date != 0) ? QDateTime::fromSecsSinceEpoch(date) : QDateTime());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::creator() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2017-03-07 14:41:38 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->creator());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::comment() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2017-03-07 14:41:38 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->comment());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentInfo::isPrivate() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return false;
|
|
|
|
return m_nativeInfo->priv();
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong TorrentInfo::totalSize() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->total_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentInfo::filesCount() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->num_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentInfo::pieceLength() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->piece_length();
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
int TorrentInfo::pieceLength(const int index) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
2020-06-26 03:05:09 +03:00
|
|
|
return m_nativeInfo->piece_size(lt::piece_index_t {index});
|
2016-02-29 02:44:08 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
int TorrentInfo::piecesCount() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->num_pieces();
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
QString TorrentInfo::filePath(const int index) const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2019-06-16 20:14:15 +03:00
|
|
|
return Utils::Fs::toUniformPath(
|
2020-06-26 03:05:09 +03:00
|
|
|
QString::fromStdString(m_nativeInfo->files().file_path(lt::file_index_t {index})));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TorrentInfo::filePaths() const
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
for (int i = 0; i < filesCount(); ++i)
|
|
|
|
list << filePath(i);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
QString TorrentInfo::fileName(const int index) const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2015-05-06 14:53:27 +03:00
|
|
|
return Utils::Fs::fileName(filePath(index));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
QString TorrentInfo::origFilePath(const int index) const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2019-06-16 20:14:15 +03:00
|
|
|
return Utils::Fs::toUniformPath(
|
2020-06-26 03:05:09 +03:00
|
|
|
QString::fromStdString(m_nativeInfo->orig_files().file_path(lt::file_index_t {index})));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
qlonglong TorrentInfo::fileSize(const int index) const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
2020-06-26 03:05:09 +03:00
|
|
|
return m_nativeInfo->files().file_size(lt::file_index_t {index});
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
qlonglong TorrentInfo::fileOffset(const int index) const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
if (!isValid()) return -1;
|
2020-06-26 03:05:09 +03:00
|
|
|
return m_nativeInfo->files().file_offset(lt::file_index_t {index});
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-05-21 07:21:17 +03:00
|
|
|
QVector<TrackerEntry> TorrentInfo::trackers() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2019-05-21 07:21:17 +03:00
|
|
|
const std::vector<lt::announce_entry> trackers = m_nativeInfo->trackers();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2019-05-21 07:21:17 +03:00
|
|
|
QVector<TrackerEntry> ret;
|
|
|
|
ret.reserve(trackers.size());
|
|
|
|
|
|
|
|
for (const lt::announce_entry &tracker : trackers)
|
|
|
|
ret.append(tracker);
|
|
|
|
return ret;
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 07:55:06 +03:00
|
|
|
QVector<QUrl> TorrentInfo::urlSeeds() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2019-08-02 07:55:06 +03:00
|
|
|
const std::vector<lt::web_seed_entry> &nativeWebSeeds = m_nativeInfo->web_seeds();
|
|
|
|
|
|
|
|
QVector<QUrl> urlSeeds;
|
|
|
|
urlSeeds.reserve(nativeWebSeeds.size());
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
for (const lt::web_seed_entry &webSeed : nativeWebSeeds)
|
|
|
|
{
|
2019-05-07 06:22:39 +03:00
|
|
|
if (webSeed.type == lt::web_seed_entry::url_seed)
|
2015-04-19 18:17:47 +03:00
|
|
|
urlSeeds.append(QUrl(webSeed.url.c_str()));
|
2019-08-02 07:55:06 +03:00
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
return urlSeeds;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray TorrentInfo::metadata() const
|
|
|
|
{
|
2019-02-14 20:16:42 +03:00
|
|
|
if (!isValid()) return {};
|
2020-10-25 08:44:02 +03:00
|
|
|
#if (LIBTORRENT_VERSION_NUM >= 20000)
|
|
|
|
const lt::span<const char> infoSection {m_nativeInfo->info_section()};
|
|
|
|
return {infoSection.data(), static_cast<int>(infoSection.size())};
|
|
|
|
#else
|
2019-02-14 20:16:42 +03:00
|
|
|
return {m_nativeInfo->metadata().get(), m_nativeInfo->metadata_size()};
|
2020-10-25 08:44:02 +03:00
|
|
|
#endif
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
QStringList TorrentInfo::filesForPiece(const int pieceIndex) const
|
2016-02-27 22:51:39 +03:00
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
// no checks here because fileIndicesForPiece() will return an empty list
|
2019-02-09 18:40:14 +03:00
|
|
|
const QVector<int> fileIndices = fileIndicesForPiece(pieceIndex);
|
2016-02-27 22:51:39 +03:00
|
|
|
|
|
|
|
QStringList res;
|
2016-02-29 02:44:08 +03:00
|
|
|
res.reserve(fileIndices.size());
|
|
|
|
std::transform(fileIndices.begin(), fileIndices.end(), std::back_inserter(res),
|
|
|
|
[this](int i) { return filePath(i); });
|
|
|
|
|
2016-02-27 22:51:39 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
QVector<int> TorrentInfo::fileIndicesForPiece(const int pieceIndex) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
|
|
|
if (!isValid() || (pieceIndex < 0) || (pieceIndex >= piecesCount()))
|
2019-02-14 20:16:42 +03:00
|
|
|
return {};
|
2016-02-29 02:44:08 +03:00
|
|
|
|
2019-05-07 06:22:39 +03:00
|
|
|
const std::vector<lt::file_slice> files(
|
2020-06-26 03:05:09 +03:00
|
|
|
nativeInfo()->map_block(lt::piece_index_t {pieceIndex}, 0
|
|
|
|
, nativeInfo()->piece_size(lt::piece_index_t {pieceIndex})));
|
2016-02-29 02:44:08 +03:00
|
|
|
QVector<int> res;
|
2016-12-01 10:51:54 +03:00
|
|
|
res.reserve(int(files.size()));
|
2016-02-29 02:44:08 +03:00
|
|
|
std::transform(files.begin(), files.end(), std::back_inserter(res),
|
2019-05-07 06:22:39 +03:00
|
|
|
[](const lt::file_slice &s) { return static_cast<int>(s.file_index); });
|
2016-02-29 02:44:08 +03:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-05 19:36:00 +03:00
|
|
|
QVector<QByteArray> TorrentInfo::pieceHashes() const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const int count = piecesCount();
|
|
|
|
QVector<QByteArray> hashes;
|
|
|
|
hashes.reserve(count);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
2020-06-26 03:05:09 +03:00
|
|
|
hashes += {m_nativeInfo->hash_for_piece_ptr(lt::piece_index_t {i}), InfoHash::length()};
|
2017-04-05 19:36:00 +03:00
|
|
|
|
|
|
|
return hashes;
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:53:45 +03:00
|
|
|
TorrentInfo::PieceRange TorrentInfo::filePieces(const QString &file) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
|
|
|
if (!isValid()) // if we do not check here the debug message will be printed, which would be not correct
|
|
|
|
return {};
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
const int index = fileIndex(file);
|
2020-11-16 10:02:11 +03:00
|
|
|
if (index == -1)
|
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
qDebug() << "Filename" << file << "was not found in torrent" << name();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return filePieces(index);
|
|
|
|
}
|
|
|
|
|
2019-02-09 18:40:14 +03:00
|
|
|
TorrentInfo::PieceRange TorrentInfo::filePieces(const int fileIndex) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return {};
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if ((fileIndex < 0) || (fileIndex >= filesCount()))
|
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
qDebug() << "File index (" << fileIndex << ") is out of range for torrent" << name();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:22:39 +03:00
|
|
|
const lt::file_storage &files = nativeInfo()->files();
|
2020-06-26 03:05:09 +03:00
|
|
|
const auto fileSize = files.file_size(lt::file_index_t {fileIndex});
|
|
|
|
const auto fileOffset = files.file_offset(lt::file_index_t {fileIndex});
|
2019-05-13 21:42:44 +03:00
|
|
|
|
|
|
|
const int beginIdx = (fileOffset / pieceLength());
|
|
|
|
const int endIdx = ((fileOffset + fileSize - 1) / pieceLength());
|
|
|
|
|
|
|
|
if (fileSize <= 0)
|
|
|
|
return {beginIdx, 0};
|
|
|
|
return makeInterval(beginIdx, endIdx);
|
2016-02-29 02:44:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:12:38 +03:00
|
|
|
void TorrentInfo::renameFile(const int index, const QString &newPath)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
if (!isValid()) return;
|
2020-06-26 03:05:09 +03:00
|
|
|
nativeInfo()->rename_file(lt::file_index_t {index}, Utils::Fs::toNativePath(newPath).toStdString());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:37:00 +03:00
|
|
|
int TorrentInfo::fileIndex(const QString &fileName) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
2018-04-14 22:53:45 +03:00
|
|
|
// the check whether the object is valid is not needed here
|
|
|
|
// because if filesCount() returns -1 the loop exits immediately
|
2016-02-29 02:44:08 +03:00
|
|
|
for (int i = 0; i < filesCount(); ++i)
|
|
|
|
if (fileName == filePath(i))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-19 09:41:33 +03:00
|
|
|
QString TorrentInfo::rootFolder() const
|
2017-07-28 12:13:57 +03:00
|
|
|
{
|
2020-12-10 09:54:27 +03:00
|
|
|
return getRootFolder(filePaths());
|
2018-05-19 09:41:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentInfo::hasRootFolder() const
|
|
|
|
{
|
|
|
|
return !rootFolder().isEmpty();
|
2017-07-28 12:13:57 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:54:27 +03:00
|
|
|
void TorrentInfo::setContentLayout(const TorrentContentLayout layout)
|
2015-11-07 21:44:53 +03:00
|
|
|
{
|
2020-12-10 09:54:27 +03:00
|
|
|
switch (layout)
|
|
|
|
{
|
|
|
|
case TorrentContentLayout::Original:
|
|
|
|
setContentLayout(defaultContentLayout());
|
|
|
|
break;
|
|
|
|
case TorrentContentLayout::Subfolder:
|
|
|
|
if (rootFolder().isEmpty())
|
|
|
|
addRootFolder();
|
|
|
|
break;
|
|
|
|
case TorrentContentLayout::NoSubfolder:
|
|
|
|
if (!rootFolder().isEmpty())
|
|
|
|
stripRootFolder();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 16:00:50 +03:00
|
|
|
|
2020-12-10 09:54:27 +03:00
|
|
|
void TorrentInfo::stripRootFolder()
|
|
|
|
{
|
2019-05-09 07:45:52 +03:00
|
|
|
lt::file_storage files = m_nativeInfo->files();
|
2016-01-04 16:00:50 +03:00
|
|
|
|
|
|
|
// Solution for case of renamed root folder
|
2019-08-06 18:07:57 +03:00
|
|
|
const QString path = filePath(0);
|
|
|
|
const std::string newName = path.left(path.indexOf('/')).toStdString();
|
2020-11-16 10:02:11 +03:00
|
|
|
if (files.name() != newName)
|
|
|
|
{
|
2019-08-06 18:07:57 +03:00
|
|
|
files.set_name(newName);
|
2016-01-04 16:00:50 +03:00
|
|
|
for (int i = 0; i < files.num_files(); ++i)
|
2020-06-26 03:05:09 +03:00
|
|
|
files.rename_file(lt::file_index_t {i}, files.file_path(lt::file_index_t {i}));
|
2016-01-04 16:00:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
files.set_name("");
|
|
|
|
m_nativeInfo->remap_files(files);
|
2015-11-07 21:44:53 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 09:54:27 +03:00
|
|
|
void TorrentInfo::addRootFolder()
|
|
|
|
{
|
2021-01-18 10:18:40 +03:00
|
|
|
const QString originalName = name();
|
|
|
|
Q_ASSERT(!originalName.isEmpty());
|
|
|
|
|
|
|
|
const QString extension = Utils::Fs::fileExtension(originalName);
|
|
|
|
const QString rootFolder = extension.isEmpty()
|
|
|
|
? originalName
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
|
|
|
: originalName.chopped(extension.size() + 1);
|
|
|
|
#else
|
|
|
|
: originalName.left(originalName.size() - (extension.size() + 1));
|
|
|
|
#endif
|
2020-12-10 09:54:27 +03:00
|
|
|
const std::string rootPrefix = Utils::Fs::toNativePath(rootFolder + QLatin1Char {'/'}).toStdString();
|
|
|
|
lt::file_storage files = m_nativeInfo->files();
|
|
|
|
files.set_name(rootFolder.toStdString());
|
|
|
|
for (int i = 0; i < files.num_files(); ++i)
|
|
|
|
files.rename_file(lt::file_index_t {i}, rootPrefix + files.file_path(lt::file_index_t {i}));
|
|
|
|
m_nativeInfo->remap_files(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentContentLayout TorrentInfo::defaultContentLayout() const
|
|
|
|
{
|
|
|
|
QStringList origFilePaths;
|
|
|
|
origFilePaths.reserve(filesCount());
|
|
|
|
for (int i = 0; i < filesCount(); ++i)
|
|
|
|
origFilePaths << origFilePath(i);
|
|
|
|
|
|
|
|
const QString origRootFolder = getRootFolder(origFilePaths);
|
|
|
|
return (origRootFolder.isEmpty()
|
|
|
|
? TorrentContentLayout::NoSubfolder
|
|
|
|
: TorrentContentLayout::Subfolder);
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:05:09 +03:00
|
|
|
std::shared_ptr<lt::torrent_info> TorrentInfo::nativeInfo() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
return m_nativeInfo;
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|