From ba3e4feaa172540740e8c489ceb23e4153e400c0 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 10 Jun 2021 14:40:32 +0200 Subject: [PATCH 1/4] Allow mount identifier, for MountManager of FlySystem library, into prefixes --- src/Server.php | 15 +++++++++++++-- tests/ServerTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index a32a9ca4..b3a6534d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -111,6 +111,17 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } + private function trimPrefix(string $prefix): string + { + if ('//' == substr($prefix, -2)) { + if (':' == substr(rtrim($prefix, '/'), -1)) { + return rtrim($prefix, '/') . '/'; + } + } + + return trim($prefix, '/'); + } + /** * Set source file system. * @@ -142,7 +153,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = trim($sourcePathPrefix, '/'); + $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix); } /** @@ -254,7 +265,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = trim($cachePathPrefix, '/'); + $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix); } /** diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 3048ba6e..6c16d453 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()); From 5ff3000f131f880ac4ca9adbcb822be131be8d5f Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Tue, 30 Nov 2021 17:56:50 +0100 Subject: [PATCH 2/4] Allow mount identifier into path without set prefix --- src/Server.php | 18 +++++++++++++++--- tests/ServerTest.php | 11 +++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index b3a6534d..db47a977 100644 --- a/src/Server.php +++ b/src/Server.php @@ -122,6 +122,15 @@ private function trimPrefix(string $prefix): string return trim($prefix, '/'); } + private function purgePath(string $path): string + { + if (strpos($path, '://') === false) { + return $path; + } + + return explode('://', $path, 2)[1] ?? ''; + } + /** * Set source file system. * @@ -153,7 +162,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix); + $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix ?? ''); } /** @@ -265,7 +274,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix); + $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix ?? ''); } /** @@ -372,10 +381,13 @@ public function getCachePath($path, array $params = []) $md5 = md5($sourcePath.'?'.http_build_query($params)); + if (strpos($sourcePath, '://') !== false) { + $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; + } $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; if ($this->cachePathPrefix) { - $cachedPath = $this->cachePathPrefix.'/'.$cachedPath; + $cachedPath = $this->cachePathPrefix.'/'.$this->purgePath($cachedPath); } if ($this->cacheWithFileExtensions) { diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 6c16d453..28956eff 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -296,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) { From 7babfbb33db2d39d22213fa225ac96386123e1e1 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Tue, 30 Nov 2021 18:04:40 +0100 Subject: [PATCH 3/4] COde style --- src/Server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index db47a977..bdb42cd6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -115,7 +115,7 @@ private function trimPrefix(string $prefix): string { if ('//' == substr($prefix, -2)) { if (':' == substr(rtrim($prefix, '/'), -1)) { - return rtrim($prefix, '/') . '/'; + return rtrim($prefix, '/').'/'; } } @@ -124,7 +124,7 @@ private function trimPrefix(string $prefix): string private function purgePath(string $path): string { - if (strpos($path, '://') === false) { + if (false === strpos($path, '://')) { return $path; } @@ -381,7 +381,7 @@ public function getCachePath($path, array $params = []) $md5 = md5($sourcePath.'?'.http_build_query($params)); - if (strpos($sourcePath, '://') !== false) { + if (false !== strpos($sourcePath, '://')) { $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; } $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; From 86ad092e2aa215450818858781d6fa12b4ec5960 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Mon, 29 Aug 2022 13:09:37 +0200 Subject: [PATCH 4/4] Improve methods names & PhpDoc --- src/Server.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Server.php b/src/Server.php index bdb42cd6..06814eb1 100644 --- a/src/Server.php +++ b/src/Server.php @@ -111,7 +111,14 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } - private function trimPrefix(string $prefix): string + /** + * 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)) { @@ -122,7 +129,14 @@ private function trimPrefix(string $prefix): string return trim($prefix, '/'); } - private function purgePath(string $path): string + /** + * Remove filesystem identifier if present. + * + * @param string $path + * + * @return string + */ + private function removeFilesystemIdentifier(string $path): string { if (false === strpos($path, '://')) { return $path; @@ -162,7 +176,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix ?? ''); + $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix ?? ''); } /** @@ -274,7 +288,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix ?? ''); + $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix ?? ''); } /** @@ -387,7 +401,7 @@ public function getCachePath($path, array $params = []) $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; if ($this->cachePathPrefix) { - $cachedPath = $this->cachePathPrefix.'/'.$this->purgePath($cachedPath); + $cachedPath = $this->cachePathPrefix.'/'.$this->removeFilesystemIdentifier($cachedPath); } if ($this->cacheWithFileExtensions) {