partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

FileController.php (2227B)


      1 <?php
      2 
      3 namespace PartKeepr\UploadedFileBundle\Controller;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use Gaufrette\Exception\FileNotFound;
      7 use PartKeepr\UploadedFileBundle\Entity\UploadedFile;
      8 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
      9 use Symfony\Component\HttpFoundation\Response;
     10 
     11 abstract class FileController extends Controller
     12 {
     13     /**
     14      * Returns the mimetype icon for an uploaded file.
     15      *
     16      * @param int $id The ID of the entity
     17      *
     18      * @return Response
     19      */
     20     public function getMimeTypeIconAction($id)
     21     {
     22         /**
     23          * @var EntityManager
     24          */
     25         $em = $this->getDoctrine()->getManager();
     26 
     27         /**
     28          * @var UploadedFile
     29          */
     30         $file = $em->find($this->getEntityClass(), $id);
     31 
     32         $icon = $this->get('partkeepr_mimetype_icon_service')->getMimetypeIcon($file->getMimeType());
     33 
     34         return new Response(file_get_contents($icon), 200, ['Content-Type' => 'image/svg+xml']);
     35     }
     36 
     37     /**
     38      * Returns the file. Directly sends the response to the browser.
     39      *
     40      * @param int $id The ID of the file
     41      *
     42      * @return Response
     43      */
     44     public function getFileAction($id)
     45     {
     46         /**
     47          * @var EntityManager
     48          */
     49         $em = $this->getDoctrine()->getManager();
     50 
     51         /**
     52          * @var UploadedFile
     53          */
     54         $file = $em->find($this->getEntityClass(), $id);
     55 
     56         $storage = $this->get('partkeepr_uploadedfile_service')->getStorage($file);
     57 
     58         try {
     59             return new Response(
     60                 $storage->read($file->getFullFilename()),
     61                 200,
     62                 ['Content-Type' => $file->getMimeType()]
     63             );
     64         } catch (FileNotFound $e) {
     65             $this->get('logger')->addError(
     66                 sprintf(
     67                     'File %s not found in storage %s',
     68                     $file->getFullFilename(),
     69                     $file->getType()
     70                 )
     71             );
     72 
     73             return new Response(
     74                 '404 File not found',
     75                 404
     76             );
     77         }
     78     }
     79 
     80     /**
     81      * Returns the entity class (FQDN) for operation.
     82      *
     83      * @return string
     84      */
     85     abstract protected function getEntityClass();
     86 }