/// <summary> /// Gets a file or directory. /// </summary> /// <param name="nameSpace">namespace of the name</param> /// <param name="name">name</param> /// <returns> /// The meta info of the file(s) downloaded. /// </returns> public DataMetaInfo Get(string nameSpace, string name) { string downloadPath; var torrentBytes = _manager.GetData(nameSpace, name, out downloadPath); return(MakeDataMetaInfo(downloadPath, torrentBytes, false)); }
/// <summary> /// Gets a block of data. /// </summary> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <param name="offset">The offset.</param> /// <param name="bytesToRead">The bytes to read.</param> /// <param name="waitHandle">The wait handle.</param> /// <returns></returns> public byte[] GetDataBlock(string nameSpace, string name, long offset, int bytesToRead) { // Download whole torrent. var wholeTorrentBytes = _torrentHelper.ReadOrDownloadTorrent( nameSpace, name, _dhtProxy); Torrent wholeTorrent = Torrent.Load(wholeTorrentBytes); // Find out the piece to download. int pieceIndex = (int)Math.Floor(offset / (double)wholeTorrent.PieceLength); var pieceName = MakePieceDataName(name, pieceIndex); int offsetInPiece = (int)(offset % wholeTorrent.PieceLength); // If the piece is already downloaded, we just return it. string piecePath = _bittorrentCache.GetPathOfItemInDownloads( nameSpace, pieceName); if (IOUtil.FileOrDirectoryExists(piecePath)) { return(IOUtil.Read(piecePath, offsetInPiece, bytesToRead)); } var pieceTorrentBytes = DownloadPieceTorrent(nameSpace, name, wholeTorrent, pieceIndex); // Write it to filesys so that the ordinary torrent downloading can pick it up. var pieceTorrentPath = _torrentHelper.WriteTorrentFile(nameSpace, pieceName, pieceTorrentBytes); Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "Piece torrent downloaded and written to path {0}", pieceTorrentPath)); // Download as a regular torrent. string pieceDownloadPath; _manager.GetData(nameSpace, pieceName, out pieceDownloadPath); return(IOUtil.Read(pieceDownloadPath, offsetInPiece, bytesToRead)); }