lampac/Shared/Engine/JacRed/BencodeTo.cs
lampac-talks f843f04fd4 chore: initial commit 154.3
Signed-off-by: lampac-talks <lampac-talks@users.noreply.github.com>
2026-01-30 16:23:09 +03:00

67 lines
1.8 KiB
C#

using BencodeNET.Parsing;
using BencodeNET.Torrents;
using System.Text.RegularExpressions;
namespace Shared.Engine.JacRed
{
public static class BencodeTo
{
#region Magnet
public static string Magnet(byte[] torrent)
{
try
{
if (torrent == null)
return null;
var parser = new BencodeParser();
var res = parser.Parse<Torrent>(torrent);
string magnet = res.GetMagnetLink();
if (res.OriginalInfoHash != null)
magnet = Regex.Replace(magnet, @"urn:btih:[\w0-9]+", $"urn:btih:{res.OriginalInfoHash.ToLower()}", RegexOptions.IgnoreCase);
return magnet;
}
catch
{
return null;
}
}
#endregion
#region SizeName
public static string SizeName(byte[] torrent)
{
try
{
if (torrent == null)
return null;
var parser = new BencodeParser();
var res = parser.Parse<Torrent>(torrent);
string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return String.Format("{0:N2} {1}", dblSByte, Suffix[i]).Replace(",", ".");
}
return FormatBytes(res.TotalSize);
}
catch
{
return null;
}
}
#endregion
}
}