<?php
namespace Diplix\KMGBundle\Entity;
use Cocur\Slugify\Slugify;
use Doctrine\ORM\Mapping as ORM;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use Psr\Http\Message\StreamInterface;
/**
* @ORM\Entity(repositoryClass="Diplix\KMGBundle\Repository\FileRepository")
* @ORM\Table(name="files",indexes={@ORM\Index(name="index_type", columns={"type"})})
*
*/
class File extends BasicEntity
{
const TYPE_IMAGE = "image";
const TYPE_FILE = "file";
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $fileName;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $physicalFileName;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $path;
/**
* @ORM\Column(type="string", length=64)
*/
protected $type;
/**
* @ORM\Column(type="integer",options={"unsigned"=true})
*/
protected $fileSize;
///////////////////
public static function secureUniqueFilename($name,$origExt=null)
{
$slugify = new Slugify();
if ($origExt===null)
{
$i = strrpos($name,'.');
$origExt = substr($name,$i+1);
}
$origName = str_replace($origExt, '',$name);
return $slugify->slugify($origName) . '.' .uniqid('', false). '.' . $slugify->slugify($origExt);
}
/**
* @param StreamInterface|string $stream
* @param string $fileName
* @param Filesystem|FilesystemOperator $target
* @param null|string $path
* @return File
*/
public static function fromPsr7StreamOrString( $stream, $fileName, $target, $path = null)
{
$newFn = self::secureUniqueFilename($fileName);
$path = ($path!==null ? trim($path, '/') : '') . '/';
if ($stream instanceof StreamInterface)
$target->write($path.$newFn, $stream->getContents());
else
$target->write($path.$newFn, $stream);
$F = new self();
$F->setFileName($fileName);
$F->setPhysicalFileName($newFn);
$F->setPath($path);
$F->setFileSize( ($stream instanceof StreamInterface ? $stream->getSize() : strlen($stream) ));
$F->setType(self::detectMimeType($fileName));
return $F;
}
public static function detectMimeType($fileName)
{
if (@file_exists($fileName))
{
return mime_content_type($fileName);
}
$ext = strtolower(trim(substr($fileName,strrpos($fileName,'.')+1)));
// fallback if only a name is provided using an incomplete list
$mimet = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/msword',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
if (array_key_exists($ext,$mimet))
return $mimet[$ext];
return 'application/octet-stream';
}
/////////////////
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getFileName()
{
return $this->fileName;
}
/**
* @param mixed $fileName
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
/**
* @return mixed
*/
public function getPhysicalFileName()
{
return $this->physicalFileName;
}
/**
* @param mixed $physicalFileName
*/
public function setPhysicalFileName($physicalFileName)
{
$this->physicalFileName = $physicalFileName;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* @return mixed
*/
public function getFileSize()
{
return $this->fileSize;
}
/**
* @param mixed $fileSize
*/
public function setFileSize($fileSize)
{
$this->fileSize = $fileSize;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*/
public function setPath($path)
{
$this->path = $path;
}
}