Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ webimages:

> By default all these routes will be prepended before your other routes - assuming you use `Nette\Application\Routers\RouteList` as your root router. You can disable this by setting `prependRoutesToRouter: false`. Then it's your responsibility to plug webimages router (service `webimages.router`) to your routing implementation.

If you need custom permission for creating folders, you can set the permission in config:

```
webimages:
umask: 0777
```

> Custom permission implementation uses umask

Addon gives you new macro `n:src`. Now you're ready to use it.

```html
Expand Down
2 changes: 2 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Extension extends DI\CompilerExtension

/** @var array */
private $defaults = [
'umask' => FALSE,
'routes' => [],
'prependRoutesToRouter' => TRUE,
'rules' => [],
Expand All @@ -35,6 +36,7 @@ public function loadConfiguration()
$generator = $container->addDefinition($this->prefix('generator'))
->setClass('DotBlue\WebImages\Generator', [
$config['wwwDir'],
$config['umask'],
]);

foreach ($config['rules'] as $rule) {
Expand Down
28 changes: 23 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ class Generator extends Nette\Object

/** @var IProvider[] */
private $providers = [];

/** @var integer */
private $umask;



public function __construct($wwwDir, Http\IRequest $httpRequest, Http\IResponse $httpResponse, Validator $validator)
public function __construct($wwwDir, $umask, Http\IRequest $httpRequest, Http\IResponse $httpResponse, Validator $validator)
{
$this->wwwDir = $wwwDir;
$this->umask = $umask;
$this->httpRequest = $httpRequest;
$this->httpResponse = $httpResponse;
$this->validator = $validator;
Expand Down Expand Up @@ -91,10 +95,7 @@ public function generateImage(ImageRequest $request)

$dirname = dirname($destination);
if (!is_dir($dirname)) {
$success = @mkdir($dirname, 0777, TRUE);
if (!$success) {
throw new Application\BadRequestException;
}
$this->createFolder($dirname);
}

$success = $image->save($destination, 90, $format);
Expand All @@ -105,5 +106,22 @@ public function generateImage(ImageRequest $request)
$image->send();
exit;
}



private function createFolder($dirname)
{
if ($this->umask) {
$oldmask = umask(0);
$success = @mkdir($dirname, octdec($this->umask), TRUE);
umask($oldmask);
} else {
$success = @mkdir($dirname, 0777, TRUE);
}

if (!$success) {
throw new Application\BadRequestException;
}
}

}