Skip to content
Draft
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ parameters:
path: src/ImageStorage.php

-
message: "#^Parameter \\#5 \\$props of class Contributte\\\\ImageStorage\\\\Image constructor expects array\\<bool\\|Contributte\\\\ImageStorage\\\\ImageNameScript\\|string\\|null\\>, array\\<string, mixed\\> given\\.$#"
message: "#^Parameter \\#6 \\$props of class Contributte\\\\ImageStorage\\\\Image constructor expects array\\<bool\\|Contributte\\\\ImageStorage\\\\ImageNameScript\\|string\\|null\\>, array\\<string, mixed\\> given\\.$#"
count: 2
path: src/ImageStorage.php

Expand Down
6 changes: 5 additions & 1 deletion src/DI/ImageStorageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Contributte\ImageStorage\Latte\LatteExtension;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\FactoryDefinition;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use stdClass;
Expand Down Expand Up @@ -37,10 +38,13 @@ public function loadConfiguration(): void
$config = $this->getConfig();
$config->orig_path ??= $config->data_path;

$arguments = (array) $config;
$arguments['httpRequest'] = new Statement('@Nette\Http\IRequest');

$builder->addDefinition($this->prefix('storage'))
->setType(ImageStorage::class)
->setFactory(ImageStorage::class)
->setArguments((array) $config);
->setArguments($arguments);
}

public function beforeCompile(): void
Expand Down
15 changes: 12 additions & 3 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class Image

private bool $friendly_url = false;

private string $basePath = '';

/**
* @param bool[]|string[]|ImageNameScript[]|null[] $props
*/
public function __construct(bool $friendly_url, string $data_dir, string $data_path, string $identifier, array $props = [])
public function __construct(bool $friendly_url, string $data_dir, string $data_path, string $identifier, string $basePath = '', array $props = [])
{
$this->data_dir = $data_dir;
$this->data_path = $data_path;
$this->identifier = $identifier;
$this->friendly_url = $friendly_url;
$this->basePath = $basePath;

if (stripos($this->identifier, '/') === 0) {
$this->identifier = substr($this->identifier, 1);
Expand Down Expand Up @@ -67,11 +70,17 @@ public function getQuery(): string

public function createLink(): string
{
$parts = $this->basePath !== '' ? [$this->basePath] : [];

if ($this->friendly_url) {
return implode('/', [$this->data_dir, $this->getScript()->toQuery()]);
$parts[] = $this->data_dir;
$parts[] = $this->getScript()->toQuery();
} else {
$parts[] = $this->data_dir;
$parts[] = $this->identifier;
}

return implode('/', [$this->data_dir, $this->identifier]);
return implode('/', $parts);
}

public function getScript(): ImageNameScript
Expand Down
28 changes: 21 additions & 7 deletions src/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Contributte\ImageStorage\Exception\ImageStorageException;
use DirectoryIterator;
use Nette\Http\FileUpload;
use Nette\Http\IRequest;
use Nette\SmartObject;
use Nette\Utils\FileSystem;
use Nette\Utils\Image as NetteImage;
Expand Down Expand Up @@ -38,6 +39,8 @@ class ImageStorage

private bool $friendly_url;

private ?IRequest $httpRequest;

private int $mask = 0775;

/** @var int[] */
Expand All @@ -62,7 +65,8 @@ public function __construct(
int $quality,
string $default_transform,
string $noimage_identifier,
bool $friendly_url
bool $friendly_url,
?IRequest $httpRequest = null
)
{
$this->data_path = $data_path;
Expand All @@ -74,6 +78,7 @@ public function __construct(
$this->default_transform = $default_transform;
$this->noimage_identifier = $noimage_identifier;
$this->friendly_url = $friendly_url;
$this->httpRequest = $httpRequest;
}

public function delete(mixed $arg, bool $onlyChangedImages = false): void
Expand Down Expand Up @@ -125,7 +130,7 @@ public function saveUpload(FileUpload $upload, string $namespace, ?string $check

$upload->move($path);

return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, [
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, $this->getBasePath(), [
'sha' => $checksum,
'name' => self::fixName($upload->getUntrustedName()),
]);
Expand All @@ -145,7 +150,7 @@ public function saveContent(mixed $content, string $name, string $namespace, ?st

file_put_contents($path, $content, LOCK_EX);

return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, [
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, $this->getBasePath(), [
'sha' => $checksum,
'name' => self::fixName($name),
]);
Expand Down Expand Up @@ -175,7 +180,7 @@ public function fromIdentifier(mixed $args): Image
@copy($orig_file, $data_file);
}

return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier);
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, $this->getBasePath());
}

preg_match('/(\d+)?x(\d+)?(crop(\d+)x(\d+)x(\d+)x(\d+))?/', $args[1], $matches);
Expand Down Expand Up @@ -248,7 +253,7 @@ public function fromIdentifier(mixed $args): Image
);
}

return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, ['script' => $script]);
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, $this->getBasePath(), ['script' => $script]);
}

/**
Expand Down Expand Up @@ -282,7 +287,7 @@ public function getNoImage(bool $return_image = false): Image|array
}

if ($return_image) {
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier);
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, $this->getBasePath());
}

$script = ImageNameScript::fromIdentifier($identifier);
Expand All @@ -291,7 +296,7 @@ public function getNoImage(bool $return_image = false): Image|array
}

if ($return_image) {
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $this->noimage_identifier);
return new Image($this->friendly_url, $this->data_dir, $this->data_path, $this->noimage_identifier, $this->getBasePath());
}

return [$script, $file];
Expand All @@ -302,6 +307,15 @@ public function setFriendlyUrl(bool $friendly_url = true): void
$this->friendly_url = $friendly_url;
}

public function getBasePath(): string
{
if ($this->httpRequest === null) {
return '';
}

return rtrim($this->httpRequest->getUrl()->getBasePath(), '/');
}

private static function fixName(string $name): string
{
return Strings::webalize($name, '._');
Expand Down
6 changes: 3 additions & 3 deletions src/Latte/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function tagImg(Tag $tag): Node
$args = $tag->parser->parseArguments();

return new AuxiliaryNode(
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "<img src=\"" . $basePath . "/" . $_img->createLink() . "\">";', $args)
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "<img src=\"" . $_img->createLink() . "\">";', $args)
);
}

Expand All @@ -50,7 +50,7 @@ public function attrImg(Tag $tag): Node
$args = $tag->parser->parseArguments();

return new AuxiliaryNode(
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $basePath . "/" . $_img->createLink() . \'"\';', $args)
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $_img->createLink() . \'"\';', $args)
);
}

Expand All @@ -68,7 +68,7 @@ public function linkImg(Tag $tag): Node
$args = $tag->parser->parseArguments();

return new AuxiliaryNode(
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $basePath . "/" . $_img->createLink();', $args)
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $_img->createLink();', $args)
);
}

Expand Down
Loading