Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Add filter option #79

Merged
merged 1 commit into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 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,7 +127,7 @@ 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);
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