Skip to content

Commit

Permalink
Merge pull request #189 from KingCrunch/folder-permissions
Browse files Browse the repository at this point in the history
mkdir() doesn't take care about the umask
  • Loading branch information
havvg committed May 29, 2013
2 parents 6459655 + 30b4519 commit 407c4f7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
39 changes: 32 additions & 7 deletions Imagine/Cache/Resolver/AbstractFilesystemResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ abstract class AbstractFilesystemResolver implements ResolverInterface, CacheMan
*/
protected $cacheManager;

/**
* @var int
*/
protected $folderPermissions = 0777;

/**
* Constructs a filesystem based cache resolver.
*
Expand Down Expand Up @@ -55,6 +60,14 @@ public function setBasePath($basePath)
$this->basePath = $basePath;
}

/**
* @param int $mkdirMode
*/
public function setFolderPermissions ($folderPermissions)
{
$this->folderPermissions = $folderPermissions;
}

/**
* Stores the content into a static file.
*
Expand All @@ -70,13 +83,7 @@ public function store(Response $response, $targetPath, $filter)
{
$dir = pathinfo($targetPath, PATHINFO_DIRNAME);

try {
if (!is_dir($dir)) {
$this->filesystem->mkdir($dir);
}
} catch (IOException $e) {
throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e);
}
$this->makeFolder($dir);

file_put_contents($targetPath, $response->getContent());

Expand All @@ -101,6 +108,24 @@ public function remove($targetPath, $filter)
return !file_exists($filename);
}

/**
* @param string $dir
* @throws \RuntimeException
*/
protected function makeFolder ($dir)
{
if (!is_dir($dir)) {
$parent = dirname($dir);
try {
$this->makeFolder($parent);
$this->filesystem->mkdir($dir);
$this->filesystem->chmod($dir, $this->folderPermissions);
} catch (IOException $e) {
throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e);
}
}
}

/**
* Return the local filepath.
*
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
<call method="setBasePath">
<argument type="string">%liip_imagine.cache.resolver.base_path%</argument>
</call>
<call method="setFolderPermissions">
<argument type="string">%liip_imagine.cache_mkdir_mode%</argument>
</call>
</service>

<service id="liip_imagine.cache.resolver.no_cache" class="%liip_imagine.cache.resolver.no_cache.class%">
Expand Down
23 changes: 23 additions & 0 deletions Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ public function testStoreCyrillicFilename()
$this->assertEquals($data, file_get_contents($targetPath));
}

public function testMkdirVerifyPermissionOnLastLevel () {
if (false !== strpos(strtolower(PHP_OS), 'win')) {
$this->markTestSkipped('mkdir mode is ignored on windows');
}

$resolver = $this->getMockAbstractFilesystemResolver(new Filesystem());


$resolver->store(new Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail');
$this->assertEquals(040777, fileperms($this->tempDir . '/first-level/second-level'));
}

public function testMkdirVerifyPermissionOnFirstLevel () {
if (false !== strpos(strtolower(PHP_OS), 'win')) {
$this->markTestSkipped('mkdir mode is ignored on windows');
}

$resolver = $this->getMockAbstractFilesystemResolver(new Filesystem());

$resolver->store(new Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail');
$this->assertEquals(040777, fileperms($this->tempDir . '/first-level'));
}

public function testStoreInvalidDirectory()
{
if (false !== strpos(strtolower(PHP_OS), 'win')) {
Expand Down

0 comments on commit 407c4f7

Please sign in to comment.