START<?php namespace Media\AdminBundle\Model; use Symfony\Component\HttpFoundation\File\File; use Exception; use Symfony\Component\HttpFoundation\File\Exception\FileException; /** * */ class Resource { const DEFAULT_WEB_PATH = "web"; const DEFAULT_UPLOAD_PATH = DIRECTORY_SEPARATOR."uploads".DIRECTORY_SEPARATOR."media".DIRECTORY_SEPARATOR; const DEFAULT_UPLOAD_ITEM_PATH = DIRECTORY_SEPARATOR."uploads".DIRECTORY_SEPARATOR."items".DIRECTORY_SEPARATOR; protected $file; protected $folder; protected $web_dir; protected $type; protected $size; public function __construct() { } public function getWeb() { return $this->web_dir; } public function setWeb($webDir) { $this->web_dir = $webDir; } /** * @param string file * @return void */ public function setFile($file) { $this->file = $file; } /** * @return string */ public function getFile() { return $this->file; } /** * @param string folder * @return void */ public function setFolder($folder) { $this->folder = $folder; } /** * @return string */ public function getFolder() { if (null === $this->folder) { throw new Exception("folder is required for your ressource", 1); } return $this->folder; } /** * @return void */ public function upload() { if ($this->file instanceof File) { $name = sha1($this->file->getClientOriginalName() . uniqid() . getrandmax()) . '.' . $this->file->guessExtension(); $this->type = $this->file->getMimeType(); $this->size = $this->file->getClientSize(); $this->file->move($this->getWeb() . $this->folder, $name); $this->file = $name; return true; } else { throw new FileException("It must be a Symfony\Component\HttpFoundation\File\File instance"); } } /** * @return array */ public function toArray() { return array( 'folder' => $this->folder, 'file' => $this->file, 'type' => $this->type, 'size' => $this->size, ); } } END