Skip to content
Merged
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
34 changes: 31 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -120,7 +144,7 @@ public function getSource(): FilesystemOperator
*/
public function setSourcePathPrefix(string $sourcePathPrefix): void
{
$this->sourcePathPrefix = trim($sourcePathPrefix, '/');
$this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix);
}

/**
Expand Down Expand Up @@ -228,7 +252,7 @@ public function getCache(): FilesystemOperator
*/
public function setCachePathPrefix(string $cachePathPrefix): void
{
$this->cachePathPrefix = trim($cachePathPrefix, '/');
$this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix);
}

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