Support creating .torrent with larger piece size

Warning: users are at their own discretion to create .torrent with >= 64 MiB piece size as not
every torrent client supports it.
Larger piece sizes are only available when using libtorrent 2.x. libtorrent 1.x is not
efficient with memory usage and in order to avoid user complaints it is limited to 128 MiB.
Also note that, as of this writing, libtorrent 2.0.9 has an internal limitation that only
allows loading maximum 256 MiB piece size. And therefore > 256 MiB size options are forbidden
for now.

Closes #19527.
PR #19535.
This commit is contained in:
Chocobo1 2023-09-03 14:31:32 +08:00 committed by GitHub
parent 2dc1a7d66f
commit 4d2015cfed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 76 deletions

View file

@ -259,12 +259,14 @@ QString Utils::Misc::unitString(const SizeUnit unit, const bool isSpeed)
return ret;
}
QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed)
QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed, const int precision)
{
const std::optional<SplitToFriendlyUnitResult> result = splitToFriendlyUnit(bytes);
if (!result)
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
return Utils::String::fromDouble(result->value, friendlyUnitPrecision(result->unit))
const int digitPrecision = (precision >= 0) ? precision : friendlyUnitPrecision(result->unit);
return Utils::String::fromDouble(result->value, digitPrecision)
+ C_NON_BREAKING_SPACE
+ unitString(result->unit, isSpeed);
}

View file

@ -74,7 +74,7 @@ namespace Utils::Misc
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
// value must be given in bytes
QString friendlyUnit(qint64 bytes, bool isSpeed = false);
QString friendlyUnit(qint64 bytes, bool isSpeed = false, int precision = -1);
int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit);

View file

@ -39,6 +39,7 @@
#include "base/bittorrent/torrentdescriptor.h"
#include "base/global.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "ui_torrentcreatordialog.h"
#include "utils.h"
@ -80,6 +81,18 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const Path &defaultP
{
m_ui->setupUi(this);
m_ui->comboPieceSize->addItem(tr("Auto"), 0);
#ifdef QBT_USES_LIBTORRENT2
for (int i = 4; i <= 18; ++i)
#else
for (int i = 4; i <= 17; ++i)
#endif
{
const int size = 1024 << i;
const QString displaySize = Utils::Misc::friendlyUnit(size, false, 0);
m_ui->comboPieceSize->addItem(displaySize, size);
}
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
m_ui->textInputPath->setMode(FileSystemPathEdit::Mode::ReadOnly);
@ -133,8 +146,7 @@ void TorrentCreatorDialog::onAddFileButtonClicked()
int TorrentCreatorDialog::getPieceSize() const
{
const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB
return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024;
return m_ui->comboPieceSize->currentData().toInt();
}
#ifdef QBT_USES_LIBTORRENT2
@ -251,15 +263,19 @@ void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &branchPath)
{
// Remove busy cursor
setCursor(QCursor(Qt::ArrowCursor));
setInteractionEnabled(true);
QMessageBox::information(this, tr("Torrent creator")
, u"%1\n%2"_s.arg(tr("Torrent created:"), path.toString()));
if (m_ui->checkStartSeeding->isChecked())
{
// Create save path temp data
const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(path);
if (!loadResult)
{
QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
const QString message = tr("Add torrent to transfer list failed.") + u'\n' + tr("Reason: \"%1\"").arg(loadResult.error());
QMessageBox::critical(this, tr("Add torrent failed"), message);
return;
}
@ -276,9 +292,6 @@ void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &b
BitTorrent::Session::instance()->addTorrent(loadResult.value(), params);
}
QMessageBox::information(this, tr("Torrent creator")
, u"%1\n%2"_s.arg(tr("Torrent created:"), path.toString()));
setInteractionEnabled(true);
}
void TorrentCreatorDialog::updateProgressBar(int progress)
@ -308,6 +321,7 @@ void TorrentCreatorDialog::setInteractionEnabled(const bool enabled) const
m_ui->trackersList->setEnabled(enabled);
m_ui->URLSeedsList->setEnabled(enabled);
m_ui->txtComment->setEnabled(enabled);
m_ui->lineEditSource->setEnabled(enabled);
m_ui->comboPieceSize->setEnabled(enabled);
m_ui->buttonCalcTotalPieces->setEnabled(enabled);
m_ui->checkPrivate->setEnabled(enabled);

View file

@ -194,71 +194,6 @@
<property name="minimumContentsLength">
<number>7</number>
</property>
<item>
<property name="text">
<string>Auto</string>
</property>
</item>
<item>
<property name="text">
<string>16 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>32 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>64 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>128 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>256 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>512 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>1 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>2 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>4 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>8 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>16 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>32 MiB</string>
</property>
</item>
</widget>
</item>
<item>