Chunking NG: Assemble in natural sort order of files

For https://github.com/owncloud/client/pull/5476

Before this, the assembly could be bogusly in the order 0,1,10,11,2,3 etc.

As per the spec "The name of every chunk should be its chunk number."
https://github.com/cernbox/smashbox/blob/master/protocol/chunking.md

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Markus Goetz 2017-01-20 17:46:04 +01:00 committed by Morris Jobke
parent 39afcbd49f
commit 075a606514
No known key found for this signature in database
GPG key ID: 9CE5ED29E7FCD38A
2 changed files with 13 additions and 1 deletions

View file

@ -67,7 +67,7 @@ class AssemblyStream implements \Icewind\Streams\File {
$nodes = $this->nodes;
// http://stackoverflow.com/a/10985500
@usort($nodes, function(IFile $a, IFile $b) {
return strcmp($a->getName(), $b->getName());
return strnatcmp($a->getName(), $b->getName());
});
$this->nodes = $nodes;

View file

@ -52,6 +52,15 @@ class AssemblyStreamTest extends \Test\TestCase {
function providesNodes() {
$data8k = $this->makeData(8192);
$dataLess8k = $this->makeData(8191);
$tonofnodes = [];
$tonofdata = "";
for ($i = 0; $i < 101; $i++) {
$thisdata = rand(0,100); // variable length and content
$tonofdata .= $thisdata;
array_push($tonofnodes, $this->buildNode($i,$thisdata));
}
return[
'one node zero bytes' => [
'', [
@ -90,6 +99,9 @@ class AssemblyStreamTest extends \Test\TestCase {
$this->buildNode('1', $data8k . 'X'),
$this->buildNode('0', $data8k)
]],
'a ton of nodes' => [
$tonofdata, $tonofnodes
]
];
}