diff --git a/src/Server.php b/src/Server.php index 4c8705c..d22a358 100644 --- a/src/Server.php +++ b/src/Server.php @@ -93,6 +93,30 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } + /** + * Trim path separator from prefix to prevent multiple combined separator. + */ + private function trimPrefixPathSeparator(string $prefix): string + { + if (str_ends_with($prefix, '://')) { + return rtrim($prefix, '/').'/'; + } + + return trim($prefix, '/'); + } + + /** + * Remove filesystem identifier if present. + */ + private function removeFilesystemIdentifier(string $path): string + { + if (false === ($identifierPos = strpos($path, '://'))) { + return $path; + } + + return substr($path, $identifierPos + 3); + } + /** * Set source file system. * @@ -120,7 +144,7 @@ public function getSource(): FilesystemOperator */ public function setSourcePathPrefix(string $sourcePathPrefix): void { - $this->sourcePathPrefix = trim($sourcePathPrefix, '/'); + $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix); } /** @@ -228,7 +252,7 @@ public function getCache(): FilesystemOperator */ public function setCachePathPrefix(string $cachePathPrefix): void { - $this->cachePathPrefix = trim($cachePathPrefix, '/'); + $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix); } /** @@ -357,12 +381,16 @@ public function getCachePath(string $path, array $params = []): string $cachedPath = hash('xxh3', $sourcePath.'?'.http_build_query($params)); + if (false !== ($identifierPos = strpos($sourcePath, '://'))) { + $sourcePath = substr($sourcePath, $identifierPos + 3); + } + if ($this->groupCacheInFolders) { $cachedPath = $sourcePath.'/'.$cachedPath; } if ($this->cachePathPrefix) { - $cachedPath = $this->cachePathPrefix.'/'.$cachedPath; + $cachedPath = $this->cachePathPrefix.'/'.$this->removeFilesystemIdentifier($cachedPath); } if ($this->cacheWithFileExtensions) { diff --git a/tests/ServerTest.php b/tests/ServerTest.php index b7aed8c..b798625 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -59,6 +59,12 @@ public function testSetSourcePathPrefix(): void $this->assertEquals('img', $this->server->getSourcePathPrefix()); } + public function testSetSourcePathPrefixWithMonthIdentifier(): void + { + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); + } + public function testGetSourcePathPrefix(): void { $this->assertEquals('', $this->server->getSourcePathPrefix()); @@ -83,6 +89,22 @@ public function testGetSourcePathWithPrefix(): void { $this->server->setSourcePathPrefix('img/'); $this->assertEquals('img/image.jpg', $this->server->getSourcePath('image.jpg')); + + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img://image.jpg', $this->server->getSourcePath('image.jpg')); + $this->assertEquals('img://path/image.jpg', $this->server->getSourcePath('/path/image.jpg')); + } + + public function testGetSourcePathWithBaseUrlAndPrefix() + { + $this->server->setBaseUrl('base/'); + + $this->server->setSourcePathPrefix('img/'); + $this->assertEquals('img/image.jpg', $this->server->getSourcePath('/base/image.jpg')); + + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img://image.jpg', $this->server->getSourcePath('base/image.jpg')); + $this->assertEquals('img://path/image.jpg', $this->server->getSourcePath('/base/path/image.jpg')); } public function testGetSourcePathWithMissingPath(): void @@ -135,6 +157,12 @@ public function testSetCachePathPrefix(): void $this->assertEquals('img', $this->server->getCachePathPrefix()); } + public function testSetCachePathPrefixWithMonthIdentifier(): void + { + $this->server->setCachePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getCachePathPrefix()); + } + public function testGetCachePathPrefix(): void { $this->assertEquals('', $this->server->getCachePathPrefix()); @@ -316,6 +344,17 @@ public function testGetCachePathWithExtensionAndPjpgFmFromPreset(): void $this->assertEquals('image.jpg/58b79a7735b61b0d.jpg', $this->server->getCachePath('image.jpg', ['p' => 'pjpg'])); } + public function testGetCachePathWithMount(): void + { + $this->assertEquals('image.jpg/7116fbe362f62057', $this->server->getCachePath('file://image.jpg', [])); + } + + public function testGetCachePathWithMountAndCachePrefix(): void + { + $this->server->setCachePathPrefix('cache://'); + $this->assertEquals('cache://image.jpg/7116fbe362f62057', $this->server->getCachePath('file://image.jpg', [])); + } + public function testCacheFileExists(): void { $this->server->setCache(\Mockery::mock(FilesystemOperator::class, function ($mock) {