Overview

Namespaces

  • CRUDlex

Classes

  • CRUDControllerProvider
  • CRUDData
  • CRUDEntity
  • CRUDEntityDefinition
  • CRUDMySQLData
  • CRUDMySQLDataFactory
  • CRUDServiceProvider
  • CRUDSimpleFilesystemFileProcessor

Interfaces

  • CRUDDataFactoryInterface
  • CRUDFileProcessorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the CRUDlex package.
  5:  *
  6:  * (c) Philip Lehmann-Böhm <philip@philiplb.de>
  7:  *
  8:  * For the full copyright and license information, please view the LICENSE
  9:  * file that was distributed with this source code.
 10:  */
 11: 
 12: namespace CRUDlex;
 13: 
 14: use Symfony\Component\HttpFoundation\Request;
 15: use Symfony\Component\HttpFoundation\Response;
 16: use Symfony\Component\HttpFoundation\StreamedResponse;
 17: 
 18: use CRUDlex\CRUDFileProcessorInterface;
 19: use CRUDlex\CRUDEntity;
 20: 
 21: class CRUDSimpleFilesystemFileProcessor implements CRUDFileProcessorInterface {
 22: 
 23:     /**
 24:      * Constructs a file system path for the given parameters for storing the
 25:      * file of the file field.
 26:      *
 27:      * @param string $entityName
 28:      * the entity name
 29:      * @param CRUDEntity $entity
 30:      * the entity
 31:      * @param string $field
 32:      * the file field in the entity
 33:      *
 34:      * @return string
 35:      * the constructed path for storing the file of the file field
 36:      */
 37:     protected function getPath($entityName, CRUDEntity $entity, $field) {
 38:         return $entity->getDefinition()->getFilePath($field).'/'.$entityName.'/'.$entity->get('id').'/'.$field;
 39:     }
 40: 
 41:     /**
 42:      * {@inheritdoc}
 43:      */
 44:     public function createFile(Request $request, CRUDEntity $entity, $entityName, $field) {
 45:         $file = $request->files->get($field);
 46:         if ($file) {
 47:             $targetPath = $this->getPath($entityName, $entity, $field);
 48:             if (!file_exists($targetPath)) {
 49:                 mkdir($targetPath, 0777, true);
 50:             }
 51:             $file->move($targetPath, $file->getClientOriginalName());
 52:         }
 53:     }
 54: 
 55:     /**
 56:      * {@inheritdoc}
 57:      * For now, this implementation is defensive and doesn't delete ever.
 58:      */
 59:     public function updateFile(Request $request, CRUDEntity $entity, $entityName, $field) {
 60:         // We could first delete the old file, but for now, we are defensive and don't delete ever.
 61:         $this->createFile($request, $entity, $entityName, $field);
 62:     }
 63: 
 64:     /**
 65:      * {@inheritdoc}
 66:      * For now, this implementation is defensive and doesn't delete ever.
 67:      */
 68:     public function deleteFile(CRUDEntity $entity, $entityName, $field) {
 69:         // For now, we are defensive and don't delete ever.
 70:         /*
 71:         $targetPath = $this->getPath($entityName, $entity, $field);
 72:         $fileName = $entity->get($field);
 73:         $file = $targetPath.'/'.$fileName;
 74:         if ($fileName && file_exists($file)) {
 75:             unlink($file);
 76:         }
 77:         */
 78:     }
 79: 
 80:     /**
 81:      * {@inheritdoc}
 82:      */
 83:     public function renderFile(CRUDEntity $entity, $entityName, $field) {
 84:         $targetPath = $this->getPath($entityName, $entity, $field);
 85:         $fileName = $entity->get($field);
 86:         $file = $targetPath.'/'.$fileName;
 87:         $response = new Response('');
 88:         $finfo = finfo_open(FILEINFO_MIME_TYPE);
 89:         $mimeType = finfo_file($finfo, $file);
 90:         finfo_close($finfo);
 91:         $size = filesize($file);
 92:         if ($fileName && file_exists($file)) {
 93:             $response = new StreamedResponse(function () use ($file) {
 94:                 set_time_limit(0);
 95:                 $handle = fopen($file,"rb");
 96:                 if ($handle !== false) {
 97:                     $chunkSize = 1024 * 1024;
 98:                     while (!feof($handle)) {
 99:                         $buffer = fread($handle, $chunkSize);
100:                         echo $buffer;
101:                         flush();
102:                     }
103:                     fclose($handle);
104:                 }
105:             }, 200, array(
106:                 'Content-Type' => $mimeType,
107:                 'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
108:                 'Content-length' => $size
109:             ));
110:             $response->send();
111:         }
112:         return $response;
113:     }
114: }
115: 
CRUDlex API API documentation generated by ApiGen