MagnetLink() public static method

public static MagnetLink ( object magnetLink ) : void
magnetLink object
return void
Esempio n. 1
0
        public static InfoHash FromMagnetLink(string magnetLink)
        {
            Check.MagnetLink(magnetLink);
            if (!magnetLink.StartsWith("magnet:?"))
            {
                throw new ArgumentException("Invalid magnet link format");
            }
            magnetLink = magnetLink.Substring("magnet:?".Length);
            var hashStart = magnetLink.IndexOf("xt=urn:btih:", StringComparison.Ordinal);

            if (hashStart == -1)
            {
                throw new ArgumentException("Magnet link does not contain an infohash");
            }
            hashStart += "xt=urn:btih:".Length;

            var hashEnd = magnetLink.IndexOf('&', hashStart);

            if (hashEnd == -1)
            {
                hashEnd = magnetLink.Length;
            }

            switch (hashEnd - hashStart)
            {
            case 32:
                return(FromBase32(magnetLink.Substring(hashStart, 32)));

            case 40:
                return(FromHex(magnetLink.Substring(hashStart, 40)));

            default:
                throw new ArgumentException("Infohash must be base32 or hex encoded.");
            }
        }