resultRawFactory = $resultRawFactory; $this->urlDecoder = $urlDecoder; $this->filesystem = $filesystem; $this->storage = $storage; $this->fileFactory = $fileFactory; $this->ioFile = $ioFile; } /** * Customer address view file action * * @return ResultInterface|ResponseInterface|void * @throws NotFoundException */ public function execute() { list($file, $plain) = $this->getFileParams(); $directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); $fileName = AddressMetadataInterface::ENTITY_TYPE_ADDRESS . DIRECTORY_SEPARATOR . ltrim($file, DIRECTORY_SEPARATOR); $path = $directory->getAbsolutePath($fileName); if (mb_strpos($path, '..') !== false || (!$directory->isFile($fileName) && !$this->storage->processStorageFile($path)) ) { throw new NotFoundException(__('Page not found.')); } $pathInfo = $this->ioFile->getPathInfo($path); if ($plain) { $extension = $pathInfo['extension']; switch (strtolower($extension)) { case 'gif': $contentType = 'image/gif'; break; case 'jpg': $contentType = 'image/jpeg'; break; case 'png': $contentType = 'image/png'; break; default: $contentType = 'application/octet-stream'; break; } $stat = $directory->stat($fileName); $contentLength = $stat['size']; $contentModify = $stat['mtime']; /** @var Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); $resultRaw->setHttpResponseCode(200) ->setHeader('Pragma', 'public', true) ->setHeader('Content-type', $contentType, true) ->setHeader('Content-Length', $contentLength) ->setHeader('Last-Modified', date('r', $contentModify)); $resultRaw->setContents($directory->readFile($fileName)); return $resultRaw; } else { $name = $pathInfo['basename']; return $this->fileFactory->create( $name, ['type' => 'filename', 'value' => $fileName], DirectoryList::MEDIA ); } } /** * Get parameters from request. * * @return array * @throws NotFoundException */ private function getFileParams() : array { $plain = false; if ($this->getRequest()->getParam('file', '')) { // download file $file = $this->urlDecoder->decode( $this->getRequest()->getParam('file', '') ); } elseif ($this->getRequest()->getParam('image', '')) { // show plain image $file = $this->urlDecoder->decode( $this->getRequest()->getParam('image', '') ); $plain = true; } else { throw new NotFoundException(__('Page not found.')); } return [$file, $plain]; } }