‪TYPO3CMS  ‪main
File.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
25 class ‪File extends ‪AbstractFile
26 {
33  protected ‪$updatedProperties = [];
34 
38  private ‪$metaDataAspect;
39 
44  public function ‪__construct(array $fileData, ‪ResourceStorage ‪$storage, array $metaData = [])
45  {
46  $this->identifier = $fileData['identifier'] ?? null;
47  $this->name = $fileData['name'] ?? '';
48  $this->‪properties = $fileData;
49  $this->storage = ‪$storage;
50 
51  if (!empty($metaData)) {
52  $this->‪getMetaData()->add($metaData);
53  }
54  }
55 
56  /*******************************
57  * VARIOUS FILE PROPERTY GETTERS
58  *******************************/
64  public function ‪getProperty(string $key): mixed
65  {
66  if (parent::hasProperty($key)) {
67  return parent::getProperty($key);
68  }
69  return $this->‪getMetaData()[$key];
70  }
71 
78  public function ‪hasProperty($key): bool
79  {
80  if (!parent::hasProperty($key)) {
81  return isset($this->‪getMetaData()[$key]);
82  }
83  return true;
84  }
85 
89  public function ‪getProperties(): array
90  {
91  return array_merge(
92  parent::getProperties(),
93  array_diff_key($this->‪getMetaData()->get(), parent::getProperties()),
94  [
95  'metadata_uid' => $this->‪getMetaData()->get()['uid'] ?? 0,
96  ]
97  );
98  }
99 
100  /******************
101  * CONTENTS RELATED
102  ******************/
106  public function ‪getContents(): string
107  {
108  return $this->‪getStorage()->getFileContents($this);
109  }
110 
116  public function ‪getSha1(): string
117  {
118  if (empty($this->‪properties['sha1'])) {
119  $this->‪properties['sha1'] = parent::getSha1();
120  }
121  return $this->‪properties['sha1'];
122  }
123 
129  public function ‪setContents(string $contents): self
130  {
131  $this->‪getStorage()->setFileContents($this, $contents);
132  return $this;
133  }
134 
135  /***********************
136  * INDEX RELATED METHODS
137  ***********************/
141  public function ‪isIndexed(): bool
142  {
143  return true;
144  }
145 
157  public function ‪updateProperties(array ‪$properties)
158  {
159  // Setting identifier and name to update values; we have to do this
160  // here because we might need a new identifier when loading
161  // (and thus possibly indexing) a file.
162  if (isset(‪$properties['identifier'])) {
163  $this->identifier = ‪$properties['identifier'];
164  }
165  if (isset(‪$properties['name'])) {
166  $this->name = ‪$properties['name'];
167  }
168 
169  if (isset(‪$properties['uid']) && $this->‪properties['uid'] != 0) {
170  unset(‪$properties['uid']);
171  }
172  foreach (‪$properties as $key => $value) {
173  if (!isset($this->‪properties[$key]) || $this->‪properties[$key] !== $value) {
174  if (!in_array($key, $this->updatedProperties)) {
175  $this->updatedProperties[] = $key;
176  }
177  $this->‪properties[$key] = $value;
178  }
179  }
180  // If the mime_type property should be updated and it was changed also update the type.
181  if (array_key_exists('mime_type', ‪$properties) && in_array('mime_type', $this->updatedProperties)) {
182  $this->updatedProperties[] = 'type';
183  unset($this->‪properties['type']);
184  $this->‪getType();
185  }
186  if (array_key_exists('storage', ‪$properties) && in_array('storage', $this->updatedProperties)) {
187  $this->storage = GeneralUtility::makeInstance(StorageRepository::class)->findByUid((int)‪$properties['storage']);
188  }
189  }
190 
196  public function ‪getUpdatedProperties()
197  {
199  }
200 
201  /****************************************
202  * STORAGE AND MANAGEMENT RELATED METHODS
203  ****************************************/
210  public function ‪checkActionPermission($action)
211  {
212  return $this->‪getStorage()->checkFileActionPermission($action, $this);
213  }
214 
215  /*****************
216  * SPECIAL METHODS
217  *****************/
225  public function ‪calculateChecksum()
226  {
227  return md5(
228  $this->‪getCombinedIdentifier() . '|' .
229  $this->‪getMimeType() . '|' .
230  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
231  );
232  }
233 
240  public function ‪process(string $taskType, array $configuration): ‪ProcessedFile
241  {
242  return $this->‪getStorage()->processFile($this, $taskType, $configuration);
243  }
244 
251  public function toArray(): array
252  {
253  ‪$array = [
254  'id' => $this->‪getCombinedIdentifier(),
255  'name' => $this->‪getName(),
256  'extension' => $this->‪getExtension(),
257  'type' => $this->‪getType(),
258  'mimetype' => $this->‪getMimeType(),
259  'size' => $this->getSize(),
260  'url' => $this->‪getPublicUrl(),
261  'indexed' => true,
262  'uid' => $this->‪getUid(),
263  'permissions' => [
264  'read' => $this->‪checkActionPermission('read'),
265  'write' => $this->‪checkActionPermission('write'),
266  'delete' => $this->‪checkActionPermission('delete'),
267  ],
268  'checksum' => $this->‪calculateChecksum(),
269  ];
270  foreach ($this->‪properties as $key => $value) {
271  ‪$array[$key] = $value;
272  }
273  ‪$stat = $this->‪getStorage()->getFileInfo($this);
274  foreach (‪$stat as $key => $value) {
275  ‪$array[$key] = $value;
276  }
277  return ‪$array;
278  }
279 
283  public function ‪isMissing()
284  {
285  return (bool)$this->‪getProperty('missing');
286  }
287 
291  public function ‪setMissing($missing)
292  {
293  $this->‪updateProperties(['missing' => $missing ? 1 : 0]);
294  }
295 
303  public function ‪getPublicUrl(): ?string
304  {
305  if ($this->‪isMissing() || $this->deleted) {
306  return null;
307  }
308  return $this->‪getStorage()->‪getPublicUrl($this);
309  }
310 
316  public function ‪_getPropertyRaw($key)
317  {
318  return parent::getProperty($key);
319  }
320 
324  public function ‪getMetaData(): MetaDataAspect
325  {
326  if ($this->metaDataAspect === null) {
327  $this->metaDataAspect = GeneralUtility::makeInstance(MetaDataAspect::class, $this);
328  }
330  }
331 }
‪TYPO3\CMS\Core\Resource\File\$metaDataAspect
‪MetaDataAspect $metaDataAspect
Definition: File.php:36
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:281
‪TYPO3\CMS\Core\Resource\File\setMissing
‪setMissing($missing)
Definition: File.php:289
‪TYPO3\CMS\Core\Resource\File\getContents
‪getContents()
Definition: File.php:104
‪TYPO3\CMS\Core\Resource\File\isIndexed
‪isIndexed()
Definition: File.php:139
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪getExtension()
Definition: AbstractFile.php:243
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪getName()
Definition: AbstractFile.php:157
‪TYPO3\CMS\Core\Resource\File\getUpdatedProperties
‪array getUpdatedProperties()
Definition: File.php:194
‪TYPO3\CMS\Core\Resource\File\__construct
‪__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:42
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\AbstractFile\properties
‪array< non-empty-string, function getProperties() { return $this-> properties
Definition: AbstractFile.php:141
‪TYPO3\CMS\Core\Resource\File\getProperty
‪getProperty(string $key)
Definition: File.php:62
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:29
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪getPublicUrl()
Definition: File.php:301
‪TYPO3\CMS\Core\Resource\File\getSha1
‪non empty string getSha1()
Definition: File.php:114
‪TYPO3\CMS\Core\Resource\File\$updatedProperties
‪array $updatedProperties
Definition: File.php:32
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪int< 0, getSize():int { if( $this->deleted) { throw new \RuntimeException( 'File has been deleted.', 1329821480);} if(empty( $this->properties[ 'size'])) { $fileInfo=$this-> getStorage() -> getFileInfoByIdentifier($this->getIdentifier(), ['size'])
‪TYPO3\CMS\Core\Resource\File\calculateChecksum
‪string calculateChecksum()
Definition: File.php:223
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\File\$array
‪foreach($stat as $key=> $value) return $array
Definition: File.php:272
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪non empty string getMimeType()
Definition: AbstractFile.php:257
‪TYPO3\CMS\Core\Resource\File\getCombinedIdentifier
‪array< non-empty-string, toArray():array { $array=['id'=> $this getCombinedIdentifier()
‪TYPO3\CMS\Core\Resource\File\getProperties
‪getProperties()
Definition: File.php:87
‪TYPO3\CMS\Core\Resource\File\checkActionPermission
‪bool checkActionPermission($action)
Definition: File.php:208
‪TYPO3\CMS\Core\Resource\File\$stat
‪array< non-empty-string, toArray():array { $array=['id'=> $this foreach($this->properties as $key=> $value) $stat
Definition: File.php:271
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪return MathUtility::canBeInterpretedAsInteger($size) ?(int) $size int getUid()
Definition: AbstractFile.php:195
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪getMetaData()
Definition: File.php:322
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:128
‪TYPO3\CMS\Core\Resource\File\setContents
‪$this setContents(string $contents)
Definition: File.php:127
‪TYPO3\CMS\Core\Resource\File\process
‪process(string $taskType, array $configuration)
Definition: File.php:238
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\File\updateProperties
‪updateProperties(array $properties)
Definition: File.php:155
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:39
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage null $storage
Definition: AbstractFile.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\File\isMissing
‪bool isMissing()
Definition: File.php:281
‪TYPO3\CMS\Core\Resource\File\_getPropertyRaw
‪mixed _getPropertyRaw($key)
Definition: File.php:314
‪TYPO3\CMS\Core\Resource\File\hasProperty
‪hasProperty($key)
Definition: File.php:76