Ejemplo n.º 1
0
        void createTorrent(TorrentCreationViewModel vm)
        {
            /*
             *  info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details)
             *  announce: The announce URL of the tracker (string)
             *  announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings).
             *      The official request for a specification change is here.
             *  creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
             *  comment: (optional) free-form textual comments of the author (string)
             *  created by: (optional) name and version of the program used to create the .torrent (string)
             *  encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string)
             */

            BDictionary info = new BDictionary();

            info["pieces"]       = new BString(buildPiecesHash(vm.Media));
            info["piece length"] = new BInteger(pieceLength);
            info["private"]      = new BInteger(vm.IsPrivate ? 1 : 0);

            if (vm.Media.Count == 1)
            {
                singleFileTorrent(info, new FileInfo(vm.Media.ElementAt(0).Location));
            }
            else
            {
                List <FileInfo> fileInfo = new List <FileInfo>();

                foreach (MediaFileItem item in vm.Media)
                {
                    fileInfo.Add(new FileInfo(item.Location));
                }

                multiFileTorrent(info, vm, fileInfo);
            }

            BDictionary metaInfo = new BDictionary();

            metaInfo["info"]          = info;
            metaInfo["announce"]      = new BString(vm.AnnounceURL);
            metaInfo["creation date"] = new BInteger((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds);
            metaInfo["created by"]    = new BString(createdBy);

            if (!String.IsNullOrEmpty(vm.Comment) && !String.IsNullOrWhiteSpace(vm.Comment) && vm.IsCommentEnabled)
            {
                metaInfo["comment"] = new BString(vm.Comment);
            }

            if (!String.IsNullOrEmpty(encoding) && !String.IsNullOrWhiteSpace(encoding) && vm.IsCommentEnabled)
            {
                metaInfo["encoding"] = new BString(encoding);
            }

            String torrentName = String.IsNullOrEmpty(vm.TorrentName) ?
                                 Path.GetFileNameWithoutExtension(vm.Media.ElementAt(0).Location) : vm.TorrentName;
            String torrentFullName = vm.OutputPath + "\\" + torrentName + ".torrent";

            if (CancellationToken.IsCancellationRequested)
            {
                return;
            }

            FileStream outputTorrent = null;

            try
            {
                outputTorrent = new FileStream(torrentFullName, FileMode.CreateNew);

                BinaryWriter bw = new BinaryWriter(outputTorrent);

                foreach (char c in metaInfo.ToBencodedString())
                {
                    bw.Write((byte)c);
                }

                InfoMessages.Add("Created torrent file: " + torrentFullName);
            }
            finally
            {
                if (outputTorrent != null)
                {
                    outputTorrent.Close();
                }
            }
        }
Ejemplo n.º 2
0
 void singleFileTorrent(BDictionary info, FileInfo file)
 {
     info["name"]   = new BString(file.Name);
     info["length"] = new BInteger(file.Length);
 }