function setFile($file, $store_original_file = true) { if (!$file instanceof sfValidatedFile) { if (is_string($file)) { return parent::setFile($file); } } $filename = $this->formatFilename($file->getOriginalName()) . $file->getOriginalExtension(); $extension = $file->getOriginalExtension(); parent::setFile($filename); // fixme this isn't setting the whole path $this->setSize($file->getSize()); $this->setMimeType($file->getType()); $sha1 = method_exists($file, 'getFileSha1') ? $file->getFileSha1() : sha1_file($file->getTempName()); $this->setFileSha1($sha1); $torrent_file = $this->getTorrentPath(); if (file_exists($torrent_file)) { $this->cleanupFiles(); throw new limeException('torrent', "{$torrent_file} already exists"); parent::setFile(null); } if ($store_original_file) { $file->save($this->getOriginalFilePath(), 0644); } else { } // NB: formatFilename should probably put the real filename on the file // but we've factored this out $MakeTorrent = new File_Bittorrent2_MakeTorrent($file->isSaved() ? $file->getSavedName() : $file->getTempName()); $MakeTorrent->setAnnounce(url_for('client/announce', true)); $MakeTorrent->setComment('TODO'); $MakeTorrent->setPieceLength(256); // KiBw $info = array(); if ($file instanceof sfValidatedFileFromUrl) { $info['url-list'] = array($file->getUrl()); } $meta = $MakeTorrent->buildTorrent($info); if ($meta && file_put_contents($torrent_file, $meta)) { $File_Bittorrent2_Decode = new File_Bittorrent2_Decode(); $info = $File_Bittorrent2_Decode->decodeFile($torrent_file); $this->setInfoHash($info['info_hash']); if (!file_exists($torrent_file)) { $this->cleanupFiles(); throw new limeException('torrent', "{$torrent_file} not written"); } } else { $this->cleanupFiles(); throw new limeException('torrent', 'Unable to generate torrent'); } if ($file instanceof sfValidatedFileFromUrl) { $this->setWebUrl($file->getUrl()); } else { $this->setWebUrl(_compute_public_path($this->getFile(), 'uploads', '', true)); } }
public function testMakePrivateTorrent() { $Torrent = new File_Bittorrent2_MakeTorrent(__FILE__); $Torrent->setAnnounce('http://example.com/'); $Torrent->setIsPrivate(true); $tfile = tempnam('./', __CLASS__); $bemetainfo = $Torrent->buildTorrent(); file_put_contents($tfile, $bemetainfo); $Decode = new File_Bittorrent2_Decode(); $Decode->decodeFile($tfile); $metainfo = $Decode->decode($bemetainfo); unlink($tfile); $this->assertTrue($Decode->isPrivate()); $this->assertTrue($metainfo['info']['private'] === 1); }
public function testAnnounceList() { $MakeTorrent = new File_Bittorrent2_MakeTorrent(self::$torrent); // Set the announce URL $MakeTorrent->setAnnounce('http://www.example.org'); // Set the comment $MakeTorrent->setComment('Hello World!'); // Set the piece length (in KB) $MakeTorrent->setPieceLength(256); // Build the torrent $metainfo = $MakeTorrent->buildTorrent(); $Decode = new File_Bittorrent2_Decode(); $info = $Decode->decode($metainfo); $this->assertEquals(count($info['info']['files']), 3); $files = array(); foreach ($info['info']['files'] as $k => $v) { $files[] = $v['path'][0]; } sort($files); $expected = array('1.txt', '2.txt', '3.txt'); $this->assertEquals($expected, $files); }
<?php error_reporting(E_ALL); require_once 'File/Bittorrent2/MakeTorrent.php'; $MakeTorrent = new File_Bittorrent2_MakeTorrent('example.php'); // Set the announce URL $MakeTorrent->setAnnounce('http://www.example.org'); // Set the comment $MakeTorrent->setComment('Hello World!'); // Set the piece length (in KB) $MakeTorrent->setPieceLength(256); // Build the torrent $metainfo = $MakeTorrent->buildTorrent(); // Then put this into a file, instead of echoing it normally... echo $metainfo;