-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
10 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
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 {} |
This file contains 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
This file contains 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
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" | ||
); | ||
} | ||
} |
This file contains 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
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" | ||
); | ||
} | ||
} |
This file contains 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
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 ]; | ||
} | ||
} |
This file contains 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
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" | ||
); | ||
} | ||
} |
This file contains 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
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(); | ||
} |
This file contains 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
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", | ||
); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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