Пример #1
0
 /**
  * Create thumbnail for image and save it to thumbnails directory
  *
  * @param string $source Image path to be resized
  * @param bool $keepRation Keep aspect ratio or not
  * @return bool|string Resized filepath or false if errors were occurred
  */
 public function resizeFile($source, $keepRation = true)
 {
     if (!$this->_filesystem->isFile($source) || !$this->_filesystem->isReadable($source)) {
         return false;
     }
     $targetDir = $this->getThumbsPath($source);
     if (!$this->_filesystem->isWritable($targetDir)) {
         $this->_filesystem->createDirectory($targetDir);
     }
     if (!$this->_filesystem->isWritable($targetDir)) {
         return false;
     }
     $adapter = Mage::helper('Mage_Core_Helper_Data')->getImageAdapterType();
     $image = Varien_Image_Adapter::factory($adapter);
     $image->open($source);
     $width = $this->getConfigData('resize_width');
     $height = $this->getConfigData('resize_height');
     $image->keepAspectRatio($keepRation);
     $image->resize($width, $height);
     $dest = $targetDir . DS . pathinfo($source, PATHINFO_BASENAME);
     $image->save($dest);
     if ($this->_filesystem->isFile($dest)) {
         return $dest;
     }
     return false;
 }
Пример #2
0
 /**
  * Create Writeable directory if it doesn't exist
  *
  * @param string Absolute directory path
  * @return void
  */
 protected function _createWriteableDir($path)
 {
     try {
         $this->_filesystem->createDirectory($path, 0777);
     } catch (Magento_Filesystem_Exception $e) {
         Mage::throwException(Mage::helper('Mage_Catalog_Helper_Data')->__("Cannot create writeable directory '%s'.", $path));
     }
 }
Пример #3
0
 /**
  * Return path of the current selected directory or root directory for startup
  * Try to create target directory if it doesn't exist
  *
  * @throws Mage_Core_Exception
  * @return string
  */
 public function getCurrentPath()
 {
     if (!$this->_currentPath) {
         $currentPath = $this->getStorageRoot();
         $path = $this->_getRequest()->getParam($this->getTreeNodeName());
         if ($path) {
             $path = $this->convertIdToPath($path);
             if ($this->_filesystem->isDirectory($path)) {
                 $currentPath = $path;
             }
         }
         try {
             if (!$this->_filesystem->isWritable($currentPath)) {
                 $this->_filesystem->createDirectory($currentPath);
             }
         } catch (Magento_Filesystem_Exception $e) {
             $message = Mage::helper('Mage_Cms_Helper_Data')->__('The directory %s is not writable by server.', $currentPath);
             Mage::throwException($message);
         }
         $this->_currentPath = $currentPath;
     }
     return $this->_currentPath;
 }
Пример #4
0
 /**
  * Process File Queue
  * @return Mage_Catalog_Model_Product_Type_Abstract
  */
 public function processFileQueue()
 {
     if (empty($this->_fileQueue)) {
         return $this;
     }
     foreach ($this->_fileQueue as &$queueOptions) {
         if (isset($queueOptions['operation']) && ($operation = $queueOptions['operation'])) {
             switch ($operation) {
                 case 'receive_uploaded_file':
                     $src = isset($queueOptions['src_name']) ? $queueOptions['src_name'] : '';
                     $dst = isset($queueOptions['dst_name']) ? $queueOptions['dst_name'] : '';
                     /** @var $uploader Zend_File_Transfer_Adapter_Http */
                     $uploader = isset($queueOptions['uploader']) ? $queueOptions['uploader'] : null;
                     $path = dirname($dst);
                     try {
                         $this->_filesystem->createDirectory($path, 0777);
                     } catch (Magento_Filesystem_Exception $e) {
                         Mage::throwException($this->_helper('Mage_Catalog_Helper_Data')->__("Cannot create writeable directory '%s'.", $path));
                     }
                     $uploader->setDestination($path);
                     if (empty($src) || empty($dst) || !$uploader->receive($src)) {
                         /**
                          * @todo: show invalid option
                          */
                         if (isset($queueOptions['option'])) {
                             $queueOptions['option']->setIsValid(false);
                         }
                         Mage::throwException($this->_helper('Mage_Catalog_Helper_Data')->__("File upload failed"));
                     }
                     $this->_helper('Mage_Core_Helper_File_Storage_Database')->saveFile($dst);
                     break;
                 case 'move_uploaded_file':
                     $src = $queueOptions['src_name'];
                     $dst = $queueOptions['dst_name'];
                     move_uploaded_file($src, $dst);
                     $this->_helper('Mage_Core_Helper_File_Storage_Database')->saveFile($dst);
                     break;
                 default:
                     break;
             }
         }
         $queueOptions = null;
     }
     return $this;
 }
Пример #5
0
 public function __destruct()
 {
     if ($this->_isMapChanged && $this->_canSaveMap) {
         if (!$this->_filesystem->isDirectory($this->_mapDir)) {
             $this->_filesystem->createDirectory($this->_mapDir, 0777);
         }
         $this->_filesystem->write($this->_mapFile, serialize($this->_map));
     }
 }
Пример #6
0
 /**
  * Merge files, located under the same folder, into one and return file name of merged file
  *
  * @param array $files list of names relative to the same folder
  * @param string $contentType
  * @return string
  * @throws Magento_Exception if not existing file requested for merge
  */
 protected function _mergeFiles($files, $contentType)
 {
     $filesToMerge = array();
     $mergedFile = array();
     $jsDir = Mage::getBaseDir('js');
     $publicDir = $this->_buildPublicViewFilename('');
     foreach ($files as $file) {
         $params = array();
         $this->_updateParamDefaults($params);
         $filesToMerge[$file] = $this->_publishViewFile($file, $params);
         $mergedFile[] = str_replace('\\', '/', str_replace(array($jsDir, $publicDir), '', $filesToMerge[$file]));
     }
     $mergedFile = self::PUBLIC_MERGE_DIR . DS . md5(implode('|', $mergedFile)) . ".{$contentType}";
     $mergedFile = $this->_buildPublicViewFilename($mergedFile);
     $mergedMTimeFile = $mergedFile . '.dat';
     $filesMTimeData = '';
     foreach ($filesToMerge as $file) {
         $filesMTimeData .= $this->_filesystem->getMTime($file);
     }
     if ($this->_filesystem->has($mergedFile) && $this->_filesystem->has($mergedMTimeFile) && $filesMTimeData == $this->_filesystem->read($mergedMTimeFile)) {
         return $mergedFile;
     }
     if (!$this->_filesystem->isDirectory(dirname($mergedFile))) {
         $this->_filesystem->createDirectory(dirname($mergedFile), 0777);
     }
     $result = array();
     foreach ($filesToMerge as $file) {
         if (!$this->_filesystem->has($file)) {
             throw new Magento_Exception("Unable to locate file '{$file}' for merging.");
         }
         $content = $this->_filesystem->read($file);
         if ($contentType == self::CONTENT_TYPE_CSS) {
             $offset = $this->_getFilesOffset($file, dirname($mergedFile));
             $content = $this->_applyCssUrlOffset($content, $offset);
         }
         $result[] = $content;
     }
     $result = ltrim(implode($result));
     if ($contentType == self::CONTENT_TYPE_CSS) {
         $result = $this->_popCssImportsUp($result);
     }
     $this->_filesystem->write($mergedFile, $result);
     $this->_filesystem->write($mergedMTimeFile, $filesMTimeData);
     return $mergedFile;
 }
Пример #7
0
 /**
  * @dataProvider workingDirDataProvider
  * @param string|null $workingDirectory
  */
 public function testCreateDirectory($workingDirectory)
 {
     $validPath = '/tmp/path';
     $adapterMock = $this->getMockBuilder('Magento_Filesystem_AdapterInterface')->getMock();
     $adapterMock->expects($this->exactly(2))->method('isDirectory')->with('/tmp')->will($this->returnValue(true));
     $adapterMock->expects($this->once())->method('createDirectory')->with($validPath);
     $filesystem = new Magento_Filesystem($adapterMock);
     $filesystem->setWorkingDirectory('/tmp');
     $filesystem->createDirectory($validPath, 0777, $workingDirectory);
 }