Implements PHP infrastructure for jQuery File Upload in Nette FW.
Created
May 15, 2013 09:49
-
-
Save Kedrigern/5582844 to your computer and use it in GitHub Desktop.
Implements PHP infrastructure for http://blueimp.github.io/jQuery-File-Upload in Nette FW.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Nette\Utils\Finder; | |
| /** | |
| * @todo fake delete (delete to global trash) | |
| */ | |
| class UploadManager extends \Nette\Object | |
| { | |
| /** @var string upload dir */ | |
| private $uploadDir; | |
| /** @var string */ | |
| private $thumbnailDir = 'thumbnail/'; | |
| /** @param string $path */ | |
| public function __construct($path) | |
| { | |
| $this->setUploadDir($path); | |
| } | |
| /** @return string */ | |
| public function getUploadDir() | |
| { | |
| return $this->uploadDir; | |
| } | |
| /** @return string */ | |
| public function getThumbnailDir() | |
| { | |
| return $this->uploadDir . $this->thumbnailDir; | |
| } | |
| /** | |
| * @param string $path | |
| * @throws InvalidArgumentException | |
| */ | |
| public function setUploadDir($path) | |
| { | |
| // path does not end with slash | |
| if( !preg_match('#^[\w/]+/$#', $path )) { | |
| $path .= '/'; | |
| } | |
| if(!is_dir($path)) { | |
| if( ! mkdir($path) ) | |
| throw new \InvalidArgumentException("$path does not exists and dir cannot be create."); | |
| } | |
| if(!is_dir($path. $this->thumbnailDir) ) { | |
| if( ! mkdir($path . $this->thumbnailDir)) | |
| throw new \InvalidArgumentException("$path does not exists and dir cannot be create."); | |
| } | |
| $this->uploadDir = $path; | |
| } | |
| /** @return array */ | |
| public function getFiles() | |
| { | |
| $files = array(); | |
| foreach (Finder::findFiles('*.jpg', '*.png', '*.pdf')->in($this->uploadDir) as $file) { | |
| $files[] = array( | |
| 'name' => $file->getBasename(), | |
| 'size' => $file->getSize(), | |
| 'url' => 'http://localhost/new.koupelny/www/files/'.$file->getBasename(), | |
| 'thumbnail_url' => 'http://localhost/new.koupelny/www/files/thumbnail/'.$file->getBasename(), | |
| ); | |
| } | |
| return $files; | |
| } | |
| /** | |
| * @param array|\Nette\Http\FileUpload file or files to save | |
| * @return array | |
| */ | |
| public function save($files) | |
| { | |
| $uploadedFiles = array(); | |
| switch( gettype($files) ) { | |
| case 'object': | |
| $uploadedFiles[] = $this->saveOne($files); | |
| break; | |
| case 'array': | |
| foreach($files as $file) { | |
| $uploadedFiles[] = $this->saveOne($file); | |
| } | |
| break; | |
| default: | |
| throw new \Nette\InvalidArgumentException(); | |
| break; | |
| } | |
| return $uploadedFiles; | |
| } | |
| /** @param null $files */ | |
| public function delete($files = null) | |
| { | |
| switch( gettype($files) ) { | |
| case 'string': | |
| $this->deleteOne($files); | |
| break; | |
| case 'array': | |
| foreach($files as $file) { | |
| $this->deleteOne($file); | |
| } | |
| break; | |
| default: | |
| $this->deleteAll(); | |
| break; | |
| } | |
| } | |
| /** | |
| * Save one file to the uploadDir | |
| * | |
| * @param Nette\Http\FileUpload $file | |
| * @return array info about file | |
| */ | |
| private function saveOne(\Nette\Http\FileUpload $file) | |
| { | |
| $name = $file->getSanitizedName(); | |
| $path = $this->uploadDir . $name; | |
| $file->move($path); | |
| $img = \Nette\Image::fromFile($path); | |
| $img->resize(80, 80)->save($this->uploadDir . 'thumbnail/' . $name); | |
| return array( | |
| 'name' => $name, | |
| 'size' => filesize($path), | |
| 'url' => 'http://localhost/new.koupelny/www/files/' . $name, | |
| 'thumbnail_url' => 'http://localhost/new.koupelny/www/files/thumbnail/'.$name, | |
| ); | |
| } | |
| /** | |
| * Delete one file in uploadDir | |
| * | |
| * @param string $name | |
| */ | |
| private function deleteOne($name) | |
| { | |
| if( file_exists($this->uploadDir . $name) ) { | |
| unlink($this->uploadDir . $name); | |
| } | |
| if( file_exists($this->uploadDir . $this->thumbnailDir . $name) ) { | |
| unlink($this->uploadDir . $this->thumbnailDir . $name); | |
| } | |
| } | |
| /** | |
| * Delete all file with sufixes: jpg, png, pdf in given directory tree (recursivly) | |
| */ | |
| private function deleteAll() | |
| { | |
| foreach (Finder::findFiles('*.jpg', '*.png', '*.pdf')->from($this->uploadDir) as $file) { | |
| unlink( $file->getRealPath() ); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace BackendModule; | |
| use Nette\Application\UI\Form; | |
| class UploadPresenter extends BasePresenter | |
| { | |
| /** @var string */ | |
| private $uploadPath; | |
| public function startup() | |
| { | |
| parent::startup(); | |
| $this->uploadPath = __DIR__ . '/../../../www/files/'; | |
| } | |
| public function actionDefault() | |
| { | |
| switch ($_SERVER['REQUEST_METHOD']) { | |
| case 'OPTIONS': | |
| case 'HEAD': | |
| //$this->head(); | |
| break; | |
| case 'GET': | |
| $this->handleImages(); | |
| break; | |
| case 'PATCH': | |
| case 'PUT': | |
| case 'POST': | |
| $this->handleUpload(); | |
| break; | |
| case 'DELETE': | |
| break; | |
| default: | |
| header('HTTP/1.1 405 Method Not Allowed'); | |
| break; | |
| } | |
| } | |
| public function handleImages() | |
| { | |
| $files = $this->imagesFilter(scandir($this->uploadPath)); | |
| $this->payload->files = array(); | |
| foreach( $files as $file) { | |
| $this->payload->files[] = | |
| array( | |
| 'name' => $file, | |
| 'size' => filesize($this->uploadPath . $file), | |
| "url" => 'http://localhost/new.koupelny/www/files/'.$file, | |
| "thumbnail_url" => 'http://localhost/new.koupelny/www/files/thumbnail/'.$file, | |
| "delete_url" => $this->link('delete!', array('file'=>$file)), // 'http://localhost/new.koupelny/www/admin/category/upload?file=206.jpg', | |
| "delete_type" =>"DELETE" | |
| ); | |
| } | |
| $this->sendPayload(); | |
| } | |
| /** @param string $file */ | |
| public function handleDelete($file) | |
| { | |
| if( file_exists( $this->uploadPath . $file) ) { | |
| unlink($this->uploadPath . $file); | |
| } | |
| if( file_exists( $this->uploadPath . 'thumbnail/' . $file) ) { | |
| unlink($this->uploadPath . 'thumbnail/' . $file); | |
| } | |
| } | |
| public function handleUpload() | |
| { | |
| $files = $this->getHttpRequest()->getFiles(); | |
| foreach( $files as $file) { | |
| if( $file[0]->isOk() ) { | |
| $name = $file[0]->getSanitizedName(); | |
| $file[0]->move($this->uploadPath . $name); | |
| $img = \Nette\Image::fromFile($this->uploadPath . $name); | |
| $img->resize(80,80)->save($this->uploadPath . 'thumbnail/' . $name); | |
| } | |
| } | |
| $this->payload->files = array( | |
| array( | |
| 'name' => $name, | |
| 'size' => $file[0]->getSize(), | |
| "url" => 'http://localhost/new.koupelny/www/files/' . $name, | |
| "thumbnail_url" => 'http://localhost/new.koupelny/www/files/thumbnail/'. $name, | |
| "delete_url" => $this->link('delete!', array('file'=>$name)),// 'http://localhost/new.koupelny/www/admin/category/upload?file=206.jpg', | |
| "delete_type" =>"DELETE" | |
| ) | |
| ); | |
| $this->sendPayload(); | |
| } | |
| private function prepareImages($path) | |
| { | |
| } | |
| private function imagesFilter(array $files) | |
| { | |
| $images = array(); | |
| foreach($files as $file) { | |
| if( preg_match('/\w*.jpg/', $file)) $images[] = $file; | |
| if( preg_match('/\w*.png/', $file)) $images[] = $file; | |
| } | |
| return $images; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment