-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePersister.php
More file actions
177 lines (147 loc) 路 6.29 KB
/
Copy pathImagePersister.php
File metadata and controls
177 lines (147 loc) 路 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace SixtyEightPublishers\ImageStorage\Persistence;
use League\Flysystem\FilesystemException as LeagueFilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\FilesystemReader;
use League\Flysystem\StorageAttributes;
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
use SixtyEightPublishers\FileStorage\Exception\FilesystemException;
use SixtyEightPublishers\FileStorage\PathInfoInterface as FilePathInfoInterface;
use SixtyEightPublishers\FileStorage\Resource\ResourceInterface;
use SixtyEightPublishers\ImageStorage\Config\Config;
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
use SixtyEightPublishers\ImageStorage\Resource\ResourceInterface as ImageResourceInterface;
use SixtyEightPublishers\ImageStorage\Resource\TmpFileImageResource;
use function assert;
use function preg_match;
use function preg_quote;
use function sprintf;
final class ImagePersister implements ImagePersisterInterface
{
public function __construct(
private readonly FilesystemOperator $filesystemOperator,
private readonly ConfigInterface $config,
) {}
public function getFilesystem(): FilesystemOperator
{
return $this->filesystemOperator;
}
public function exists(FilePathInfoInterface $pathInfo): bool
{
$pathInfo = $this->assertPathInfo($pathInfo, __METHOD__);
$prefix = null !== $pathInfo->getModifiers() ? self::FILESYSTEM_PREFIX_CACHE : self::FILESYSTEM_PREFIX_SOURCE;
try {
return $this->filesystemOperator->fileExists($prefix . $pathInfo->getPath());
} catch (LeagueFilesystemException $e) {
return false;
}
}
public function save(ResourceInterface $resource, array $config = []): string
{
assert($resource instanceof ImageResourceInterface);
$pathInfo = $this->assertPathInfo($resource->getPathInfo(), __METHOD__);
if (null !== $pathInfo->getModifiers()) {
$resource = $resource->modifyImage($pathInfo->getModifiers(), true);
$prefix = self::FILESYSTEM_PREFIX_CACHE;
} else {
$prefix = self::FILESYSTEM_PREFIX_SOURCE;
}
$path = $pathInfo->getPath();
$flushCache = self::FILESYSTEM_PREFIX_SOURCE === $prefix && $this->exists($pathInfo);
try {
$this->filesystemOperator->write($prefix . $path, $this->encodeImage($resource), $config);
if ($flushCache) {
$this->delete($pathInfo, [
self::OPTION_DELETE_CACHE_ONLY => true,
self::OPTION_SUPPRESS_EXCEPTIONS => true,
]);
}
} catch (LeagueFilesystemException $e) {
if (!($config[self::OPTION_SUPPRESS_EXCEPTIONS] ?? false)) {
throw new FilesystemException($e->getMessage(), $e->getCode(), $e);
}
} finally {
if ($resource instanceof TmpFileImageResource) {
$resource->unlink();
}
}
return $path;
}
public function delete(FilePathInfoInterface $pathInfo, array $config = []): void
{
$pathInfo = $this->assertPathInfo($pathInfo, __METHOD__);
# delete modification only
if (null !== $pathInfo->getModifiers()) {
$this->deleteFile(self::FILESYSTEM_PREFIX_CACHE . $pathInfo->getPath(), $config);
return;
}
# delete all modifications
try {
$contents = $this->filesystemOperator->listContents(self::FILESYSTEM_PREFIX_CACHE . $pathInfo->getNamespace(), FilesystemReader::LIST_DEEP)->toArray();
$regex = sprintf(
'/^%s%s%s[^\/]+\/%s\.[a-zA-Z]+$/',
preg_quote(self::FILESYSTEM_PREFIX_CACHE, '/'),
preg_quote($pathInfo->getNamespace(), '/'),
'' !== $pathInfo->getNamespace() ? '\/' : '',
preg_quote($pathInfo->getName(), '/'),
);
foreach ($contents as $attributes) {
assert($attributes instanceof StorageAttributes);
if ($attributes->isFile() && preg_match($regex, $attributes->path())) {
$this->deleteFile($attributes->path(), $config);
}
}
} catch (LeagueFilesystemException $e) {
if (true !== ($config[self::OPTION_SUPPRESS_EXCEPTIONS] ?? false)) {
throw new FilesystemException($e->getMessage(), $e->getCode(), $e);
}
}
# stop if cache-only option is set
if (true === ($config[self::OPTION_DELETE_CACHE_ONLY] ?? false)) {
return;
}
$this->deleteFile(self::FILESYSTEM_PREFIX_SOURCE . $pathInfo->getPath(), $config);
}
private function encodeImage(ImageResourceInterface $resource): string
{
if (!$resource->hasBeenModified()) {
$contents = @file_get_contents($resource->getLocalFilename());
if (false !== $contents) {
return $contents;
}
}
$quality = (int) ($resource->getEncodeQuality() ?? $this->config[Config::ENCODE_QUALITY] ?? 90);
$format = $resource->getEncodeFormat() ?? '';
$image = $resource->getSource();
$image = $image->encode($format, $quality);
return $image->getEncoded();
}
/**
* @param array<string, mixed> $config
* @throws FilesystemException
*/
private function deleteFile(string $path, array $config): void
{
try {
$this->filesystemOperator->delete($path);
} catch (LeagueFilesystemException $e) {
if (true === ($config[self::OPTION_SUPPRESS_EXCEPTIONS] ?? false)) {
return;
}
throw new FilesystemException($e->getMessage(), $e->getCode(), $e);
}
}
private function assertPathInfo(FilePathInfoInterface $pathInfo, string $method): ImagePathInfoInterface
{
if (!$pathInfo instanceof ImagePathInfoInterface) {
throw new InvalidArgumentException(sprintf(
'Path info passed into the method %s() must be an instance of %s.',
$method,
ImagePathInfoInterface::class,
));
}
return $pathInfo;
}
}