From 553dd52404f59f33a41acce56b595c13c71e2bee Mon Sep 17 00:00:00 2001 From: David Neukirchen Date: Fri, 30 Dec 2016 15:10:14 +0100 Subject: [PATCH] Use the full path in the api response (#74) * Use the full path * Update tests * Use relative path --- .../libraries/media/file/adapter/local.php | 23 ++++++++++--------- .../libraries/file/LocalAdapterTest.php | 8 +++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/administrator/components/com_media/libraries/media/file/adapter/local.php b/administrator/components/com_media/libraries/media/file/adapter/local.php index e1e7c54f0fd96..cda48132e5409 100644 --- a/administrator/components/com_media/libraries/media/file/adapter/local.php +++ b/administrator/components/com_media/libraries/media/file/adapter/local.php @@ -64,24 +64,25 @@ public function __construct($rootPath) public function getFiles($path = '/') { // Set up the path correctly - $path = JPath::clean('/' . $path); + $path = JPath::clean('/' . $path); + $basePath = JPath::clean($this->rootPath . $path); // Check if file exists - if (!file_exists($this->rootPath . $path)) + if (!file_exists($basePath)) { return array(); } // Check if the path points to a file - if (is_file($this->rootPath . $path)) + if (is_file($basePath)) { // Create the file object $obj = new stdClass; $obj->type = 'file'; $obj->name = basename($path); - $obj->path = pathinfo($path, PATHINFO_DIRNAME); + $obj->path = str_replace($this->rootPath, '/', $basePath); $obj->extension = JFile::getExt($obj->name); - $obj->size = filesize($this->rootPath . $path); + $obj->size = filesize($basePath); return array($obj); } @@ -90,27 +91,27 @@ public function getFiles($path = '/') $data = array(); // Read the folders - foreach (JFolder::folders($this->rootPath . $path) as $folder) + foreach (JFolder::folders($basePath) as $folder) { $obj = new stdClass; $obj->type = 'dir'; $obj->name = $folder; - $obj->path = $path; + $obj->path = str_replace($this->rootPath, '/', JPath::clean($basePath . '/' . $folder)); - $data[] = $obj; + $data[] = $obj; } // Read the files - foreach (JFolder::files($this->rootPath . $path) as $file) + foreach (JFolder::files($basePath) as $file) { $obj = new stdClass; $obj->type = 'file'; $obj->name = $file; - $obj->path = $path; + $obj->path = str_replace($this->rootPath, '/', JPath::clean($basePath . '/' . $file)); $obj->extension = JFile::getExt($file); $obj->size = filesize($this->rootPath . $path); - $data[] = $obj; + $data[] = $obj; } // Return the data diff --git a/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php b/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php index 0b7354f3dbce9..f35143a56ca53 100644 --- a/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php +++ b/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php @@ -78,14 +78,14 @@ public function testGetFiles() $this->assertInstanceOf('stdClass', $files[0]); $this->assertEquals('dir', $files[0]->type); $this->assertEquals('unit', $files[0]->name); - $this->assertEquals('/', $files[0]->path); + $this->assertEquals('/unit', $files[0]->path); // Check the file $this->assertInstanceOf('stdClass', $files[1]); $this->assertEquals('file', $files[1]->type); $this->assertEquals('test.txt', $files[1]->name); $this->assertEquals('txt', $files[1]->extension); - $this->assertEquals('/', $files[1]->path); + $this->assertEquals('/test.txt', $files[1]->path); $this->assertGreaterThan(1, $files[1]->size); } @@ -114,7 +114,7 @@ public function testGetSingleFile() $this->assertEquals('file', $files[0]->type); $this->assertEquals('test.txt', $files[0]->name); $this->assertEquals('txt', $files[0]->extension); - $this->assertEquals('/', $files[0]->path); + $this->assertEquals('/test.txt', $files[0]->path); $this->assertGreaterThan(0, $files[0]->size); } @@ -143,7 +143,7 @@ public function testGetSingleFileSpecialPath() $this->assertEquals('file', $files[0]->type); $this->assertEquals('test.txt', $files[0]->name); $this->assertEquals('txt', $files[0]->extension); - $this->assertEquals('/', $files[0]->path); + $this->assertEquals('/test.txt', $files[0]->path); $this->assertGreaterThan(0, $files[0]->size); }