Skip to content

Commit

Permalink
Use the full path in the api response (#74)
Browse files Browse the repository at this point in the history
* Use the full path

* Update tests

* Use relative path
  • Loading branch information
dneukirchen authored and yvesh committed Dec 30, 2016
1 parent a027e11 commit 553dd52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 553dd52

Please sign in to comment.