diff --git a/src/Server.php b/src/Server.php index a32a9ca4..06814eb1 100644 --- a/src/Server.php +++ b/src/Server.php @@ -111,6 +111,40 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } + /** + * Trim path separator from prefix to prevent multiple combined separator. + * + * @param string $prefix + * + * @return string + */ + private function trimPrefixPathSeparator(string $prefix): string + { + if ('//' == substr($prefix, -2)) { + if (':' == substr(rtrim($prefix, '/'), -1)) { + return rtrim($prefix, '/').'/'; + } + } + + return trim($prefix, '/'); + } + + /** + * Remove filesystem identifier if present. + * + * @param string $path + * + * @return string + */ + private function removeFilesystemIdentifier(string $path): string + { + if (false === strpos($path, '://')) { + return $path; + } + + return explode('://', $path, 2)[1] ?? ''; + } + /** * Set source file system. * @@ -142,7 +176,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = trim($sourcePathPrefix, '/'); + $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix ?? ''); } /** @@ -254,7 +288,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = trim($cachePathPrefix, '/'); + $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix ?? ''); } /** @@ -361,10 +395,13 @@ public function getCachePath($path, array $params = []) $md5 = md5($sourcePath.'?'.http_build_query($params)); + if (false !== strpos($sourcePath, '://')) { + $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; + } $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; 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 3048ba6e..28956eff 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -61,6 +61,15 @@ public function testSetSourcePathPrefix() $this->assertEquals('img', $this->server->getSourcePathPrefix()); } + public function testSetSourcePathPrefixWithMonthIdentifier() + { + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); + + $this->server->setSourcePathPrefix('img:///'); + $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); + } + public function testGetSourcePathPrefix() { $this->assertEquals('', $this->server->getSourcePathPrefix()); @@ -85,6 +94,22 @@ public function testGetSourcePathWithPrefix() { $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() @@ -137,6 +162,15 @@ public function testSetCachePathPrefix() $this->assertEquals('img', $this->server->getCachePathPrefix()); } + public function testSetCachePathPrefixWithMonthIdentifier() + { + $this->server->setCachePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getCachePathPrefix()); + + $this->server->setCachePathPrefix('img:///'); + $this->assertEquals('img:/', $this->server->getCachePathPrefix()); + } + public function testGetCachePathPrefix() { $this->assertEquals('', $this->server->getCachePathPrefix()); @@ -262,6 +296,17 @@ public function testGetCachePathWithExtensionAndPjpgFmFromPreset() $this->assertEquals('image.jpg/ce5cb75f4a37dec0a0a49854e94123eb.jpg', $this->server->getCachePath('image.jpg', ['p' => 'pjpg'])); } + public function testGetCachePathWithMount() + { + $this->assertEquals('image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + } + + public function testGetCachePathWithMountAndCachePrefix() + { + $this->server->setCachePathPrefix('cache://'); + $this->assertEquals('cache://image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + } + public function testCacheFileExists() { $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) {