Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\IO\IOMetadataHandler;
8
9 use DateTime;
10 use eZ\Publish\Core\IO\IOMetadataHandler;
11 use Doctrine\DBAL\Connection;
12 use Doctrine\DBAL\DBALException;
13 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
14 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
15 use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
16 use eZ\Publish\Core\IO\Exception\InvalidBinaryFileIdException;
17 use eZ\Publish\Core\IO\UrlDecorator;
18 use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
19 use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
20 use RuntimeException;
21
27 class LegacyDFSCluster implements IOMetadataHandler
28 {
30 private $db;
31
33 private $urlDecorator;
34
39 public function __construct(Connection $connection, UrlDecorator $urlDecorator = null)
40 {
41 $this->db = $connection;
42 $this->urlDecorator = $urlDecorator;
43 }
44
57 public function create(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
58 {
59 if (!($binaryFileCreateStruct->mtime instanceof DateTime)) {
60 throw new InvalidArgumentException('$binaryFileCreateStruct', 'Property \'mtime\' must be a DateTime');
61 }
62
63 $path = $this->addPrefix($binaryFileCreateStruct->id);
64
65 try {
66 /*
67 * @todo what might go wrong here ? Can another process be trying to insert the same image ?
68 * what happens if somebody did ?
69 **/
70 $stmt = $this->db->prepare(<<<SQL
71 INSERT INTO ezdfsfile
72 (name, name_hash, name_trunk, mtime, size, scope, datatype)
73 VALUES (:name, :name_hash, :name_trunk, :mtime, :size, :scope, :datatype)
74 ON DUPLICATE KEY UPDATE
75 datatype=VALUES(datatype), scope=VALUES(scope), size=VALUES(size),
76 mtime=VALUES(mtime)
77 SQL
78 );
79 $stmt->bindValue('name', $path);
80 $stmt->bindValue('name_hash', md5($path));
81 $stmt->bindValue('name_trunk', $this->getNameTrunk($binaryFileCreateStruct));
82 $stmt->bindValue('mtime', $binaryFileCreateStruct->mtime->getTimestamp());
83 $stmt->bindValue('size', $binaryFileCreateStruct->size);
84 $stmt->bindValue('scope', $this->getScope($binaryFileCreateStruct));
85 $stmt->bindValue('datatype', $binaryFileCreateStruct->mimeType);
86 $stmt->execute();
87 } catch (DBALException $e) {
88 throw new RuntimeException("A DBAL error occured while writing $path", 0, $e);
89 }
90
91 return $this->mapSPIBinaryFileCreateStructToSPIBinaryFile($binaryFileCreateStruct);
92 }
93
101 public function delete($spiBinaryFileId)
102 {
103 $path = $this->addPrefix($spiBinaryFileId);
104
105 // Unlike the legacy cluster, the file is directly deleted. It was inherited from the DB cluster anyway
106 $stmt = $this->db->prepare('DELETE FROM ezdfsfile WHERE name_hash LIKE :name_hash');
107 $stmt->bindValue('name_hash', md5($path));
108 $stmt->execute();
109
110 if ($stmt->rowCount() != 1) {
111 // Is this really necessary ?
112 throw new BinaryFileNotFoundException($path);
113 }
114 }
115
126 public function load($spiBinaryFileId)
127 {
128 $path = $this->addPrefix($spiBinaryFileId);
129
130 $stmt = $this->db->prepare('SELECT * FROM ezdfsfile WHERE name_hash LIKE ? AND expired != 1 AND mtime > 0');
131 $stmt->bindValue(1, md5($path));
132 $stmt->execute();
133
134 if ($stmt->rowCount() == 0) {
135 throw new BinaryFileNotFoundException($path);
136 }
137
138 $row = $stmt->fetch(\PDO::FETCH_ASSOC) + ['id' => $spiBinaryFileId];
139
140 return $this->mapArrayToSPIBinaryFile($row);
141 }
142
153 public function exists($spiBinaryFileId)
154 {
155 $path = $this->addPrefix($spiBinaryFileId);
156
157 $stmt = $this->db->prepare('SELECT name FROM ezdfsfile WHERE name_hash LIKE ? and mtime > 0 and expired != 1');
158 $stmt->bindValue(1, md5($path));
159 $stmt->execute();
160
161 return $stmt->rowCount() == 1;
162 }
163
169 protected function getNameTrunk(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
170 {
171 return $this->addPrefix($binaryFileCreateStruct->id);
172 }
173
184 protected function getScope(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
185 {
186 list($filePrefix) = explode('/', $binaryFileCreateStruct->id);
187
188 switch ($filePrefix) {
189 case 'images':
190 return 'image';
191
192 case 'original':
193 return 'binaryfile';
194 }
195
196 return 'UNKNOWN_SCOPE';
197 }
198
206 protected function addPrefix($id)
207 {
208 return isset($this->urlDecorator) ? $this->urlDecorator->decorate($id) : $id;
209 }
210
220 protected function removePrefix($prefixedId)
221 {
222 return isset($this->urlDecorator) ? $this->urlDecorator->undecorate($prefixedId) : $prefixedId;
223 }
224
225 public function getMimeType($spiBinaryFileId)
226 {
227 $stmt = $this->db->prepare('SELECT * FROM ezdfsfile WHERE name_hash LIKE ? AND expired != 1 AND mtime > 0');
228 $stmt->bindValue(1, md5($this->addPrefix($spiBinaryFileId)));
229 $stmt->execute();
230
231 if ($stmt->rowCount() == 0) {
232 throw new BinaryFileNotFoundException($spiBinaryFileId);
233 }
234
235 $row = $stmt->fetch(\PDO::FETCH_ASSOC);
236
237 return $row['datatype'];
238 }
239
245 public function deleteDirectory($spiPath)
246 {
247 $query = $this->db->createQueryBuilder();
248 $query
249 ->delete('ezdfsfile')
250 ->where('name LIKE :spiPath ESCAPE :esc')
251 ->setParameter(':esc', '\\')
252 ->setParameter(
253 ':spiPath',
254 addcslashes($this->addPrefix(rtrim($spiPath, '/')), '%_') . '/%'
255 );
256 $query->execute();
257 }
258
266 protected function mapArrayToSPIBinaryFile(array $properties)
267 {
268 $spiBinaryFile = new SPIBinaryFile();
269 $spiBinaryFile->id = $properties['id'];
270 $spiBinaryFile->size = $properties['size'];
271 $spiBinaryFile->mtime = new DateTime('@' . $properties['mtime']);
272 $spiBinaryFile->mimeType = $properties['datatype'];
273
274 return $spiBinaryFile;
275 }
276
282 protected function mapSPIBinaryFileCreateStructToSPIBinaryFile(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
283 {
284 $spiBinaryFile = new SPIBinaryFile();
285 $spiBinaryFile->id = $binaryFileCreateStruct->id;
286 $spiBinaryFile->mtime = $binaryFileCreateStruct->mtime;
287 $spiBinaryFile->size = $binaryFileCreateStruct->size;
288 $spiBinaryFile->mimeType = $binaryFileCreateStruct->mimeType;
289
290 return $spiBinaryFile;
291 }
292 }
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
Definition: BinaryFileNotFoundException.php:13
eZ\Publish\Core\IO\Exception\InvalidBinaryFileIdException
Definition: InvalidBinaryFileIdException.php:12
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster
Manages IO metadata in a mysql table, ezdfsfile.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:28
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\create
create(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
Inserts a new reference to file $spiBinaryFileId.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:57
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\mapArrayToSPIBinaryFile
mapArrayToSPIBinaryFile(array $properties)
Maps an array of data base properties (id, size, mtime, datatype, md5_path, path.....
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:266
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\removePrefix
removePrefix($prefixedId)
Removes the internal prefix string from $prefixedId.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:220
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\mapSPIBinaryFileCreateStructToSPIBinaryFile
mapSPIBinaryFileCreateStructToSPIBinaryFile(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:282
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\getMimeType
getMimeType($spiBinaryFileId)
Returns the file's mimetype.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:225
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\exists
exists($spiBinaryFileId)
Checks if a file $spiBinaryFileId exists.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:153
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\addPrefix
addPrefix($id)
Adds the internal prefix string to $id.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:206
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\$urlDecorator
$urlDecorator
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:33
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\$db
$db
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:30
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\getNameTrunk
getNameTrunk(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:169
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\deleteDirectory
deleteDirectory($spiPath)
Delete directory and all the content under specified directory.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:245
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\__construct
__construct(Connection $connection, UrlDecorator $urlDecorator=null)
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:39
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\load
load($spiBinaryFileId)
Loads and returns metadata for $spiBinaryFileId.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:126
eZ\Publish\Core\IO\IOMetadataHandler\LegacyDFSCluster\getScope
getScope(SPIBinaryFileCreateStruct $binaryFileCreateStruct)
Returns the value for the scope meta field, based on the created file's path.
Definition: Publish/Core/IO/IOMetadataHandler/LegacyDFSCluster.php:184
eZ\Publish\SPI\IO\BinaryFileCreateStruct
Create struct for BinaryFile objects.
Definition: SPI/IO/BinaryFileCreateStruct.php:13
eZ\Publish\SPI\IO\BinaryFile
This class provides an abstract access to binary files.
Definition: SPI/IO/BinaryFile.php:15
eZ\Publish\Core\IO\IOMetadataHandler
Provides reading & writing of files meta data (size, modification time...).
Definition: IOMetadataHandler.php:16
eZ\Publish\Core\IO\UrlDecorator
Modifies, both way, and URI.
Definition: UrlDecorator.php:13
eZ\Publish\Core\IO\IOMetadataHandler
Definition: Publish/Core/IO/IOMetadataHandler/Flysystem.php:7
eZ\Publish\Core\IO\UrlDecorator
Definition: AbsolutePrefix.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.