Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mount identifier, for MountManager of FlySystem library, into prefixes #325

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not check for '://' === substr($prefix, -3) instead of having a separate check for :?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you respond to above query please.

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.
*
Expand Down Expand Up @@ -142,7 +176,7 @@ public function getSource()
*/
public function setSourcePathPrefix($sourcePathPrefix)
{
$this->sourcePathPrefix = trim($sourcePathPrefix, '/');
$this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix ?? '');
}

/**
Expand Down Expand Up @@ -254,7 +288,7 @@ public function getCache()
*/
public function setCachePathPrefix($cachePathPrefix)
{
$this->cachePathPrefix = trim($cachePathPrefix, '/');
$this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix ?? '');
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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()
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down