/// <summary>
 /// Publishes a file or directory.
 /// </summary>
 /// <param name="nameSpace">The name space.</param>
 /// <param name="name">The name.</param>
 /// <remarks>Uses the name of the directory of file as the publishing name.
 /// Used when you want to publish file/directory already in the Cache folder.
 /// </remarks>
 void Publish(string nameSpace, string name, bool unique)
 {
     if (unique)
     {
         _manager.PublishData(nameSpace, name);
     }
     else
     {
         _manager.UpdateData(nameSpace, name);
     }
 }
        /// <summary>
        /// Serves the torrent piece.
        /// </summary>
        /// <param name="nameSpace">The name space.</param>
        /// <param name="name">The name.</param>
        /// <param name="pieceIndex">Index of the piece.</param>
        /// <exception cref="ArgumentException">The torrent is not a single file torrent.
        /// </exception>
        public void ServePiece(string nameSpace, string name, int pieceIndex)
        {
            string pieceName = MakePieceDataName(name, pieceIndex);

            if (IOUtil.FileOrDirectoryExists(_bittorrentCache.GetTorrentFilePath(
                                                 nameSpace, pieceName)))
            {
                // It is already being served.
                return;
            }

            var torrent = Torrent.Load(_bittorrentCache.GetTorrentFilePath(nameSpace,
                                                                           name));

            if (torrent.Files.Length != 1)
            {
                throw new ArgumentException(
                          "This name specifies a multi-file torrent but only single file torrent is allowed.",
                          "name");
            }

            var offset = pieceIndex * torrent.PieceLength;

            byte[] pieceData = IOUtil.Read(_bittorrentCache.GetPathOfItemInDownloads(
                                               nameSpace, name), offset, torrent.PieceLength, torrent.Size);

            var piecePath = _bittorrentCache.GetPathOfItemInDownloads(nameSpace,
                                                                      pieceName);

            File.WriteAllBytes(piecePath, pieceData);
            Logger.WriteLineIf(LogLevel.Info, _log_props,
                               string.Format("Start to serve the piece {0}.", pieceName));
            _manager.UpdateData(nameSpace, pieceName);
        }