Skip to content

Commit

Permalink
Add filter option in API (#80)
Browse files Browse the repository at this point in the history
* Add filter option

* Remove void return types

* Remove void return types
  • Loading branch information
laoneo authored and dneukirchen committed Feb 16, 2017
1 parent f9a8209 commit eefa6be
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
14 changes: 9 additions & 5 deletions administrator/components/com_media/controllers/api.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ public function __construct($config = array())
* - GET a list of files and subfolders of a given folder:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
* - GET a list of files and subfolders of a given folder for a given filter:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop&filter=apple
* /api/files/sampledata/fruitshop?filter=apple
* - GET file information for a specific file:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
*
* - POST a new file or folder into a specific folder:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
Expand Down Expand Up @@ -123,10 +127,10 @@ public function files()
switch (strtolower($method))
{
case 'get':
$data = $this->adapter->getFiles($path);
$data = $this->adapter->getFiles($path, $this->input->getWord('filter'));
break;
case 'delete':
$data = $this->adapter->delete($path);
$this->adapter->delete($path);
break;
case 'post':
$content = $this->input->json;
Expand All @@ -136,20 +140,20 @@ public function files()
if ($mediaContent)
{
// A file needs to be created
$data = $this->adapter->createFile($name, $path, $mediaContent);
$this->adapter->createFile($name, $path, $mediaContent);
}
else
{
// A file needs to be created
$data = $this->adapter->createFolder($name, $path);
$this->adapter->createFolder($name, $path);
}
break;
case 'put':
$content = $this->input->json;
$name = basename($path);
$mediaContent = base64_decode($content->get('content'));

$data = $this->adapter->updateFile($name, str_replace($name, '', $path), $mediaContent);
$this->adapter->updateFile($name, str_replace($name, '', $path), $mediaContent);
break;
default:
throw new BadMethodCallException('Method not supported yet!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ interface MediaFileAdapterInterface
{
/**
* Returns the folders and files for the given path. The returned objects
* have the following properties:
* - type: file or dir
* have the following properties available:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
*
* @param string $path The folder
* If the type is file, then some additional properties are available:
* - extension: The file extension
* - size: The size of the file
*
* @param string $path The folder
* @param string $filter The filter
*
* @return stdClass[]
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function getFiles($path = '/');
public function getFiles($path = '/', $filter = '');

/**
* Creates a folder with the given name in the given path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct($rootPath)

/**
* Returns the folders and files for the given path. The returned objects
* have the following properties avilable:
* have the following properties available:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
Expand All @@ -54,14 +54,15 @@ public function __construct($rootPath)
* - extension: The file extension
* - size: The size of the file
*
* @param string $path The folder
* @param string $path The folder
* @param string $filter The filter
*
* @return stdClass[]
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function getFiles($path = '/')
public function getFiles($path = '/', $filter = '')
{
// Set up the path correctly
$path = JPath::clean('/' . $path);
Expand Down Expand Up @@ -91,7 +92,7 @@ public function getFiles($path = '/')
$data = array();

// Read the folders
foreach (JFolder::folders($basePath) as $folder)
foreach (JFolder::folders($basePath, $filter) as $folder)
{
$obj = new stdClass;
$obj->type = 'dir';
Expand All @@ -102,7 +103,7 @@ public function getFiles($path = '/')
}

// Read the files
foreach (JFolder::files($basePath) as $file)
foreach (JFolder::files($basePath, $filter) as $file)
{
$obj = new stdClass;
$obj->type = 'file';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,45 @@ public function testGetFiles()
$this->assertGreaterThan(1, $files[1]->size);
}

/**
* Test MediaFileAdapterLocal::getFiles with a filter
*
* @return void
*/
public function testGetFilteredFiles()
{
// Make some test files
JFile::write($this->root . 'test.txt', 'test');
JFile::write($this->root . 'foo.txt', 'test');
JFile::write($this->root . 'bar.txt', 'test');
JFolder::create($this->root . 'unit');
JFolder::create($this->root . 'foo');

// Create the adapter
$adapter = new MediaFileAdapterLocal($this->root);

// Fetch the files from the root folder
$files = $adapter->getFiles('/', 'foo');

// Check if the array is big enough
$this->assertNotEmpty($files);
$this->assertCount(2, $files);

// Check the folder
$this->assertInstanceOf('stdClass', $files[0]);
$this->assertEquals('dir', $files[0]->type);
$this->assertEquals('foo', $files[0]->name);
$this->assertEquals('/foo', $files[0]->path);

// Check the file
$this->assertInstanceOf('stdClass', $files[1]);
$this->assertEquals('file', $files[1]->type);
$this->assertEquals('foo.txt', $files[1]->name);
$this->assertEquals('txt', $files[1]->extension);
$this->assertEquals('/foo.txt', $files[1]->path);
$this->assertGreaterThan(1, $files[1]->size);
}

/**
* Test MediaFileAdapterLocal::getFiles with a single file
*
Expand Down

0 comments on commit eefa6be

Please sign in to comment.