Skip to content

Commit

Permalink
Přejmenování souboru, tokenizace.
Browse files Browse the repository at this point in the history
JavaScript:
* Zpracování přejmenování souboru
* Implementace tokenizace.

Model:
* Rozhraní aktualizováno o metodu rename.

Šablona:
* Předán token.
  • Loading branch information
JZechy committed Apr 21, 2016
1 parent 3466f36 commit 8fe90aa
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 19 deletions.
59 changes: 52 additions & 7 deletions src/FileUploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class FileUploadControl extends \Nette\Forms\Controls\UploadControl {
public static function register($systemContainer, $uploadModel = NULL) {
$class = __CLASS__;
\Nette\Forms\Container::extensionMethod("addFileUpload", function (
\Nette\Forms\Container $container, $name, $maxFiles = 25, $maxFileSize = null
\Nette\Forms\Container $container, $name, $maxFiles = 25, $maxFileSize = NULL
) use ($class, $systemContainer, $uploadModel) {
$component = new $class($name, $maxFiles, $maxFileSize); /** @var FileUploadControl $component */
$component = new $class($name, $maxFiles, $maxFileSize);
/** @var FileUploadControl $component */
$component->setContainer($systemContainer);
$component->setUploadModel($uploadModel);
$container->addComponent($component, $name);
Expand Down Expand Up @@ -126,23 +127,29 @@ public static function getScripts($basePath) {
*/
private $fileFilter;

/**
* @var string
*/
private $token;

/**
* FileUploadControl constructor.
* @param string $name Název inputu.
* @param int $maxFiles Maximální počet souborů.
* @param string $maxFileSize Maximální velikosti souboru.
*/
public function __construct($name, $maxFiles, $maxFileSize = null) {
public function __construct($name, $maxFiles, $maxFileSize = NULL) {
parent::__construct($name);
$this->maxFiles = $maxFiles;
if(is_null($maxFileSize)) {
$this->maxFileSize = $this->parseIniSize(ini_get("upload_max_filesize"));
$this->fileSizeString = ini_get("upload_max_filesize") ."B";
$this->fileSizeString = ini_get("upload_max_filesize") . "B";
} else {
$this->maxFileSize = $this->parseIniSize($maxFileSize);
$this->fileSizeString = $maxFileSize ."B";
$this->fileSizeString = $maxFileSize . "B";
}
$this->controller = new Model\UploadController($this);
$this->token = uniqid();
}

/**
Expand All @@ -158,6 +165,7 @@ protected function attached($form) {
# --------------------------------------------------------------------
/**
* @param \Nette\DI\Container $container
* @internal
*/
public function setContainer($container) {
$this->container = $container;
Expand All @@ -169,13 +177,15 @@ public function setContainer($container) {

/**
* @return \Nette\DI\Container
* @internal
*/
public function getContainer() {
return $this->container;
}

/**
* @return int
* @internal
*/
public function getMaxFiles() {
return $this->maxFiles;
Expand All @@ -190,6 +200,7 @@ public function setMaxFiles($maxFiles) {

/**
* @return Model\IUploadModel
* @internal
*/
public function getUploadModel() {
if(is_null($this->uploadModel)) {
Expand All @@ -215,6 +226,7 @@ public function setUploadModel($uploadModel) {

/**
* @return int
* @internal
*/
public function getMaxFileSize() {
return $this->maxFileSize;
Expand All @@ -236,13 +248,15 @@ public function getCache() {

/**
* @return string
* @internal
*/
public function getFileSizeString() {
return $this->fileSizeString;
}

/**
* @return string
* @internal
*/
public function getFileFilter() {
return $this->fileFilter;
Expand All @@ -256,10 +270,36 @@ public function setFileFilter($fileFilter) {
$this->fileFilter = $fileFilter;
}

/**
* Vrátí název pro frontu s tokenem.
* @return string
* @internal
*/
public function getTokenizedCacheName($token) {
return $this->getHtmlId() . "-" . $token;
}

/**
* Vrátí identifikační token.
* @return string
* @internal
*/
public function getToken() {
return $this->token;
}

# --------------------------------------------------------------------
# Methods
# --------------------------------------------------------------------
/**
* Získání identifikačního tokenu.
*/
public function loadHttpData() {
parent::loadHttpData();
$request = $this->getContainer()->getByType('\Nette\Http\Request'); /** @var \Nette\Http\Request $request */
$this->token = $request->getPost($this->getHtmlName() ."-token");
}

/**
* @return \Nette\Utils\Html
*/
Expand All @@ -268,6 +308,11 @@ public function getControl() {

$container = \Nette\Utils\Html::el("div class='zet-fileupload-container'");
$container->id = $this->getHtmlId() . "-container";

$token = \Nette\Utils\Html::el("input type='hidden' value='" . $this->token . "'");
$token->name = $this->getHtmlName() ."-token";
$container->add($token);

$container->add($this->controller->getJavaScriptTemplate());
$container->add($this->controller->getControlTemplate());

Expand All @@ -279,8 +324,8 @@ public function getControl() {
* @return mixed|NULL
*/
public function getValue() {
$files = $this->cache->load($this->getHtmlId());
$this->cache->remove($this->getHtmlId());
$files = $this->cache->load($this->getTokenizedCacheName($this->token));
$this->cache->remove($this->getTokenizedCacheName($this->token));

return $files;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Model/BaseUploadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ public function save(\Nette\Http\FileUpload $file) {
public function remove($uploaded) {
# By Pass...
}

/**
* Zpracování přejmenování souboru.
* @param $upload Hodnota navrácená funkcí save.
* @param $newName Nové jméno souboru.
* @return mixed Vlastní návratová hodnota.
*/
public function rename($upload, $newName) {
return \Nette\Utils\Strings::webalize($newName);
}
}
8 changes: 8 additions & 0 deletions src/Model/IUploadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ interface IUploadModel {
*/
public function save(\Nette\Http\FileUpload $file);

/**
* Zpracování přejmenování souboru.
* @param $upload Hodnota navrácená funkcí save.
* @param $newName Nové jméno souboru.
* @return mixed Vlastní návratová hodnota.
*/
public function rename($upload, $newName);

/**
* Zpracování požadavku o smazání souboru.
* @param $uploaded Hodnota navrácená funkcí save.
Expand Down
30 changes: 26 additions & 4 deletions src/Model/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ public function getFilter() {
public function getJavaScriptTemplate() {
$template = $this->template;
$template->setFile(__DIR__ . "/../Template/javascript.latte");

$template->uploadUrl = $this->link("upload");
$template->deleteLink = $this->link("remove");
$template->renameLink = $this->link("rename");
$template->inputId = $this->uploadControl->getHtmlId();
$template->maxFiles = $this->uploadControl->getMaxFiles();
$template->maxFileSize = $this->uploadControl->getMaxFileSize();
$template->fileSizeString = $this->uploadControl->getFileSizeString();
$template->productionMode = \Tracy\Debugger::$productionMode;
$template->token = $this->uploadControl->getToken();

return (string) $template;
}
Expand All @@ -94,6 +97,8 @@ public function getControlTemplate() {
*/
public function handleUpload() {
$files = $this->request->getFiles();
$token = $this->request->getPost("token");

$file = $files[ $this->uploadControl->getHtmlName() ];
$model = $this->uploadControl->getUploadModel();
$cache = $this->uploadControl->getCache();
Expand All @@ -106,13 +111,13 @@ public function handleUpload() {
if($file->isOk()) {
$returnData = $model->save($file);

$cacheFiles = $cache->load($this->uploadControl->getHtmlId());
$cacheFiles = $cache->load($this->uploadControl->getTokenizedCacheName($token));
if(empty($cacheFiles)) {
$cacheFiles = array($this->request->getPost("id") => $returnData);
} else {
$cacheFiles[ $this->request->getPost("id") ] = $returnData;
}
$cache->save($this->uploadControl->getHtmlId(), $cacheFiles);
$cache->save($this->uploadControl->getTokenizedCacheName($token), $cacheFiles);
}

} catch(\Zet\FileUpload\InvalidFileException $e) {
Expand Down Expand Up @@ -141,13 +146,30 @@ public function handleUpload() {
*/
public function handleRemove() {
$id = $this->request->getQuery("id");
$token = $this->request->getQuery("token");

$cache = $this->uploadControl->getCache();
$cacheFiles = $cache->load($this->uploadControl->getHtmlId());
$cacheFiles = $cache->load($this->uploadControl->getTokenizedCacheName($token));
if(isset($cacheFiles[$id])) {
$this->uploadControl->getUploadModel()->remove($cacheFiles[$id]);
unset($cacheFiles[$id]);
$cache->save($this->uploadControl->getHtmlId(), $cacheFiles);
$cache->save($this->uploadControl->getTokenizedCacheName($token), $cacheFiles);
}
}

/**
* Přejmenuje nahraný soubor.
*/
public function handleRename() {
$id = $this->request->getQuery("id");
$newName = $this->request->getQuery("newName");
$token = $this->request->getQuery("token");

$cache = $this->uploadControl->getCache();
$cacheFiles = $cache->load($this->uploadControl->getTokenizedCacheName($token));
if(isset($cacheFiles[$id])) {
$cacheFiles[$id] = $this->uploadControl->getUploadModel()->rename($cacheFiles[$id], $newName);
$cache->save($this->uploadControl->getTokenizedCacheName($token), $cacheFiles);
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/Template/javascript.latte
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ $(function() {
*/
var productionMode = {$productionMode|escapeJS|noescape};
/**
* Identifikační token.
* @type string
*/
var token = {$token|escapeJS|noescape};
/**
* Třída pro práci s fileuploaderem.
* @type FileUploadController
*/
var controller = new FileUploadController(input, productionMode);
var controller = new FileUploadController(input, productionMode, token);
controller.setDeleteLink({$deleteLink|escapeJS|noescape});
controller.setRenameLink({$renameLink|escapeJS|noescape});
// -----------------------------------------------------------------------
// Control variables
Expand Down Expand Up @@ -95,6 +102,9 @@ $(function() {
data.formData = [{
name: "id",
value: fileId++
}, {
name: "token",
value: token
}];
},
Expand Down
Loading

0 comments on commit 8fe90aa

Please sign in to comment.