/** * If DB file storage is on - find there, otherwise - just file_exists * * @param string $filename relative path * @return bool */ protected function _isFile($filename) { if ($this->_fileStorageHelper->checkDbUsage() && !$this->getMediaDirectory()->isFile($filename)) { $this->_fileStorageHelper->saveFileToFilesystem($filename); } return $this->getMediaDirectory()->isFile($filename); }
/** * Check if file exist in filesystem and try to re-create it from database record if negative. * @param string $file * @return bool|int */ public function ensureFileInFilesystem($file) { $result = true; if (!$this->_mediaDirectory->isFile($file)) { $result = $this->_coreFileStorageDatabase->saveFileToFilesystem($file); } return $result; }
/** * @param bool $expected * @param int $storage * @param int $callNum * @param int $id * @param int $callSaveFile * @dataProvider saveFileToFileSystemDataProvider */ public function testSaveFileToFileSystem($expected, $storage, $callNum, $id = 0, $callSaveFile = 0) { $this->configMock->expects($this->once())->method('getValue')->with(\Magento\Core\Model\File\Storage::XML_PATH_STORAGE_MEDIA, 'default')->will($this->returnValue($storage)); $dbModelMock = $this->getMockBuilder('Magento\\Core\\Model\\File\\Storage\\Database')->disableOriginalConstructor()->getMock(); $this->dbStorageFactoryMock->expects($this->exactly($callNum))->method('create')->will($this->returnValue($dbModelMock)); $dbModelMock->expects($this->exactly($callNum))->method('loadByFilename')->with('filename')->will($this->returnSelf()); $dbModelMock->expects($this->exactly($callNum))->method('getId')->will($this->returnValue($id)); $dbModelMock->expects($this->exactly($callSaveFile))->method('getData')->will($this->returnValue(['data'])); $this->fileStorageMock->expects($this->exactly($callSaveFile))->method('saveFile')->will($this->returnValue(true)); $this->assertEquals($expected, $this->helper->saveFileToFilesystem('media-dir/filename')); }
/** * First check this file on FS * If it doesn't exist - try to download it from DB * * @param string $filename * @return bool */ protected function _fileExists($filename) { if ($this->_mediaDirectory->isFile($filename)) { return true; } else { return $this->_coreFileStorageDatabase->saveFileToFilesystem($this->_mediaDirectory->getAbsolutePath($filename)); } }
/** * Validate file * * @param array $optionValue * @return bool|void * @throws \Magento\Framework\Model\Exception */ protected function _validateFile($optionValue) { $option = $this->getOption(); /** * @see \Magento\Catalog\Model\Product\Option\Type\File::_validateUploadFile() * There setUserValue() sets correct fileFullPath only for * quote_path. So we must form both full paths manually and * check them. */ $checkPaths = array(); if (isset($optionValue['quote_path'])) { $checkPaths[] = $optionValue['quote_path']; } if (isset($optionValue['order_path']) && !$this->getUseQuotePath()) { $checkPaths[] = $optionValue['order_path']; } $fileFullPath = null; $fileRelativePath = null; foreach ($checkPaths as $path) { if (!$this->_rootDirectory->isFile($path)) { if (!$this->_coreFileStorageDatabase->saveFileToFilesystem($fileFullPath)) { continue; } } $fileFullPath = $this->_rootDirectory->getAbsolutePath($path); $fileRelativePath = $path; break; } if ($fileFullPath === null) { return false; } $validatorChain = new \Zend_Validate(); $_dimentions = array(); if ($option->getImageSizeX() > 0) { $_dimentions['maxwidth'] = $option->getImageSizeX(); } if ($option->getImageSizeY() > 0) { $_dimentions['maxheight'] = $option->getImageSizeY(); } if (count($_dimentions) > 0 && !$this->_isImage($fileFullPath)) { return false; } if (count($_dimentions) > 0) { $validatorChain->addValidator(new \Zend_Validate_File_ImageSize($_dimentions)); } // File extension $_allowed = $this->_parseExtensionsString($option->getFileExtension()); if ($_allowed !== null) { $validatorChain->addValidator(new \Zend_Validate_File_Extension($_allowed)); } else { $_forbidden = $this->_parseExtensionsString($this->getConfigData('forbidden_extensions')); if ($_forbidden !== null) { $validatorChain->addValidator(new \Zend_Validate_File_ExcludeExtension($_forbidden)); } } // Maximum file size $maxFileSize = $this->getFileSizeService()->getMaxFileSize(); $validatorChain->addValidator(new \Zend_Validate_File_FilesSize(array('max' => $maxFileSize))); if ($validatorChain->isValid($fileFullPath)) { $ok = $this->_rootDirectory->isReadable($fileRelativePath) && isset($optionValue['secret_key']) && substr(md5($this->_rootDirectory->readFile($fileRelativePath)), 0, 20) == $optionValue['secret_key']; return $ok; } elseif ($validatorChain->getErrors()) { $errors = $this->_getValidatorErrors($validatorChain->getErrors(), $optionValue); if (count($errors) > 0) { $this->setIsValid(false); throw new Exception(implode("\n", $errors)); } } else { $this->setIsValid(false); throw new Exception(__('Please specify the product\'s required option(s).')); } }