PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/default/base/domain/behavior/fileable.php

https://github.com/bhar1red/anahita
PHP | 94 lines | 38 code | 7 blank | 49 comment | 2 complexity | 075ca705313c6fef7707d3288c50bb7a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Lib_Base
  7. * @subpackage Domain_Behavior
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id$
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. * Fileable Behavior
  17. *
  18. * @category Anahita
  19. * @package Lib_Base
  20. * @subpackage Domain_Behavior
  21. * @author Arash Sanieyan <ash@anahitapolis.com>
  22. * @author Rastin Mehr <rastin@anahitapolis.com>
  23. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  24. * @link http://www.anahitapolis.com
  25. */
  26. class LibBaseDomainBehaviorFileable extends LibBaseDomainBehaviorStorable
  27. {
  28. /**
  29. * Initializes the default configuration for the object
  30. *
  31. * Called from {@link __construct()} as a first step of object instantiation.
  32. *
  33. * @param KConfig $config An optional KConfig object with configuration options.
  34. *
  35. * @return void
  36. */
  37. protected function _initialize(KConfig $config)
  38. {
  39. $config->append(array(
  40. 'attributes' => array(
  41. 'filesize' => array('column'=>'medium_excerpt', 'type'=>'integer', 'write'=>'private'),
  42. 'mimeType' => array('column'=>'medium_mime_type', 'match'=>'/\w+\/\w+/', 'write'=>'private'),
  43. )
  44. ));
  45. parent::_initialize($config);
  46. }
  47. /**
  48. * Store Data
  49. *
  50. * @param array|KConfig $file
  51. * @return void
  52. */
  53. public function storeFile($file)
  54. {
  55. $filename = md5($this->id);
  56. $data = file_get_contents($file->tmp_name);
  57. if ( $this->getFileName() == $this->name ) {
  58. $this->name = $file->name;
  59. }
  60. $file->append(array(
  61. 'type' => mime_content_type($file->name)
  62. ));
  63. $this->mimeType = $file->type;
  64. $this->setValue('file_name', $file->name);
  65. $this->fileSize = strlen($data);
  66. $this->writeData($filename, $data, false);
  67. }
  68. /**
  69. * Return the file content;
  70. *
  71. * @return string
  72. */
  73. public function getFileContent()
  74. {
  75. $filename = md5($this->id);
  76. return $this->readData($filename, false);
  77. }
  78. /**
  79. * Return the original file name
  80. *
  81. * @return string
  82. */
  83. public function getFileName()
  84. {
  85. return $this->getValue('file_name');
  86. }
  87. }