Skip to content

Commit

Permalink
Use webmozart/path-util to generate URLs, closes #11, closes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Aug 28, 2016
1 parent 78bb6e2 commit eefc8c2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.5.0",
"contao/imagine-svg": "^0.1",
"imagine/imagine": "^0.6",
"symfony/filesystem": "~2.8|~3.0"
"symfony/filesystem": "~2.8|~3.0",
"webmozart/path-util": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~1.8",
Expand Down
12 changes: 5 additions & 7 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

/**
* Image class.
Expand Down Expand Up @@ -94,15 +95,12 @@ public function getPath()
*/
public function getUrl($rootDir)
{
if (
substr($this->path, 0, strlen($rootDir) + 1) === $rootDir.'/'
|| substr($this->path, 0, strlen($rootDir) + 1) === $rootDir.'\\'
) {
$url = substr($this->path, strlen($rootDir) + 1);
} else {
throw new \InvalidArgumentException('Path "'.$this->path.'" is not inside root directory "'.$rootDir.'"');
if (!Path::isBasePath($rootDir, $this->path)) {
throw new \InvalidArgumentException(sprintf('Path "%s" is not inside root directory "%s"', $this->path, $rootDir));
}

$url = Path::makeRelative($this->path, $rootDir);

$url = str_replace('%2F', '/', rawurlencode($url));

return $url;
Expand Down
62 changes: 57 additions & 5 deletions tests/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,70 @@ public function testGetPath()
*/
public function testGetUrl()
{
$image = $this->createImage('/path/to/a/filename with special&<>"\'chars.jpeg');
$image = $this->createImage('C:\path\to\a\filename with special&<>"\'%2Fchars.jpeg');

$this->assertEquals('path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg', $image->getUrl(''));
$this->assertEquals('to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg', $image->getUrl('/path'));
$this->assertEquals('a/filename%20with%20special%26%3C%3E%22%27chars.jpeg', $image->getUrl('/path/to'));
$this->assertEquals('filename%20with%20special%26%3C%3E%22%27chars.jpeg', $image->getUrl('/path/to/a'));
$this->assertEquals('path/to/a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('C:/'));
$this->assertEquals('to/a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('C:/path'));
$this->assertEquals('a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('C:/path/to'));
$this->assertEquals('filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('C:/path/to/a'));

$image = $this->createImage('/path/to/a/filename with special&<>"\'%2Fchars.jpeg');

$this->assertEquals('path/to/a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('/'));
$this->assertEquals('to/a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('/path'));
$this->assertEquals('a/filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('/path/to'));
$this->assertEquals('filename%20with%20special%26%3C%3E%22%27%252Fchars.jpeg', $image->getUrl('/path/to/a'));

$this->setExpectedException('InvalidArgumentException');

$image->getUrl('/path/t');
}

/**
* Tests the getUrl() method with relative path components.
*/
public function testGetUrlRealtivePath()
{
foreach ([
'/path/to/a/file.png',
'/path/to/a/subdir/../file.png',
'/path/subdir/../to/a/file.png',
] as $imagePath) {
$image = $this->createImage($imagePath);

$this->assertEquals('path/to/a/file.png', $image->getUrl('/'));
$this->assertEquals('to/a/file.png', $image->getUrl('/path'));
$this->assertEquals('file.png', $image->getUrl('/path/to/a'));
$this->assertEquals('file.png', $image->getUrl('/path/to/a/subdir/..'));
$this->assertEquals('file.png', $image->getUrl('/path/to/subdir/../a'));
$this->assertEquals('file.png', $image->getUrl('/path/subdir/../to/a'));
}

foreach ([
'C:\path\to\a\file.png',
'C:\path\to\a/subdir\..\file.png',
'C:\path/subdir\..\to\a/file.png',
] as $imagePath) {
$image = $this->createImage($imagePath);

$this->assertEquals('path/to/a/file.png', $image->getUrl('C:\\'));
$this->assertEquals('to/a/file.png', $image->getUrl('C:\path'));
$this->assertEquals('to/a/file.png', $image->getUrl('C:/path'));
$this->assertEquals('file.png', $image->getUrl('C:\path\to\a'));
$this->assertEquals('file.png', $image->getUrl('C:\path/to/a'));
$this->assertEquals('file.png', $image->getUrl('C:\path\to\a\subdir\..'));
$this->assertEquals('file.png', $image->getUrl('C:\path\to\subdir\..\a'));
$this->assertEquals('file.png', $image->getUrl('C:\path\subdir\..\to\a'));
$this->assertEquals('file.png', $image->getUrl('C:\path\subdir/../to\a'));
}

$image = $this->createImage('C:\path/subdir\..\to\a/file.png');

$this->setExpectedException('InvalidArgumentException');

$image->getUrl('C:\path/subdir');
}

/**
* Tests the getDimensions() method.
*/
Expand Down
10 changes: 5 additions & 5 deletions tests/PictureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testGetImg()

$this->assertEquals(
'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg',
$picture->getImg('')['src']
$picture->getImg('/')['src']
);

$this->assertEquals(
Expand All @@ -65,7 +65,7 @@ public function testGetImg()

$this->assertEquals(
'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg 1x',
$picture->getImg('')['srcset']
$picture->getImg('/')['srcset']
);

$this->assertEquals(
Expand All @@ -84,7 +84,7 @@ public function testGetImg()
);

$this->assertEquals('custom attribute', $picture->getImg()['data-custom']);
$this->assertEquals('custom attribute', $picture->getImg('')['data-custom']);
$this->assertEquals('custom attribute', $picture->getImg('/')['data-custom']);

$this->setExpectedException('InvalidArgumentException');

Expand All @@ -102,7 +102,7 @@ public function testGetSources()

$this->assertEquals(
'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg 1x',
$picture->getSources('')[0]['srcset']
$picture->getSources('/')[0]['srcset']
);

$this->assertEquals(
Expand All @@ -121,7 +121,7 @@ public function testGetSources()
);

$this->assertEquals('custom attribute', $picture->getSources()[0]['data-custom']);
$this->assertEquals('custom attribute', $picture->getSources('')[0]['data-custom']);
$this->assertEquals('custom attribute', $picture->getSources('/')[0]['data-custom']);

$this->setExpectedException('InvalidArgumentException');

Expand Down

0 comments on commit eefc8c2

Please sign in to comment.