/** * @expectedException \Magento\Framework\Model\Exception */ public function testUploadInvalidJsFile() { $fileName = 'file.name'; $this->_service = new \Magento\Theme\Model\Uploader\Service($this->_filesystemMock, $this->_fileSizeMock, $this->_uploaderFactory, ['js' => '100M']); $this->_uploader->expects($this->once())->method('getFileSize')->will($this->returnValue(499 * self::MB_MULTIPLIER)); $this->_service->uploadJsFile($fileName); }
/** * Upload js file * * @param string $file - Key in the $_FILES array * @return array * @throws \Magento\Framework\Model\Exception */ public function uploadJsFile($file) { /** @var $fileUploader \Magento\Core\Model\File\Uploader */ $fileUploader = $this->_uploaderFactory->create(array('fileId' => $file)); $fileUploader->setAllowedExtensions(array('js')); $fileUploader->setAllowRenameFiles(true); $fileUploader->setAllowCreateFolders(true); $isValidFileSize = $this->_validateFileSize($fileUploader->getFileSize(), $this->getJsUploadMaxSize()); if (!$isValidFileSize) { throw new \Magento\Framework\Model\Exception(__('The JS file must be less than %1M.', $this->getJsUploadMaxSizeInMb())); } $file = $fileUploader->validateFile(); return array('filename' => $file['name'], 'content' => $this->getFileContent($file['tmp_name'])); }
/** * Validate uploaded file * * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _validateUploadedFile() { $option = $this->getOption(); $processingParams = $this->_getProcessingParams(); /** * Upload init */ $upload = new \Zend_File_Transfer_Adapter_Http(); $file = $processingParams->getFilesPrefix() . 'options_' . $option->getId() . '_file'; $maxFileSize = $this->getFileSizeService()->getMaxFileSize(); try { $runValidation = $option->getIsRequire() || $upload->isUploaded($file); if (!$runValidation) { $this->setUserValue(null); return $this; } $fileInfo = $upload->getFileInfo($file); $fileInfo = $fileInfo[$file]; $fileInfo['title'] = $fileInfo['name']; } catch (\Exception $e) { // when file exceeds the upload_max_filesize, $_FILES is empty if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > $maxFileSize) { $this->setIsValid(false); $value = $this->getFileSizeService()->getMaxFileSizeInMb(); throw new Exception(__("The file you uploaded is larger than %1 Megabytes allowed by server", $value)); } else { switch ($this->getProcessMode()) { case \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL: throw new Exception(__('Please specify the product\'s required option(s).')); break; default: $this->setUserValue(null); break; } return $this; } } /** * Option Validations */ // Image dimensions $_dimentions = array(); if ($option->getImageSizeX() > 0) { $_dimentions['maxwidth'] = $option->getImageSizeX(); } if ($option->getImageSizeY() > 0) { $_dimentions['maxheight'] = $option->getImageSizeY(); } if (count($_dimentions) > 0) { $upload->addValidator('ImageSize', false, $_dimentions); } // File extension $_allowed = $this->_parseExtensionsString($option->getFileExtension()); if ($_allowed !== null) { $upload->addValidator('Extension', false, $_allowed); } else { $_forbidden = $this->_parseExtensionsString($this->getConfigData('forbidden_extensions')); if ($_forbidden !== null) { $upload->addValidator('ExcludeExtension', false, $_forbidden); } } // Maximum filesize $upload->addValidator('FilesSize', false, array('max' => $maxFileSize)); /** * Upload process */ $this->_initFilesystem(); if ($upload->isUploaded($file) && $upload->isValid($file)) { $extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION); $fileName = \Magento\Core\Model\File\Uploader::getCorrectFileName($fileInfo['name']); $dispersion = \Magento\Core\Model\File\Uploader::getDispretionPath($fileName); $filePath = $dispersion; $tmpDirectory = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem::SYS_TMP_DIR); $fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name']))); $filePath .= '/' . $fileHash . '.' . $extension; $fileFullPath = $this->_mediaDirectory->getAbsolutePath($this->_quotePath . $filePath); $upload->addFilter('Rename', array('target' => $fileFullPath, 'overwrite' => true)); $this->getProduct()->getTypeInstance()->addFileQueue(array('operation' => 'receive_uploaded_file', 'src_name' => $file, 'dst_name' => $fileFullPath, 'uploader' => $upload, 'option' => $this)); $_width = 0; $_height = 0; if ($tmpDirectory->isReadable($tmpDirectory->getRelativePath($fileInfo['tmp_name']))) { $_imageSize = getimagesize($fileInfo['tmp_name']); if ($_imageSize) { $_width = $_imageSize[0]; $_height = $_imageSize[1]; } } $uri = $this->_filesystem->getUri(\Magento\Framework\App\Filesystem::MEDIA_DIR); $this->setUserValue(array('type' => $fileInfo['type'], 'title' => $fileInfo['name'], 'quote_path' => $uri . $this->_quotePath . $filePath, 'order_path' => $uri . $this->_orderPath . $filePath, 'fullpath' => $fileFullPath, 'size' => $fileInfo['size'], 'width' => $_width, 'height' => $_height, 'secret_key' => substr($fileHash, 0, 20))); } elseif ($upload->getErrors()) { $errors = $this->_getValidatorErrors($upload->getErrors(), $fileInfo); 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).')); } return $this; }
/** * Move file from tmp path to base path * * @param string $baseTmpPath * @param string $basePath * @param string $file * @return string */ protected function _moveFileFromTmp($baseTmpPath, $basePath, $file) { if (strrpos($file, '.tmp') == strlen($file) - 4) { $file = substr($file, 0, strlen($file) - 4); } $destFile = dirname($file) . '/' . \Magento\Core\Model\File\Uploader::getNewFileName($this->getFilePath($basePath, $file)); $this->_coreFileStorageDatabase->copyFile($this->getFilePath($baseTmpPath, $file), $this->getFilePath($basePath, $destFile)); $this->_mediaDirectory->renameFile($this->getFilePath($baseTmpPath, $file), $this->getFilePath($basePath, $destFile)); return str_replace('\\', '/', $destFile); }
/** * Get filename which is not duplicated with other files in media temporary and media directories * * @param string $fileName * @param string $dispretionPath * @return string */ protected function _getNotDuplicatedFilename($fileName, $dispretionPath) { $fileMediaName = $dispretionPath . '/' . \Magento\Core\Model\File\Uploader::getNewFileName($this->_mediaConfig->getMediaPath($fileName)); $fileTmpMediaName = $dispretionPath . '/' . \Magento\Core\Model\File\Uploader::getNewFileName($this->_mediaConfig->getTmpMediaPath($fileName)); if ($fileMediaName != $fileTmpMediaName) { if ($fileMediaName != $fileName) { return $this->_getNotDuplicatedFileName($fileMediaName, $dispretionPath); } elseif ($fileTmpMediaName != $fileName) { return $this->_getNotDuplicatedFilename($fileTmpMediaName, $dispretionPath); } } return $fileMediaName; }