Skip to content

Commit

Permalink
More file information in the file adapter (#81)
Browse files Browse the repository at this point in the history
* Return the path information

* More file information
  • Loading branch information
laoneo authored and dneukirchen committed Feb 18, 2017
1 parent eefa6be commit 85d671d
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 78 deletions.
12 changes: 6 additions & 6 deletions administrator/components/com_media/controllers/api.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct($config = array())
* /api/files/sampledata/fruitshop/test.jpg
*
*
* - POST a new file or folder into a specific folder:
* - POST a new file or folder into a specific folder, the file or folder information is returned:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
*
Expand All @@ -87,7 +87,7 @@ public function __construct($config = array())
* "name": "test",
* }
*
* - PUT a media file:
* - PUT a media file, the file or folder information is returned:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
Expand All @@ -96,10 +96,6 @@ public function __construct($config = array())
* "content":"base64 encoded image"
* }
*
* - PUT process a media file:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg&action=process
* /api/files/sampledata/fruitshop/test.jpg/process
*
* - DELETE an existing folder in a specific folder:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test
* /api/files/sampledata/fruitshop/test
Expand Down Expand Up @@ -147,13 +143,17 @@ public function files()
// A file needs to be created
$this->adapter->createFolder($name, $path);
}

$data = $this->adapter->getFiles($path . '/' . $name);
break;
case 'put':
$content = $this->input->json;
$name = basename($path);
$mediaContent = base64_decode($content->get('content'));

$this->adapter->updateFile($name, str_replace($name, '', $path), $mediaContent);

$data = $this->adapter->getFiles($path . '/' . $name);
break;
default:
throw new BadMethodCallException('Method not supported yet!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ interface MediaFileAdapterInterface
/**
* Returns the folders and files for the given path. The returned objects
* 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
*
* If the type is file, then some additional properties are available:
* - extension: The file extension
* - size: The size of the file
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* @param string $path The folder
* @param string $filter The filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ public function __construct($rootPath)
/**
* Returns the folders and files for the given path. The returned objects
* 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
*
* If the type is file, then some additional properties are available:
* - extension: The file extension
* - size: The size of the file
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* @param string $path The folder
* @param string $filter The filter
Expand All @@ -77,15 +80,7 @@ public function getFiles($path = '/', $filter = '')
// Check if the path points to a file
if (is_file($basePath))
{
// Create the file object
$obj = new stdClass;
$obj->type = 'file';
$obj->name = basename($path);
$obj->path = str_replace($this->rootPath, '/', $basePath);
$obj->extension = JFile::getExt($obj->name);
$obj->size = filesize($basePath);

return array($obj);
return array($this->getPathInformation($basePath));
}

// The data to return
Expand All @@ -94,25 +89,13 @@ public function getFiles($path = '/', $filter = '')
// Read the folders
foreach (JFolder::folders($basePath, $filter) as $folder)
{
$obj = new stdClass;
$obj->type = 'dir';
$obj->name = $folder;
$obj->path = str_replace($this->rootPath, '/', JPath::clean($basePath . '/' . $folder));

$data[] = $obj;
$data[] = $this->getPathInformation(JPath::clean($basePath . '/' . $folder));
}

// Read the files
foreach (JFolder::files($basePath, $filter) as $file)
{
$obj = new stdClass;
$obj->type = 'file';
$obj->name = $file;
$obj->path = str_replace($this->rootPath, '/', JPath::clean($basePath . '/' . $file));
$obj->extension = JFile::getExt($file);
$obj->size = filesize($this->rootPath . $path);

$data[] = $obj;
$data[] = $this->getPathInformation(JPath::clean($basePath . '/' . $file));
}

// Return the data
Expand Down Expand Up @@ -198,4 +181,89 @@ public function delete($path)
throw new Exception('Delete not possible!');
}
}

/**
* Returns the folder or file information for the given path. The returned object
* has the following properties:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* @param string $path The folder
*
* @return stdClass
*
* @since __DEPLOY_VERSION__
*/
private function getPathInformation($path)
{
// The boolean if it is a dir
$isDir = is_dir($path);

// Set the values
$obj = new stdClass;
$obj->type = $isDir ? 'dir' : 'file';
$obj->name = basename($path);
$obj->path = str_replace($this->rootPath, '/', $path);
$obj->extension = !$isDir ? JFile::getExt($obj->name) : '';
$obj->size = !$isDir ? filesize($path) : 0;
$obj->create_date = $this->getDate(filectime($path))->format('c', true);
$obj->modified_date = $this->getDate(filemtime($path))->format('c', true);
$obj->mime_type = mime_content_type($path);

try
{
// Get the image properties
$props = JImage::getImageFileProperties($path);
$obj->width = $props->width;
$obj->height = $props->height;
}
catch (Exception $e)
{
// Probably not an image
$obj->width = 0;
$obj->height = 0;
}

return $obj;
}

/**
* Returns a JDate with the correct Joomla timezone for the given date.
*
* @param string $date The date to create a JDate from
*
* @return JDate[]
*
* @since __DEPLOY_VERSION__
*/
private function getDate($date = null)
{
$dateObj = JFactory::getDate($date);

$timezone = JFactory::getApplication()->get('offset');
$user = JFactory::getUser();
if ($user->id)
{
$userTimezone = $user->getParam('timezone');
if (!empty($userTimezone))
{
$timezone = $userTimezone;
}
}

if ($timezone)
{
$dateObj->setTimezone(new DateTimeZone($timezone));
}

return $dateObj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @subpackage com_media
* @since __DEPLOY_VERSION__
*/
class LocalAdapterTest extends PHPUnit_Framework_TestCase
class LocalAdapterTest extends TestCaseDatabase
{
/**
* The root folder to work from.
Expand All @@ -41,6 +41,10 @@ protected function setUp()
// Set up the temp root folder
$this->root = JPath::clean(JPATH_TESTS . '/tmp/test/');
JFolder::create($this->root);

// Set up the application and session
JFactory::$application = $this->getMockCmsApp();
JFactory::$session = $this->getMockSession();
}

/**
Expand Down Expand Up @@ -80,14 +84,26 @@ public function testGetFiles()
$this->assertEquals('dir', $files[0]->type);
$this->assertEquals('unit', $files[0]->name);
$this->assertEquals('/unit', $files[0]->path);
$this->assertEquals('', $files[0]->extension);
$this->assertEquals(0, $files[0]->size);
$this->assertNotEmpty($files[0]->create_date);
$this->assertNotEmpty($files[0]->modified_date);
$this->assertEquals('directory', $files[0]->mime_type);
$this->assertEquals(0, $files[0]->width);
$this->assertEquals(0, $files[0]->height);

// 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('/test.txt', $files[1]->path);
$this->assertEquals('txt', $files[1]->extension);
$this->assertGreaterThan(1, $files[1]->size);
$this->assertNotEmpty($files[1]->create_date);
$this->assertNotEmpty($files[1]->modified_date);
$this->assertEquals('text/plain', $files[1]->mime_type);
$this->assertEquals(0, $files[1]->width);
$this->assertEquals(0, $files[1]->height);
}

/**
Expand Down Expand Up @@ -115,7 +131,6 @@ public function testGetFilteredFiles()
$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);
Expand All @@ -124,9 +139,7 @@ public function testGetFilteredFiles()
$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);
}

/**
Expand All @@ -153,38 +166,14 @@ public function testGetSingleFile()
$this->assertInstanceOf('stdClass', $files[0]);
$this->assertEquals('file', $files[0]->type);
$this->assertEquals('test.txt', $files[0]->name);
$this->assertEquals('txt', $files[0]->extension);
$this->assertEquals('/test.txt', $files[0]->path);
$this->assertGreaterThan(0, $files[0]->size);
}

/**
* Test MediaFileAdapterLocal::getFiles with a single file and a different path
*
* @return void
*/
public function testGetSingleFileSpecialPath()
{
// Make some test files
JFile::write($this->root . 'test.txt', 'test');

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

// Fetch the files from the root folder
$files = $adapter->getFiles('/test.txt');

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

// Check the file
$this->assertInstanceOf('stdClass', $files[0]);
$this->assertEquals('file', $files[0]->type);
$this->assertEquals('test.txt', $files[0]->name);
$this->assertEquals('txt', $files[0]->extension);
$this->assertEquals('/test.txt', $files[0]->path);
$this->assertGreaterThan(0, $files[0]->size);
$this->assertGreaterThan(1, $files[0]->size);
$this->assertNotEmpty($files[0]->create_date);
$this->assertNotEmpty($files[0]->modified_date);
$this->assertEquals('text/plain', $files[0]->mime_type);
$this->assertEquals(0, $files[0]->width);
$this->assertEquals(0, $files[0]->height);
}

/**
Expand Down

0 comments on commit 85d671d

Please sign in to comment.