Add another function for Gazelle (0.9.2)

This commit is contained in:
itismadness
2018-04-12 21:59:30 +01:00
parent 74929fdac8
commit 5b94cf1056
2 changed files with 93 additions and 20 deletions

View File

@@ -426,33 +426,33 @@ class BencodeTorrent {
$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 @@ class BencodeTorrent {
*/
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;
}

View File

@@ -92,6 +92,57 @@ class BencodeTorrentTest extends TestCase {
$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 @@ class BencodeTorrentTest extends TestCase {
$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 @@ class BencodeTorrentTest extends TestCase {
$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());
}