diff --git a/src/BencodeTorrent.php b/src/BencodeTorrent.php index dc91ad2..b6fd9a8 100644 --- a/src/BencodeTorrent.php +++ b/src/BencodeTorrent.php @@ -426,33 +426,33 @@ public function getFileList() : array { $this->data['info']['name.utf-8'] : $this->data['info']['name']); $size = $this->data['info']['length']; - $files[] = array('name' => $name, 'size' => $size); + $files[] = ['path' => $name, 'size' => $size]; } else { - $path_key = isset($this->data['info']['files'][0]['path.utf-8']) ? - 'path.utf-8' : - 'path'; + $size = 0; foreach ($this->data['info']['files'] as $file) { - $tmp_path = array(); - foreach ($file[$path_key] as $sub_path) { - $tmp_path[] = $sub_path; - } - $files[] = array('name' => implode('/', $tmp_path), 'size' => $file['length']); + $size += $file['length']; + $path_key = isset($file['path.utf-8']) ? 'path.utf-8' : 'path'; + $files[] = ['path' => implode('/', $file[$path_key]), 'size' => $file['length']]; } - uasort( + usort( $files, function ($a, $b) { - return strnatcasecmp($a['name'], $b['name']); + return strnatcasecmp($a['path'], $b['path']); } ); } - return $files; + return array('total_size' => $size, 'files' => $files); } public function hasFiles(): bool { return isset($this->data['info']['files']); } + public function hasEncryptedFiles(): bool { + return isset($this->data['encrypted_files']); + } + /** * Returns an array of strings formatted to be inserted into a Gazelle database into the table * torrents.FileList which is then used for displaying the table of files to the user when @@ -465,15 +465,15 @@ public function hasFiles(): bool { */ public function getGazelleFileList() : array { $files = []; - foreach ($this->getFileList() as $file) { - $name = $file['name']; + foreach ($this->getFileList()['files'] as $file) { + $path = $file['path']; $size = $file['size']; - $name = $this->makeUTF8(strtr($name, "\n\r\t", ' ')); - $ext_pos = strrpos($name, '.'); + $path = $this->makeUTF8(strtr($path, "\n\r\t", ' ')); + $ext_pos = strrpos($path, '.'); // Should not be $ExtPos !== false. Extension-less files that start with a . // should not get extensions - $ext = ($ext_pos ? trim(substr($name, $ext_pos + 1)) : ''); - $files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $name, self::$utf8_filelist_delim); + $ext = ($ext_pos ? trim(substr($path, $ext_pos + 1)) : ''); + $files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $path, self::$utf8_filelist_delim); } return $files; } diff --git a/tests/BencodeTorrentTest.php b/tests/BencodeTorrentTest.php index e12fe91..8de575b 100644 --- a/tests/BencodeTorrentTest.php +++ b/tests/BencodeTorrentTest.php @@ -92,6 +92,57 @@ public function testLoadTorrent() { $this->assertStringEqualsFile(__DIR__.'/data/test_1.torrent', $bencode->getEncode()); $this->assertEquals('1f830103427029a88dd5fde85be74e622ee07951', $bencode->getInfoHash()); $this->assertEquals($bencode->getInfoHash(), unpack('H*', $bencode->getHexInfoHash())[1]); + $this->assertFalse($bencode->hasEncryptedFiles()); + $file_list = [ + 'total_size' => 104916260, + 'files' => [ + [ + 'size' => 9584196, + 'path' => '01 Death with Dignity.mp3' + ], + [ + 'size' => 12310347, + 'path' => '02 Should have known better.mp3' + ], + [ + 'size' => 8871591, + 'path' => '03 All of me wants all of you.mp3' + ], + [ + 'size' => 7942661, + 'path' => '04 Drawn to the Blood.mp3' + ], + [ + 'size' => 5878964, + 'path' => '05 Eugene.mp3' + ], + [ + 'size' => 11175567, + 'path' => '06 Fourth of July.mp3' + ], + [ + 'size' => 11367829, + 'path' => '07 The Only Thing.mp3' + ], + [ + 'size' => 7789055, + 'path' => '08 Carrie & Lowell.mp3' + ], + [ + 'size' => 12197480, + 'path' => '09 John My Beloved.mp3' + ], + [ + 'size' => 6438044, + 'path' => '10 No shade in the shadow of the cross.mp3' + ], + [ + 'size' => 11360526, + 'path' => '11 Blue Bucket of Gold.mp3' + ] + ] + ]; + $this->assertEquals($file_list, $bencode->getFileList()); } public function testSetData() { @@ -112,7 +163,18 @@ public function testSetData() { $this->assertEquals('test', $bencode->getName()); $this->assertFalse($bencode->isPrivate()); $this->assertEquals(1213134, $bencode->getSize()); - $this->assertEquals([['name' => 'test', 'size' => 1213134]], $bencode->getFileList()); + $this->assertEquals( + [ + 'total_size' => 1213134, + 'files' => [ + [ + 'path' => 'test', + 'size' => 1213134 + ] + ] + ], + $bencode->getFileList() + ); } public function testEmptyDictionary() { @@ -327,7 +389,18 @@ public function testGetUtf8Name() { $bencode->setData($data); $this->assertEquals('test!!', $bencode->getName()); $this->assertEquals(12345, $bencode->getSize()); - $this->assertEquals([['name' => 'test!!', 'size' => 12345]], $bencode->getFileList()); + $this->assertEquals( + [ + 'total_size' => 12345, + 'files' => [ + [ + 'path' => 'test!!', + 'size' => 12345 + ] + ] + ], + $bencode->getFileList() + ); $this->assertEquals(['. s12345s test!! รท'], $bencode->getGazelleFileList()); }