Commit: bcda457c5308d35081807d9f7e84458d44190816

Author: gwoo | Date: 2010-01-24 09:46:00 -0800
adding delete to media data source. adding some standard errors.
diff --git a/extensions/data/source/Media.php b/extensions/data/source/Media.php index 649c111..b048dce 100644 --- a/extensions/data/source/Media.php +++ b/extensions/data/source/Media.php @@ -9,6 +9,8 @@ namespace li3_lab\extensions\data\source; use \lithium\core\Libraries; +use \RecursiveDirectoryIterator; +use \RecursiveIteratorIterator; /** * Data source for managing files @@ -43,6 +45,20 @@ class Media extends \lithium\data\Source { ); /** + * Error messages for possible upload errors. + * + * @var array + */ + protected $_errors = array( + null, + "The uploaded file exceeds the upload_max_filesize directive in php.ini", + "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", + "The uploaded file was only partially uploaded", + "No file was uploaded", + "Missing a temporary folder" + ); + + /** * Constructor for setting config options. * * @param string $config @@ -149,19 +165,35 @@ class Media extends \lithium\data\Source { public function create($query, $options = array()) { $params = compact('query', 'options'); $path = $this->_path; + $errors = $this->_errors; - return $this->_filter(__METHOD__, $params, function($self, $params) use ($path) { + return $this->_filter(__METHOD__, $params, function($self, $params) use ($path, $errors) { extract($params); extract($query->export($self), EXTR_OVERWRITE); $data = $query->data(); + + if(!empty($data['error']) && isset($errors[$data['error']])) { + $query->record()->errors('file', $errors[$data['error']]); + return false; + } $filename = preg_replace('/\W-/', '', $data['name']); + if (!empty($data['tmp_name'])) { + if (!file_exists($data['tmp_name'])) { + $query->record()->errors('file', $data['tmp_name'] . 'does not exist'); + return false; + } $data['contents'] = file_get_contents($data['tmp_name']); } + + if (empty($data['contents'])) { + return false; + } if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data['contents'])) { $data['contents'] = base64_decode($data['contents']); } $file = "{$path}/{$table}/{$filename}"; + if (file_put_contents($file, $data['contents'])) { $query->conditions(array('name' => $filename)); $record = $self->read($query, $options); @@ -209,14 +241,44 @@ class Media extends \lithium\data\Source { return $this->create($query, $options); } - public function delete($query, $options) { - return compact('query', 'options'); + public function delete($query, $options = array()) { + $params = compact('query', 'options'); + $path = $this->_path; + $config = $this->_config; + + return $this->_filter(__METHOD__, $params, function($self, $params) use ($path, $config) { + extract($params); + extract($query->export($self), EXTR_OVERWRITE); + $data = $query->data(); + $conditions += $options; + if (empty($conditions['name']) && is_object($data[0])) { + $conditions['name'] = $data[0]->getFilename(); + } + if (!empty($conditions['name'])) { + $file = "{$path}/{$table}/{$conditions['name']}"; + + if (!file_exists($file)) { + return true; + } + if (is_file($file)) { + return (boolean) unlink($file); + } + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($file), RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($iterator as $item) { + ($item->isDir()) ? rmdir($item->getPathname()) : unlink($item->getPathname()); + } + return (boolean) rmdir($file); + } + return false; + }); } public function conditions($conditions, $context) { $defaults = array( - 'namespaces' => true, 'recursive' => false, - 'filter' => '', 'exclude' => '', + 'namespaces' => false, 'recursive' => false, + 'filter' => '', 'exclude' => '', 'suffix' => false, 'format' => '' ); $conditions = (array) $conditions + $defaults; diff --git a/tests/cases/extensions/data/source/MediaTest.php b/tests/cases/extensions/data/source/MediaTest.php index df24967..61fc54b 100644 --- a/tests/cases/extensions/data/source/MediaTest.php +++ b/tests/cases/extensions/data/source/MediaTest.php @@ -77,7 +77,7 @@ class MediaTest extends \lithium\test\Unit { $media = new Media(array('path' => '/resources/tmp/tests')); $this->query->data(array( 'name' => 'some_file.phar.gz', 'type' => 'application/tar', - 'tmp_name' => LITHIUM_LIBRARY_PATH . '/lithium/console/command/build/template/app.phar.gz' + 'tmp_name' => LITHIUM_LIBRARY_PATH . '/lithium/console/command/create/template/app.phar.gz' )); $result = $media->create($this->query, array('classes' => $this->classes)); $this->assertTrue(is_object($result)); @@ -89,6 +89,22 @@ class MediaTest extends \lithium\test\Unit { $this->assertTrue($result); } + + public function testCreateWithError() { + $media = new Media(array('path' => '/resources/tmp/tests')); + $this->query->data(array( + 'name' => 'some_file.phar.gz', 'error' => '1', + )); + $result = $media->create($this->query, array('classes' => $this->classes)); + $this->assertFalse($result); + + $expected = array( + 'file' => 'The uploaded file exceeds the upload_max_filesize directive in php.ini' + ); + $result = $this->query->record()->errors(); + $this->assertEqual($expected, $result); + } + public function testRead() { $media = new Media(array('path' => '/resources/tmp/tests')); $expected = 1; @@ -101,6 +117,31 @@ class MediaTest extends \lithium\test\Unit { $this->_cleanUp(); } + + public function testDeleteFile() { + mkdir($this->_testPath . '/mock_uploads'); + touch($this->_testPath . '/mock_uploads/media_test'); + $this->assertTrue(file_exists($this->_testPath . '/mock_uploads/media_test')); + + $this->query->data(array('name' => 'media_test')); + $media = new Media(array('path' => '/resources/tmp/tests')); + $result = $media->delete($this->query); + $this->assertTrue($result); + + $this->assertFalse(file_exists($this->_testPath . '/mock_uploads/media_test')); + } + + public function testDeleteDirectory() { + mkdir($this->_testPath . '/mock_uploads/media_test'); + $this->assertTrue(file_exists($this->_testPath . '/mock_uploads/media_test')); + + $this->query->data(array('name' => 'media_test')); + $media = new Media(array('path' => '/resources/tmp/tests')); + $result = $media->delete($this->query); + $this->assertTrue($result); + + $this->assertFalse(file_exists($this->_testPath . '/mock_uploads/media_test')); + } } class MockUpload extends \lithium\data\Model {