Skip to content

Commit

Permalink
Merge pull request #75 from joomla-projects/feature/api/putpost
Browse files Browse the repository at this point in the history
Implement missing method
  • Loading branch information
Dimitri Grammatikogianni authored Jan 9, 2017
2 parents dd5f4d9 + 1fa20e7 commit a9a3081
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
37 changes: 37 additions & 0 deletions administrator/components/com_media/controllers/api.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,25 @@ public function __construct($config = array())
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
*
* New file body:
* {
* "name": "test.jpg",
* "content":"base64 encoded image"
* }
* New folder body:
* {
* "name": "test",
* }
*
* - PUT a media file:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
* Update file body:
* {
* "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
Expand Down Expand Up @@ -113,7 +129,28 @@ public function files()
$data = $this->adapter->delete($path);
break;
case 'post':
$content = $this->input->json;
$name = $content->get('name');
$mediaContent = base64_decode($content->get('content'));

if ($mediaContent)
{
// A file needs to be created
$data = $this->adapter->createFile($name, $path, $mediaContent);
}
else
{
// A file needs to be created
$data = $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);
break;
default:
throw new BadMethodCallException('Method not supported yet!');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@ interface MediaFileAdapterInterface
*/
public function getFiles($path = '/');

/**
* Creates a folder with the given name in the given path.
*
* @param string $name The name
* @param string $path The folder
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function createFolder($name, $path);

/**
* Creates a file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function createFile($name, $path, $data);

/**
* Updates the file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function updateFile($name, $path, $data);

/**
* Deletes the folder or file of the given path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,57 @@ public function getFiles($path = '/')
return $data;
}

/**
* Creates a folder with the given name in the given path.
*
* @param string $name The name
* @param string $path The folder
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function createFolder($name, $path)
{
JFolder::create($this->rootPath . $path . '/' . $name);
}

/**
* Creates a file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param string $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function createFile($name, $path, $data)
{
JFile::write($this->rootPath . $path . '/' . $name, $data);
}

/**
* Updates the file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws Exception
*/
public function updateFile($name, $path, $data)
{
JFile::write($this->rootPath . $path . '/' . $name, $data);
}


/**
* Deletes the folder or file of the given path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function setUp()

// Set up the temp root folder
$this->root = JPath::clean(JPATH_TESTS . '/tmp/test/');
JFolder::create($this->root);
}

/**
Expand Down Expand Up @@ -167,6 +168,66 @@ public function testGetFilesInvalidPath()
$this->assertEmpty($files);
}

/**
* Test MediaFileAdapterLocal::createFolder
*
* @return void
*/
public function testCreateFolder()
{
// Create the adapter
$adapter = new MediaFileAdapterLocal($this->root);

// Fetch the files from the root folder
$adapter->createFolder('unit', '/');

// Check if the file exists
$this->assertTrue(JFolder::exists($this->root . 'unit'));
}

/**
* Test MediaFileAdapterLocal::createFile
*
* @return void
*/
public function testCreateFile()
{
// Create the adapter
$adapter = new MediaFileAdapterLocal($this->root);

// Fetch the files from the root folder
$adapter->createFile('unit.txt', '/', 'test');

// Check if the file exists
$this->assertTrue(file_exists($this->root . 'unit.txt'));

// Check if the contents is correct
$this->assertEquals('test', file_get_contents($this->root . 'unit.txt'));
}

/**
* Test MediaFileAdapterLocal::updateFile
*
* @return void
*/
public function testUpdateFile()
{
// 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
$adapter->updateFile('unit.txt', '/', 'test 2');

// Check if the file exists
$this->assertTrue(file_exists($this->root . 'unit.txt'));

// Check if the contents is correct
$this->assertEquals('test 2', file_get_contents($this->root . 'unit.txt'));
}

/**
* Test MediaFileAdapterLocal::delete
*
Expand Down

0 comments on commit a9a3081

Please sign in to comment.