Skip to content

Commit

Permalink
Added configurable permission for creating folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Olicek committed Feb 27, 2015
1 parent 7d0df26 commit 6e7471a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extensions:
webimages: DotBlue\WebImages\Extension

webimages:
umask: 0777 # default FALSE
providers:
- DefaultImageProvider(%wwwDir%)
- FakeImageProvider
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;
}
}

}

0 comments on commit 6e7471a

Please sign in to comment.