src/Diplix/KMGBundle/Entity/File.php line 14

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Entity;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use League\Flysystem\Filesystem;
  6. use League\Flysystem\FilesystemOperator;
  7. use Psr\Http\Message\StreamInterface;
  8. /**
  9.  * @ORM\Entity(repositoryClass="Diplix\KMGBundle\Repository\FileRepository")
  10.  * @ORM\Table(name="files",indexes={@ORM\Index(name="index_type", columns={"type"})})
  11.  *
  12.  */
  13. class File extends BasicEntity
  14. {
  15.     const TYPE_IMAGE "image";
  16.     const TYPE_FILE "file";
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\Column(type="integer")
  20.      * @ORM\GeneratedValue(strategy="AUTO")
  21.      */
  22.     protected $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=255)
  25.      */
  26.     protected $fileName;
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=false)
  29.      */
  30.     protected $physicalFileName;
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=false)
  33.      */
  34.     protected $path;
  35.     /**
  36.      * @ORM\Column(type="string", length=64)
  37.      */
  38.     protected $type;
  39.     /**
  40.      * @ORM\Column(type="integer",options={"unsigned"=true})
  41.      */
  42.     protected $fileSize;
  43.     ///////////////////
  44.     public static function secureUniqueFilename($name,$origExt=null)
  45.     {
  46.         $slugify = new Slugify();
  47.         if ($origExt===null)
  48.         {
  49.             $i strrpos($name,'.');
  50.             $origExt substr($name,$i+1);
  51.         }
  52.         $origName str_replace($origExt'',$name);
  53.         return $slugify->slugify($origName) . '.' .uniqid(''false). '.' $slugify->slugify($origExt);
  54.     }
  55.     /**
  56.      * @param StreamInterface|string $stream
  57.      * @param string $fileName
  58.      * @param Filesystem|FilesystemOperator $target
  59.      * @param null|string $path
  60.      * @return File
  61.      */
  62.     public static function fromPsr7StreamOrString$stream$fileName$target$path null)
  63.     {
  64.         $newFn self::secureUniqueFilename($fileName);
  65.         $path = ($path!==null trim($path'/') : '') . '/';
  66.         if ($stream instanceof StreamInterface)
  67.             $target->write($path.$newFn$stream->getContents());
  68.         else
  69.             $target->write($path.$newFn$stream);
  70.         $F = new self();
  71.         $F->setFileName($fileName);
  72.         $F->setPhysicalFileName($newFn);
  73.         $F->setPath($path);
  74.         $F->setFileSize(  ($stream instanceof StreamInterface $stream->getSize() : strlen($stream) ));
  75.         $F->setType(self::detectMimeType($fileName));
  76.         return $F;
  77.     }
  78.     public static function detectMimeType($fileName)
  79.     {
  80.         if (@file_exists($fileName))
  81.         {
  82.             return mime_content_type($fileName);
  83.         }
  84.         $ext strtolower(trim(substr($fileName,strrpos($fileName,'.')+1)));
  85.         // fallback if only a name is provided using an incomplete list
  86.         $mimet = array(
  87.             'txt' => 'text/plain',
  88.             'htm' => 'text/html',
  89.             'html' => 'text/html',
  90.             'php' => 'text/html',
  91.             'css' => 'text/css',
  92.             'js' => 'application/javascript',
  93.             'json' => 'application/json',
  94.             'xml' => 'application/xml',
  95.             'swf' => 'application/x-shockwave-flash',
  96.             'flv' => 'video/x-flv',
  97.             // images
  98.             'png' => 'image/png',
  99.             'jpe' => 'image/jpeg',
  100.             'jpeg' => 'image/jpeg',
  101.             'jpg' => 'image/jpeg',
  102.             'gif' => 'image/gif',
  103.             'bmp' => 'image/bmp',
  104.             'ico' => 'image/vnd.microsoft.icon',
  105.             'tiff' => 'image/tiff',
  106.             'tif' => 'image/tiff',
  107.             'svg' => 'image/svg+xml',
  108.             'svgz' => 'image/svg+xml',
  109.             // archives
  110.             'zip' => 'application/zip',
  111.             'rar' => 'application/x-rar-compressed',
  112.             'exe' => 'application/x-msdownload',
  113.             'msi' => 'application/x-msdownload',
  114.             'cab' => 'application/vnd.ms-cab-compressed',
  115.             // audio/video
  116.             'mp3' => 'audio/mpeg',
  117.             'qt' => 'video/quicktime',
  118.             'mov' => 'video/quicktime',
  119.             // adobe
  120.             'pdf' => 'application/pdf',
  121.             'psd' => 'image/vnd.adobe.photoshop',
  122.             'ai' => 'application/postscript',
  123.             'eps' => 'application/postscript',
  124.             'ps' => 'application/postscript',
  125.             // ms office
  126.             'doc' => 'application/msword',
  127.             'rtf' => 'application/rtf',
  128.             'xls' => 'application/vnd.ms-excel',
  129.             'ppt' => 'application/vnd.ms-powerpoint',
  130.             'docx' => 'application/msword',
  131.             'xlsx' => 'application/vnd.ms-excel',
  132.             'pptx' => 'application/vnd.ms-powerpoint',
  133.             // open office
  134.             'odt' => 'application/vnd.oasis.opendocument.text',
  135.             'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  136.         );
  137.         if (array_key_exists($ext,$mimet))
  138.             return $mimet[$ext];
  139.         return 'application/octet-stream';
  140.     }
  141.     /////////////////
  142.     /**
  143.      * @return mixed
  144.      */
  145.     public function getId()
  146.     {
  147.         return $this->id;
  148.     }
  149.     /**
  150.      * @return mixed
  151.      */
  152.     public function getFileName()
  153.     {
  154.         return $this->fileName;
  155.     }
  156.     /**
  157.      * @param mixed $fileName
  158.      */
  159.     public function setFileName($fileName)
  160.     {
  161.         $this->fileName $fileName;
  162.     }
  163.     /**
  164.      * @return mixed
  165.      */
  166.     public function getPhysicalFileName()
  167.     {
  168.         return $this->physicalFileName;
  169.     }
  170.     /**
  171.      * @param mixed $physicalFileName
  172.      */
  173.     public function setPhysicalFileName($physicalFileName)
  174.     {
  175.         $this->physicalFileName $physicalFileName;
  176.     }
  177.     /**
  178.      * @return mixed
  179.      */
  180.     public function getType()
  181.     {
  182.         return $this->type;
  183.     }
  184.     /**
  185.      * @param mixed $type
  186.      */
  187.     public function setType($type)
  188.     {
  189.         $this->type $type;
  190.     }
  191.     /**
  192.      * @return mixed
  193.      */
  194.     public function getFileSize()
  195.     {
  196.         return $this->fileSize;
  197.     }
  198.     /**
  199.      * @param mixed $fileSize
  200.      */
  201.     public function setFileSize($fileSize)
  202.     {
  203.         $this->fileSize $fileSize;
  204.     }
  205.     /**
  206.      * @return mixed
  207.      */
  208.     public function getPath()
  209.     {
  210.         return $this->path;
  211.     }
  212.     /**
  213.      * @param mixed $path
  214.      */
  215.     public function setPath($path)
  216.     {
  217.         $this->path $path;
  218.     }
  219. }