diff --git a/administrator/components/com_media/controllers/api.json.php b/administrator/components/com_media/controllers/api.json.php index abbd2484c698d..a989927c6b3a3 100644 --- a/administrator/components/com_media/controllers/api.json.php +++ b/administrator/components/com_media/controllers/api.json.php @@ -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!'); @@ -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); } } @@ -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); } } diff --git a/administrator/components/com_media/libraries/media/file/adapter/filenotfoundexception.php b/administrator/components/com_media/libraries/media/file/adapter/filenotfoundexception.php new file mode 100644 index 0000000000000..a36781e5bc31a --- /dev/null +++ b/administrator/components/com_media/libraries/media/file/adapter/filenotfoundexception.php @@ -0,0 +1,20 @@ +getPathInformation($basePath); @@ -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 * @@ -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 diff --git a/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php b/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php index 3e4adf307856f..22e7e2594d406 100644 --- a/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php +++ b/tests/unit/suites/administrator/components/com_media/libraries/file/LocalAdapterTest.php @@ -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 * @@ -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'); } /**