Skip to content

Commit

Permalink
Merge pull request #2 from aliyun/fix_listContents
Browse files Browse the repository at this point in the history
fix listContens bug
  • Loading branch information
baiyubin2020 committed Oct 31, 2017
2 parents baa0249 + 0bea25b commit 4343770
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion samples/AliyunOssFlysystem.php
Expand Up @@ -71,7 +71,7 @@
// 拷贝oss文件
$filesystem->copy('2.txt', 'test/3.txt');

//
// 按前缀list, true是递归list出所有文件,false只list当前文件夹的文件
$result = $filesystem->listContents('', true);
echo "listContents result: ";
print_r($result);
Expand Down
9 changes: 8 additions & 1 deletion src/AliyunOssAdapter.php
Expand Up @@ -211,7 +211,7 @@ public function deleteDir($dirname)
if ($val['type'] === 'file') {
$objects[] = $this->applyPathPrefix($val['path']);
} else {
$objects[] = $this->applyPathPrefix($val['path']).'/';
$objects[] = $this->applyPathPrefix($val['path']) .'/';
}
}

Expand Down Expand Up @@ -276,6 +276,7 @@ public function read($path)
public function listContents($directory = '', $recursive = false)
{
$directory = $this->applyPathPrefix($directory);
$directory = $this->applyPathSeparator($directory);

$bucket = $this->bucket;
$delimiter = '/';
Expand Down Expand Up @@ -461,4 +462,10 @@ protected function doesDirectoryExist($object)

return $objectList || $prefixList;
}

protected function applyPathSeparator($path)
{
return rtrim($path, '\\/') . '/';
}

}
19 changes: 15 additions & 4 deletions tests/AliyunOssAdapterTest.php
Expand Up @@ -281,14 +281,25 @@ public function testListContents()
$this->filesystem->write($dir . '/2.txt', '123');
$this->filesystem->write($dir . '/3.txt', '123');
$this->filesystem->write($dir . '/secondlevel/4.txt', '123');
$this->filesystem->write($dir . '/secondlevel/5.txt', '123');


$list = $this->filesystem->listContents($dir, true);
$this->assertEquals(count($list), 4);
$this->assertEquals(count($list), 5);

$this->assertEquals($list[0]['path'], $dir . '/1.txt');
$this->assertEquals($list[1]['path'], $dir . '/2.txt');
$this->assertEquals($list[2]['path'], $dir . '/3.txt');
$this->assertEquals($list[3]['path'], $dir . '/secondlevel/4.txt');
$this->assertEquals($list[4]['path'], $dir . '/secondlevel/5.txt');

$list = $this->filesystem->listContents($dir, false);
$this->assertEquals(count($list), 0);

$this->assertEquals(count($list), 4);

$this->assertEquals($list[0]['path'], $dir . '/1.txt');
$this->assertEquals($list[1]['path'], $dir . '/2.txt');
$this->assertEquals($list[2]['path'], $dir . '/3.txt');
$this->assertEquals($list[3]['path'], $dir . '/secondlevel');

$this->filesystem->deleteDir($dir);
}

Expand Down

0 comments on commit 4343770

Please sign in to comment.