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

Commit

Permalink
Return error codes when an exception happens (#96)
Browse files Browse the repository at this point in the history
this needs to be also merged in the j4 branch.
  • Loading branch information
laoneo authored and dneukirchen committed Feb 24, 2017
1 parent 24d1cdb commit 3cdd2ae
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 15 deletions.
20 changes: 16 additions & 4 deletions administrator/components/com_media/controllers/api.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function files()

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

$data = $this->adapter->getFiles($path . '/' . $name);
$data = $this->adapter->getFile($path . '/' . $name);
break;
default:
throw new BadMethodCallException('Method not supported yet!');
Expand All @@ -162,9 +162,13 @@ public function files()
// Return the data
$this->sendResponse($data);
}
catch (MediaFileAdapterFilenotfoundexception $e)
{
$this->sendResponse($e, 404);
}
catch (Exception $e)
{
$this->sendResponse($e);
$this->sendResponse($e, 500);
}
}

Expand All @@ -173,14 +177,22 @@ public function files()
*
* {"success":true,"message":"ok","messages":null,"data":[{"type":"dir","name":"banners","path":"//"}]}
*
* @param mixed $data The data to send
* @param mixed $data The data to send
* @param number $responseCode The response code
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function sendResponse($data = null)
protected function sendResponse($data = null, $responseCode = 200)
{
// Set the correct content type
JFactory::getApplication()->setHeader('Content-Type', 'application/json');

// Set the status code for the response
http_response_code($responseCode);

// Send the data
echo new JResponseJson($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

/**
* Media file adapter file not found exception.
*
* @since __DEPLOY_VERSION__
*/
class MediaFileAdapterFilenotfoundexception extends Exception
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@
*/
interface MediaFileAdapterInterface
{
/**
* Returns the requested file or folder. The returned object
* has 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
* - 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
*
* If the path doesn't exist a MediaFileAdapterFilenotfoundexception is thrown.
*
* @param string $path The path to the file or folder
*
* @return stdClass[]
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function getFile($path = '/');

/**
* Returns the folders and files for the given path. The returned objects
* have the following properties available:
Expand All @@ -30,6 +55,8 @@ interface MediaFileAdapterInterface
* - width: The width, when available
* - height: The height, when available
*
* If the path doesn't exist a MediaFileAdapterFilenotfoundexception is thrown.
*
* @param string $path The folder
* @param string $filter The filter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function __construct($rootPath)
* - width: The width, when available
* - height: The height, when available
*
* @param string $path The folder
* If the path doesn't exist a MediaFileAdapterFilenotfoundexception is thrown.
*
* @param string $path The path to the file or folder
*
* @return stdClass[]
*
Expand All @@ -73,7 +75,7 @@ public function getFile($path = '/')
// Check if file exists
if (!file_exists($basePath))
{
return array();
throw new MediaFileAdapterFilenotfoundexception();
}

return $this->getPathInformation($basePath);
Expand All @@ -93,6 +95,8 @@ public function getFile($path = '/')
* - width: The width, when available
* - height: The height, when available
*
* If the path doesn't exist a MediaFileAdapterFilenotfoundexception is thrown.
*
* @param string $path The folder
* @param string $filter The filter
*
Expand All @@ -110,7 +114,7 @@ public function getFiles($path = '/', $filter = '')
// Check if file exists
if (!file_exists($basePath))
{
return array();
throw new MediaFileAdapterFilenotfoundexception();
}

// Check if the path points to a file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,56 @@ protected function tearDown()
JFolder::delete($this->root);
}


/**
* Test MediaFileAdapterLocal::getFile
*
* @return void
*/
public function testGetFile()
{
// Make some test files
JFile::write($this->root . 'test.txt', 'test');

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

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

// Check if the array is big enough
$this->assertNotEmpty($file);

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

/**
* Test MediaFileAdapterLocal::getFile with an invalid path
*
* @expectedException MediaFileAdapterFilenotfoundexception
*
* @return void
*/
public function testGetFileInvalidPath()
{
// Create the adapter
$adapter = new MediaFileAdapterLocal($this->root);

// Fetch the file from the root folder
$adapter->getFile('invalid');
}

/**
* Test MediaFileAdapterLocal::getFiles
*
Expand Down Expand Up @@ -179,21 +229,17 @@ public function testGetSingleFile()
/**
* Test MediaFileAdapterLocal::getFiles with an invalid path
*
* @expectedException MediaFileAdapterFilenotfoundexception
*
* @return void
*/
public function testGetFilesInvalidPath()
{
// 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('/test1.txt');

// Check if the array is empty
$this->assertEmpty($files);
// Fetch the file from the root folder
$adapter->getFiles('invalid');
}

/**
Expand Down

0 comments on commit 3cdd2ae

Please sign in to comment.