Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / bittorrent / torrentdescriptor.cpp
blob42e969b0d5975f98b1bbae49e2acb1cedeb55896
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "torrentdescriptor.h"
31 #include <libtorrent/load_torrent.hpp>
32 #include <libtorrent/magnet_uri.hpp>
33 #include <libtorrent/torrent_info.hpp>
34 #include <libtorrent/version.hpp>
35 #include <libtorrent/write_resume_data.hpp>
37 #include <QByteArray>
38 #include <QDateTime>
39 #include <QRegularExpression>
40 #include <QString>
41 #include <QUrl>
43 #include "base/global.h"
44 #include "base/preferences.h"
45 #include "base/utils/io.h"
46 #include "infohash.h"
47 #include "trackerentry.h"
49 namespace
51 // BEP9 Extension for Peers to Send Metadata Files
53 bool isV1Hash(const QString &string)
55 // There are 2 representations for BitTorrent v1 info hash:
56 // 1. 40 chars hex-encoded string
57 // == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
58 // 2. 32 chars Base32 encoded string
59 // == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
60 const int V1_HEX_SIZE = SHA1Hash::length() * 2;
61 const int V1_BASE32_SIZE = SHA1Hash::length() * 1.6;
63 return ((((string.size() == V1_HEX_SIZE)) && !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s)))
64 || ((string.size() == V1_BASE32_SIZE) && !string.contains(QRegularExpression(u"[^2-7A-Za-z]"_s))));
67 bool isV2Hash(const QString &string)
69 // There are 1 representation for BitTorrent v2 info hash:
70 // 1. 64 chars hex-encoded string
71 // == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
72 const int V2_HEX_SIZE = SHA256Hash::length() * 2;
74 return (string.size() == V2_HEX_SIZE) && !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s));
77 lt::load_torrent_limits loadTorrentLimits()
79 const auto *pref = Preferences::instance();
81 lt::load_torrent_limits limits;
82 limits.max_buffer_size = static_cast<int>(pref->getTorrentFileSizeLimit());
83 limits.max_decode_depth = pref->getBdecodeDepthLimit();
84 limits.max_decode_tokens = pref->getBdecodeTokenLimit();
86 return limits;
90 const int TORRENTDESCRIPTOR_TYPEID = qRegisterMetaType<BitTorrent::TorrentDescriptor>();
92 nonstd::expected<BitTorrent::TorrentDescriptor, QString>
93 BitTorrent::TorrentDescriptor::load(const QByteArray &data) noexcept
94 try
96 return TorrentDescriptor(lt::load_torrent_buffer(lt::span<const char>(data.data(), data.size()), loadTorrentLimits()));
98 catch (const lt::system_error &err)
100 return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
103 nonstd::expected<BitTorrent::TorrentDescriptor, QString>
104 BitTorrent::TorrentDescriptor::loadFromFile(const Path &path) noexcept
107 return TorrentDescriptor(lt::load_torrent_file(path.toString().toStdString(), loadTorrentLimits()));
109 catch (const lt::system_error &err)
111 return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
114 nonstd::expected<BitTorrent::TorrentDescriptor, QString>
115 BitTorrent::TorrentDescriptor::parse(const QString &str) noexcept
118 QString magnetURI = str;
119 if (isV2Hash(str))
120 magnetURI = u"magnet:?xt=urn:btmh:1220" + str; // 0x12 0x20 is the "multihash format" tag for the SHA-256 hashing scheme.
121 else if (isV1Hash(str))
122 magnetURI = u"magnet:?xt=urn:btih:" + str;
124 return TorrentDescriptor(lt::parse_magnet_uri(magnetURI.toStdString()));
126 catch (const lt::system_error &err)
128 return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
131 nonstd::expected<void, QString> BitTorrent::TorrentDescriptor::saveToFile(const Path &path) const
134 const lt::entry torrentEntry = lt::write_torrent_file(m_ltAddTorrentParams);
135 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, torrentEntry);
136 if (!result)
137 return result.get_unexpected();
139 return {};
141 catch (const lt::system_error &err)
143 return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
146 BitTorrent::TorrentDescriptor::TorrentDescriptor(lt::add_torrent_params ltAddTorrentParams)
147 : m_ltAddTorrentParams {std::move(ltAddTorrentParams)}
149 if (m_ltAddTorrentParams.ti && m_ltAddTorrentParams.ti->is_valid())
150 m_info.emplace(*m_ltAddTorrentParams.ti);
153 BitTorrent::InfoHash BitTorrent::TorrentDescriptor::infoHash() const
155 #ifdef QBT_USES_LIBTORRENT2
156 return m_ltAddTorrentParams.info_hashes;
157 #else
158 return m_ltAddTorrentParams.info_hash;
159 #endif
162 QString BitTorrent::TorrentDescriptor::name() const
164 return m_info ? m_info->name() : QString::fromStdString(m_ltAddTorrentParams.name);
167 QDateTime BitTorrent::TorrentDescriptor::creationDate() const
169 return ((m_ltAddTorrentParams.ti->creation_date() != 0)
170 ? QDateTime::fromSecsSinceEpoch(m_ltAddTorrentParams.ti->creation_date()) : QDateTime());
173 QString BitTorrent::TorrentDescriptor::creator() const
175 return QString::fromStdString(m_ltAddTorrentParams.ti->creator());
178 QString BitTorrent::TorrentDescriptor::comment() const
180 return QString::fromStdString(m_ltAddTorrentParams.ti->comment());
183 const std::optional<BitTorrent::TorrentInfo> &BitTorrent::TorrentDescriptor::info() const
185 return m_info;
188 void BitTorrent::TorrentDescriptor::setTorrentInfo(TorrentInfo torrentInfo)
190 if (!torrentInfo.isValid())
192 m_info.reset();
193 m_ltAddTorrentParams.ti.reset();
195 else
197 m_info = std::move(torrentInfo);
198 m_ltAddTorrentParams.ti = m_info->nativeInfo();
199 #ifdef QBT_USES_LIBTORRENT2
200 m_ltAddTorrentParams.info_hashes = m_ltAddTorrentParams.ti->info_hashes();
201 #else
202 m_ltAddTorrentParams.info_hash = m_ltAddTorrentParams.ti->info_hash();
203 #endif
207 QList<BitTorrent::TrackerEntry> BitTorrent::TorrentDescriptor::trackers() const
209 QList<TrackerEntry> ret;
210 ret.reserve(static_cast<decltype(ret)::size_type>(m_ltAddTorrentParams.trackers.size()));
211 std::size_t i = 0;
212 for (const std::string &tracker : m_ltAddTorrentParams.trackers)
213 ret.append({QString::fromStdString(tracker), m_ltAddTorrentParams.tracker_tiers[i++]});
215 return ret;
218 QList<QUrl> BitTorrent::TorrentDescriptor::urlSeeds() const
220 QList<QUrl> urlSeeds;
221 urlSeeds.reserve(static_cast<decltype(urlSeeds)::size_type>(m_ltAddTorrentParams.url_seeds.size()));
223 for (const std::string &nativeURLSeed : m_ltAddTorrentParams.url_seeds)
224 urlSeeds.append(QUrl(QString::fromStdString(nativeURLSeed)));
226 return urlSeeds;
229 const libtorrent::add_torrent_params &BitTorrent::TorrentDescriptor::ltAddTorrentParams() const
231 return m_ltAddTorrentParams;