Skip to content

Commit 5b94cf1

Browse files
author
itismadness
committed
Add another function for Gazelle (0.9.2)
1 parent 74929fd commit 5b94cf1

File tree

2 files changed

+93
-20
lines changed

2 files changed

+93
-20
lines changed

src/BencodeTorrent.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -426,33 +426,33 @@ public function getFileList() : array {
426426
$this->data['info']['name.utf-8'] :
427427
$this->data['info']['name']);
428428
$size = $this->data['info']['length'];
429-
$files[] = array('name' => $name, 'size' => $size);
429+
$files[] = ['path' => $name, 'size' => $size];
430430
}
431431
else {
432-
$path_key = isset($this->data['info']['files'][0]['path.utf-8']) ?
433-
'path.utf-8' :
434-
'path';
432+
$size = 0;
435433
foreach ($this->data['info']['files'] as $file) {
436-
$tmp_path = array();
437-
foreach ($file[$path_key] as $sub_path) {
438-
$tmp_path[] = $sub_path;
439-
}
440-
$files[] = array('name' => implode('/', $tmp_path), 'size' => $file['length']);
434+
$size += $file['length'];
435+
$path_key = isset($file['path.utf-8']) ? 'path.utf-8' : 'path';
436+
$files[] = ['path' => implode('/', $file[$path_key]), 'size' => $file['length']];
441437
}
442-
uasort(
438+
usort(
443439
$files,
444440
function ($a, $b) {
445-
return strnatcasecmp($a['name'], $b['name']);
441+
return strnatcasecmp($a['path'], $b['path']);
446442
}
447443
);
448444
}
449-
return $files;
445+
return array('total_size' => $size, 'files' => $files);
450446
}
451447

452448
public function hasFiles(): bool {
453449
return isset($this->data['info']['files']);
454450
}
455451

452+
public function hasEncryptedFiles(): bool {
453+
return isset($this->data['encrypted_files']);
454+
}
455+
456456
/**
457457
* Returns an array of strings formatted to be inserted into a Gazelle database into the table
458458
* torrents.FileList which is then used for displaying the table of files to the user when
@@ -465,15 +465,15 @@ public function hasFiles(): bool {
465465
*/
466466
public function getGazelleFileList() : array {
467467
$files = [];
468-
foreach ($this->getFileList() as $file) {
469-
$name = $file['name'];
468+
foreach ($this->getFileList()['files'] as $file) {
469+
$path = $file['path'];
470470
$size = $file['size'];
471-
$name = $this->makeUTF8(strtr($name, "\n\r\t", ' '));
472-
$ext_pos = strrpos($name, '.');
471+
$path = $this->makeUTF8(strtr($path, "\n\r\t", ' '));
472+
$ext_pos = strrpos($path, '.');
473473
// Should not be $ExtPos !== false. Extension-less files that start with a .
474474
// should not get extensions
475-
$ext = ($ext_pos ? trim(substr($name, $ext_pos + 1)) : '');
476-
$files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $name, self::$utf8_filelist_delim);
475+
$ext = ($ext_pos ? trim(substr($path, $ext_pos + 1)) : '');
476+
$files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $path, self::$utf8_filelist_delim);
477477
}
478478
return $files;
479479
}

tests/BencodeTorrentTest.php

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,57 @@ public function testLoadTorrent() {
9292
$this->assertStringEqualsFile(__DIR__.'/data/test_1.torrent', $bencode->getEncode());
9393
$this->assertEquals('1f830103427029a88dd5fde85be74e622ee07951', $bencode->getInfoHash());
9494
$this->assertEquals($bencode->getInfoHash(), unpack('H*', $bencode->getHexInfoHash())[1]);
95+
$this->assertFalse($bencode->hasEncryptedFiles());
96+
$file_list = [
97+
'total_size' => 104916260,
98+
'files' => [
99+
[
100+
'size' => 9584196,
101+
'path' => '01 Death with Dignity.mp3'
102+
],
103+
[
104+
'size' => 12310347,
105+
'path' => '02 Should have known better.mp3'
106+
],
107+
[
108+
'size' => 8871591,
109+
'path' => '03 All of me wants all of you.mp3'
110+
],
111+
[
112+
'size' => 7942661,
113+
'path' => '04 Drawn to the Blood.mp3'
114+
],
115+
[
116+
'size' => 5878964,
117+
'path' => '05 Eugene.mp3'
118+
],
119+
[
120+
'size' => 11175567,
121+
'path' => '06 Fourth of July.mp3'
122+
],
123+
[
124+
'size' => 11367829,
125+
'path' => '07 The Only Thing.mp3'
126+
],
127+
[
128+
'size' => 7789055,
129+
'path' => '08 Carrie & Lowell.mp3'
130+
],
131+
[
132+
'size' => 12197480,
133+
'path' => '09 John My Beloved.mp3'
134+
],
135+
[
136+
'size' => 6438044,
137+
'path' => '10 No shade in the shadow of the cross.mp3'
138+
],
139+
[
140+
'size' => 11360526,
141+
'path' => '11 Blue Bucket of Gold.mp3'
142+
]
143+
]
144+
];
145+
$this->assertEquals($file_list, $bencode->getFileList());
95146
}
96147

97148
public function testSetData() {
@@ -112,7 +163,18 @@ public function testSetData() {
112163
$this->assertEquals('test', $bencode->getName());
113164
$this->assertFalse($bencode->isPrivate());
114165
$this->assertEquals(1213134, $bencode->getSize());
115-
$this->assertEquals([['name' => 'test', 'size' => 1213134]], $bencode->getFileList());
166+
$this->assertEquals(
167+
[
168+
'total_size' => 1213134,
169+
'files' => [
170+
[
171+
'path' => 'test',
172+
'size' => 1213134
173+
]
174+
]
175+
],
176+
$bencode->getFileList()
177+
);
116178
}
117179

118180
public function testEmptyDictionary() {
@@ -327,7 +389,18 @@ public function testGetUtf8Name() {
327389
$bencode->setData($data);
328390
$this->assertEquals('test!!', $bencode->getName());
329391
$this->assertEquals(12345, $bencode->getSize());
330-
$this->assertEquals([['name' => 'test!!', 'size' => 12345]], $bencode->getFileList());
392+
$this->assertEquals(
393+
[
394+
'total_size' => 12345,
395+
'files' => [
396+
[
397+
'path' => 'test!!',
398+
'size' => 12345
399+
]
400+
]
401+
],
402+
$bencode->getFileList()
403+
);
331404
$this->assertEquals(['. s12345s test!! ÷'], $bencode->getGazelleFileList());
332405
}
333406

0 commit comments

Comments
 (0)