An error occurred while loading the file. Please try again.
-
Guillaume Perréal authoreda3da09fb
<?php
/*
* Copyright (C) 2015 IRSTEA
* All rights reserved.
*/
namespace Irstea\FileUploadBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gaufrette\Filesystem;
use Gaufrette\Stream;
use Gaufrette\StreamMode;
use Rhumsaa\Uuid\Uuid;
/**
* @ORM\Entity(repositoryClass="Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository")
* @ORM\EntityListeners({"Irstea\FileUploadBundle\Listener\UploadedFileListener"})
*/
class UploadedFile
{
/**
* @ORM\Id
* @ORM\Column(type="guid")
* @var string
*/
private $id;
/**
* @ORM\Column(type="string", length=1024)
* @var string
*/
private $originalFilename;
/**
* @ORM\Column(type="string", length=1024)
* @var string
*/
private $path;
/**
*
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $mimeType;
/**
* @ORM\Column(type="integer", nullable=true)
* @var int
*/
private $size;
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @var string
*/
private $checksum;
/**
* @ORM\Column(type="boolean", length=64)
* @var boolean
*/
private $orphelin;
/**
* @ORM\Column(type="datetime")
* @var DateTime
*/
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
private $createdAt;
/**
* @ORM\Column(type="json_array", nullable=true)
* @var array
*/
private $metadata;
/**
* @var Filesystem
*/
private $filesystem;
/**
*
*/
public function __construct()
{
$this->id = Uuid::uuid4()->toString();
$this->orphelin = true;
$this->path = "orphan/".$this->id;
$this->createdAt = new DateTime('now');
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set originalFilename
*
* @param string $originalFilename
* @return UploadedFile
*/
public function setOriginalFilename($originalFilename)
{
$this->originalFilename = $originalFilename;
return $this;
}
/**
* Get originalFilename
*
* @return string
*/
public function getOriginalFilename()
{
return $this->originalFilename;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set path
*
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
* @param string $path
* @return UploadedFile
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Set mimeType
*
* @param string $mimeType
* @return UploadedFile
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* Get mimeType
*
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* Set size
*
* @param integer $size
* @return UploadedFile
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size
*
* @return integer
*/
public function getSize()
{
return $this->size;
}
/**
* Set checksum
*
* @param string $checksum
* @return UploadedFile
*/
public function setChecksum($checksum)
{
$this->checksum = $checksum;
return $this;
}
/**
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
* Get checksum
*
* @return string
*/
public function getChecksum()
{
return $this->checksum;
}
/**
* Set orphelin
*
* @param boolean $orphelin
* @return UploadedFile
*/
public function setOrphelin($orphelin)
{
$this->orphelin = $orphelin;
return $this;
}
/**
* Get orphelin
*
* @return boolean
*/
public function isOrphelin()
{
return $this->orphelin;
}
/**
* Get createdAt
*
* @return \datettime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set metadata
*
* @param array $metadata
* @return UploadedFile
*/
public function setMetadata(array $metadata = null)
{
$this->metadata = $metadata;
return $this;
}
/**
* Get metadata
*
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* @return string
*/
public function __toString()
{
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
$unit = "";
$size = $this->size ?: 0;
if($size >= 10240) {
$size /= 1024;
$unit = "k";
if($size >= 10240) {
$size /= 1024;
$unit = "m";
}
}
return sprintf("%s (%s, %d%so)", $this->originalFilename, $this->mimeType ?: '?/?', $size, $unit);
}
/**
* @param Filesystem $filesystem
* @return self
*/
public function setFilesystem(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
return $this;
}
/**
* @return boolean
*/
public function isValid()
{
$fs = $this->filesystem;
$path = $this->getPath();
return
$fs->has($path) &&
(null === $this->size || $fs->size($path) === $this->size) &&
(null === $this->checksum || $fs->checksum($path) === $this->checksum);
}
/**
*
* @param type $mode
* @return Stream
*/
public function open($mode = 'rb')
{
$stream = $this->filesystem->createStream($this->getPath());
$stream->open(new StreamMode($mode));
return $stream;
}
}