getFilePath()); return $this; } /** * @return $this */ protected function _beforeDelete() { $this->_removeFile(); return parent::_beforeDelete(); } /** * @param string $filename * @param string $content * * @return $this * @throws Exception */ public function setFile($filename, $content) { $filename = Mage::helper('aw_hdu3')->escapeFilename($filename); $this->setFileRealName($filename); $filename = $this->_generateFileName($filename); $this->setFileName($filename); $this->setFileContent($content); return $this; } /** * Processing object before save data * * @return Exception | Mage_Core_Model_Abstract */ protected function _beforeSave() { $this->_prepareFoldersRecursive(); file_put_contents($this->_getRootDir() . DS . $this->getFileName(), $this->getFileContent()); return parent::_beforeSave(); } /** * @return string */ public function getFilePath() { return $this->_getRootDir() . DS . $this->getFileName(); } /** * @return string */ public function getFileUrl() { return $this->_getRootUrl() . DS . $this->getFileName(); } /** * @return string */ protected final function _getRootDir() { return Mage::getBaseDir('media') . DS . self::ROOT_FOLDER . DS . $this->_getFilePath(); } /** * @return string */ protected final function _getRootUrl() { return Mage::getBaseUrl('media') . self::ROOT_FOLDER . DS . $this->_getFilePath(); } /** * @param string $filename * * @return string */ protected function _generateFileName($filename) { $newFileName = base64_encode($filename . time()); $fileExt = substr(strrchr($filename, '.'),1); if ($fileExt) { $newFileName .= '.' . $fileExt; } return $newFileName; } protected function _prepareFoldersRecursive() { $folders = explode(DS, self::ROOT_FOLDER . DS . $this->_getFilePath()); $folderPath = ''; foreach ($folders as $folder) { $folderPath .= $folder; if (!is_dir(Mage::getBaseDir('media') . DS . $folderPath)) { mkdir(Mage::getBaseDir('media') . DS . $folderPath, 0777); } $folderPath .= DS; } return $this; } }