Skip to content

Commit

Permalink
Rozhraní:
Browse files Browse the repository at this point in the history
* Upraven způsob vykreslení chyby uploadu.

Controller:
* Přidána kontrola typu souboru, zda vyhovuje filteru.

Filtry:
* Vytvořeny filtrovací třídy a rozhraní
* Filtery pro obrázky, dokumenty, audio soubory a archivy.

Výjimky:
* Přidána výjimka InvalidFileException.
  • Loading branch information
JZechy committed Apr 21, 2016
1 parent b7d2c49 commit 3466f36
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Zet\FileUpload;

/**
* Špatný typ souboru.
* @author Zechy <[email protected]>
* @package Zet\FileUpload
*/
class InvalidFileException extends \Exception {}
46 changes: 46 additions & 0 deletions src/FileUploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ public static function getScripts($basePath) {
# --------------------------------------------------------------------
# Control definition
# --------------------------------------------------------------------
/**
* Povolí nahrávat pouze obrázky png, jpeg, jpg, gif.
* @var string
*/
const FILTER_IMAGES = 'Zet\FileUpload\Filter\ImageFilter';

/**
* Povolí nahrávat pouze dokumenty typu doc, docx, xls, xlsx, ppt, pptx, pdf.
* @var string
*/
const FILTER_DOCUMENTS = 'Zet\FileUpload\Filter\DocumentFilter';

/**
* Povolí nahrávat soubory zip, tar, rar, 7z.
* @var string
*/
const FILTER_ARCHIVE = 'Zet\FileUpload\Filter\ArchiveFilter';

/**
* Povolí nahrávat pouze soubory mp3, ogg, aiff.
* @var string
*/
const FILTER_AUDIO = 'Zet\FileUpload\Filter\AudioFilter';

/**
* @var \Nette\DI\Container
*/
Expand Down Expand Up @@ -96,6 +120,12 @@ public static function getScripts($basePath) {
*/
private $uploadModel;

/**
* Třída pro filtrování nahrávaných souborů.
* @var string
*/
private $fileFilter;

/**
* FileUploadControl constructor.
* @param string $name Název inputu.
Expand Down Expand Up @@ -211,6 +241,22 @@ public function getFileSizeString() {
return $this->fileSizeString;
}

/**
* @return string
*/
public function getFileFilter() {
return $this->fileFilter;
}

/**
* Nastaví třídu pro filtrování nahrávaných souborů.
* @param string $fileFilter
*/
public function setFileFilter($fileFilter) {
$this->fileFilter = $fileFilter;
}


# --------------------------------------------------------------------
# Methods
# --------------------------------------------------------------------
Expand Down
25 changes: 25 additions & 0 deletions src/Filter/ArchiveFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Class ArchiveFilter
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
class ArchiveFilter extends BaseFilter {

/**
* Vrátí seznam povolených typů souborů s jejich typickou koncovkou.
* @example array("text/plain" => "txt")
* @return string[]
*/
protected function getMimeTypes() {
return array(
"application/zip" => "zip",
"application/x-rar-compressed" => "rar",
"application/x-tar" => "tar",
"application/x-7z-compressed" => "7z"
);
}
}
25 changes: 25 additions & 0 deletions src/Filter/AudioFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Class AudioFilter
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
class AudioFilter extends BaseFilter {

/**
* Vrátí seznam povolených typů souborů s jejich typickou koncovkou.
* @example array("text/plain" => "txt")
* @return string[]
*/
protected function getMimeTypes() {
return array(
"audio/mpeg3" => "mp3",
"audio/x-mpeg-3" => "mp3",
"audio/ogg" => "ogg",
"audio/x-aiff" => "aiff"
);
}
}
54 changes: 54 additions & 0 deletions src/Filter/BaseFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Class BaseFilter
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
abstract class BaseFilter extends \Nette\Object implements IMimeTypeFilter {

/**
* Vrátí seznam povolených typů souborů s jejich typickou koncovkou.
* @example array("text/plain" => "txt")
* @return string[]
*/
abstract protected function getMimeTypes();

/**
* Ověří mimetype předaného souboru.
* @param \Nette\Http\FileUpload $file Nahraný soubor k ověření.
* @return bool Má soubor správný mimetype?
*/
public function checkType(\Nette\Http\FileUpload $file) {
if(\Nette\Utils\Arrays::searchKey($this->getMimeTypes(), $file->getContentType()) !== FALSE) {
return TRUE;
} else {
// Pokud se nepodaří ověřit mimetype, ověříme alespoň koncovku.
if( array_search($this->getExtension($file->getName(), array_unique($this->getMimeTypes())) ) !== FALSE) {
return TRUE;
} else {
return FALSE;
}
}
}

/**
* Vrátí seznam povolených typů souborů.
* @return string
*/
public function getAllowedTypes() {
return implode(array_unique($this->getMimeTypes()), ", ");
}

/**
* Vrátí koncovku souboru.
* @param string $filename
* @return string
*/
private function getExtension($filename) {
$exploded = explode(".", $filename);
return $exploded[ count($exploded) - 1 ];
}
}
29 changes: 29 additions & 0 deletions src/Filter/DocumentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Class DocumentFilter
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
class DocumentFilter extends BaseFilter {

/**
* Vrátí seznam povolených typů souborů s jejich typickou koncovkou.
* @example array("text/plain" => "txt")
* @return string[]
*/
protected function getMimeTypes() {
return array(
"text/plain" => "txt",
"application/msword" => "doc",
"application/vnd.ms-excel" => "xls",
"application/pdf" => "pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
"application/vnd.ms-powerpoint" => "ppt",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => "pptx"
);
}
}
25 changes: 25 additions & 0 deletions src/Filter/IMimeTypeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Interface IMimeTypeFilters
* Rozhraní pro kontrolu Mime typu souboru.
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
interface IMimeTypeFilter {

/**
* Ověří mimetype předaného souboru.
* @param \Nette\Http\FileUpload $file Nahraný soubor k ověření.
* @return bool Má soubor správný mimetype?
*/
public function checkType(\Nette\Http\FileUpload $file);

/**
* Vrátí seznam povolených typů souborů.
* @return string
*/
public function getAllowedTypes();
}
26 changes: 26 additions & 0 deletions src/Filter/ImageFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Zet\FileUpload\Filter;

/**
* Class ImageFilter
* @author Zechy <[email protected]>
* @package Zet\FileUpload\Filter
*/
class ImageFilter extends BaseFilter {

/**
* Vrátí seznam povolených typů souborů s jejich typickou koncovkou.
* @example array("text/plain" => "txt")
* @return string[]
*/
protected function getMimeTypes() {
return array(
"image/png" => "png",
"image/pjpeg" => "jpeg",
"image/jpeg" => "jpg",
"image/gif" => "gif",
);
}

}
38 changes: 37 additions & 1 deletion src/Model/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class UploadController extends \Nette\Application\UI\Control {
*/
private $request;

/**
* @var \Zet\FileUpload\Filter\IMimeTypeFilter
*/
private $filter;

/**
* UploadController constructor.
* @param \Zet\FileUpload\FileUploadControl $uploadControl
Expand All @@ -34,6 +39,25 @@ public function setRequest($request) {
$this->request = $request;
}

/**
* @return \Zet\FileUpload\Filter\IMimeTypeFilter
*/
public function getFilter() {
if(is_null($this->filter)) {
$className = $this->uploadControl->getFileFilter();
$filterClass = new $className;
if($filterClass instanceof \Zet\FileUpload\Filter\IMimeTypeFilter) {
$this->filter = $filterClass;
} else {
throw new \Nette\UnexpectedValueException(
"Třída pro filtrování souborů neimplementuje rozhraní \\Zet\\FileUpload\\Filter\\IMimeTypeFilter."
);
}
}

return $this->filter;
}

/**
* Vytvoření šablony s JavaScriptem pro FileUpload.
* @return string
Expand Down Expand Up @@ -75,6 +99,10 @@ public function handleUpload() {
$cache = $this->uploadControl->getCache();

try {
if(!$this->getFilter()->checkType($file)) {
throw new \Zet\FileUpload\InvalidFileException($this->getFilter()->getAllowedTypes());
}

if($file->isOk()) {
$returnData = $model->save($file);

Expand All @@ -86,6 +114,14 @@ public function handleUpload() {
}
$cache->save($this->uploadControl->getHtmlId(), $cacheFiles);
}

} catch(\Zet\FileUpload\InvalidFileException $e) {
$this->presenter->sendResponse(new \Nette\Application\Responses\JsonResponse(array(
"id" => $this->request->getPost("id"),
"error" => 100,
"errorMessage" => $e->getMessage()
)));

} catch(\Exception $e) {
$this->presenter->sendResponse(new \Nette\Application\Responses\JsonResponse(array(
"id" => $this->request->getPost("id"),
Expand Down Expand Up @@ -116,6 +152,6 @@ public function handleRemove() {
}

public function validate() {
// Nette 2.3.10 by pass
// Nette 2.3.10 bypass
}
}
2 changes: 1 addition & 1 deletion src/Template/control.latte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<div class="row">
<div class="col-xs-12">
<table class="table table-striped zet-fileupload-table" id="{$htmlId}-table">
<table class="table zet-fileupload-table" id="{$htmlId}-table">
<tbody id="{$htmlId}-table-tbody">
</tbody>
</table>
Expand Down

0 comments on commit 3466f36

Please sign in to comment.