Skip to content

Commit

Permalink
Fix filesystem path creation for nested dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojta Mareš authored and Milan Felix Šulc committed Nov 8, 2018
1 parent f7fbbfa commit bf98965
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Image.php
Expand Up @@ -38,6 +38,9 @@ public function __construct(bool $friendly_url, string $data_dir, string $data_p
$this->identifier = $identifier;
$this->friendly_url = $friendly_url;

if (stripos($this->identifier, '/') === 0)
$this->identifier = substr($this->identifier, 1);

if ($props === null)
$props = [];

Expand All @@ -48,34 +51,29 @@ public function __construct(bool $friendly_url, string $data_dir, string $data_p
}
}


public function getPath(): Traversable
public function getPath(): string
{
return implode('/', [dirname($this->data_path), $this->createLink()]);
return implode('/', [$this->data_path, $this->identifier]);
}


public function __toString(): string
{
return $this->identifier;
}


public function getQuery(): string
{
return $this->script->toQuery();
}


public function createLink(): Traversable
public function createLink(): string
{
if ($this->friendly_url) {
return implode('/', [$this->data_dir, $this->getScript()->toQuery()]);
}
return implode('/', [$this->data_dir, $this->identifier]);
}


public function getScript(): ImageNameScript
{
return $this->script ?: ImageNameScript::fromIdentifier($this->identifier);
Expand Down
40 changes: 40 additions & 0 deletions tests/cases/ImageTest.php
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Tests\Cases;

use Contributte\ImageStorage\Image;
use Ninjify\Nunjuck\TestCase\BaseTestCase;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

final class ImageTest extends BaseTestCase
{

public function testGetPath(): void
{
$image = new Image(false, '', '/data', '/namespace/47/img.jpg');
Assert::equal('/data/namespace/47/img.jpg', $image->getPath());
}

public function testGetPathNested(): void
{
$image = new Image(false, '', '/data/images', 'namespace/47/img.jpg');
Assert::equal('/data/images/namespace/47/img.jpg', $image->getPath());
}

public function testCreateLink(): void
{
$image = new Image(false, 'data', '', 'namespace/47/img.jpg');
Assert::equal('data/namespace/47/img.jpg', $image->createLink());
}

public function testCreateLinkNested(): void
{
$image = new Image(false, 'data/images', '', 'namespace/47/img.jpg');
Assert::equal('data/images/namespace/47/img.jpg', $image->createLink());
}

}

(new ImageTest())->run();

0 comments on commit bf98965

Please sign in to comment.