From bf9896527773e391209e5a5a7cb8c1fb5bfe6063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojta=20Mare=C5=A1?= Date: Thu, 8 Nov 2018 20:41:42 +0100 Subject: [PATCH] Fix filesystem path creation for nested dirs --- src/Image.php | 14 ++++++-------- tests/cases/ImageTest.php | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/cases/ImageTest.php diff --git a/src/Image.php b/src/Image.php index 6cf3815..81b939a 100644 --- a/src/Image.php +++ b/src/Image.php @@ -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 = []; @@ -48,26 +51,22 @@ 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()]); @@ -75,7 +74,6 @@ public function createLink(): Traversable return implode('/', [$this->data_dir, $this->identifier]); } - public function getScript(): ImageNameScript { return $this->script ?: ImageNameScript::fromIdentifier($this->identifier); diff --git a/tests/cases/ImageTest.php b/tests/cases/ImageTest.php new file mode 100644 index 0000000..921d540 --- /dev/null +++ b/tests/cases/ImageTest.php @@ -0,0 +1,40 @@ +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();