‪TYPO3CMS  10.4
File.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
23 class ‪File extends ‪AbstractFile
24 {
31  protected ‪$updatedProperties = [];
32 
36  private ‪$metaDataAspect;
37 
46  public function ‪__construct(array $fileData, ‪ResourceStorage ‪$storage, array $metaData = [])
47  {
48  $this->identifier = $fileData['identifier'] ?? null;
49  $this->name = $fileData['name'] ?? '';
50  $this->properties = $fileData;
51  $this->storage = ‪$storage;
52 
53  if (!empty($metaData)) {
54  $this->‪getMetaData()->‪add($metaData);
55  }
56  }
57 
58  /*******************************
59  * VARIOUS FILE PROPERTY GETTERS
60  *******************************/
67  public function ‪getProperty($key)
68  {
69  if (parent::hasProperty($key)) {
70  return parent::getProperty($key);
71  }
72  return $this->‪getMetaData()[$key];
73  }
74 
82  public function ‪hasProperty($key): bool
83  {
84  if (!parent::hasProperty($key)) {
85  return isset($this->‪getMetaData()[$key]);
86  }
87  return true;
88  }
89 
95  public function ‪getProperties(): array
96  {
97  return array_merge(
98  parent::getProperties(),
99  array_diff_key($this->‪getMetaData()->get(), parent::getProperties()),
100  [
101  'metadata_uid' => $this->‪getMetaData()->get()['uid']
102  ]
103  );
104  }
105 
113  public function ‪_getMetaData()
114  {
115  trigger_error(
116  'The method ' . __CLASS__ . '::' . __METHOD__ . ' has been marked as deprecated and will be removed in TYPO3 v11. Use `->getMetaData()->get()` instead.',
117  E_USER_DEPRECATED
118  );
119  return $this->‪getMetaData()->‪get();
120  }
121 
122  /******************
123  * CONTENTS RELATED
124  ******************/
130  public function ‪getContents()
131  {
132  return $this->‪getStorage()->‪getFileContents($this);
133  }
134 
140  public function ‪getSha1()
141  {
142  if (empty($this->properties['sha1'])) {
143  $this->properties['sha1'] = parent::getSha1();
144  }
145  return $this->properties['sha1'];
146  }
147 
154  public function ‪setContents($contents)
155  {
156  $this->‪getStorage()->‪setFileContents($this, $contents);
157  return $this;
158  }
159 
160  /***********************
161  * INDEX RELATED METHODS
162  ***********************/
168  public function ‪isIndexed()
169  {
170  return true;
171  }
172 
184  public function ‪updateProperties(array ‪$properties)
185  {
186  // Setting identifier and name to update values; we have to do this
187  // here because we might need a new identifier when loading
188  // (and thus possibly indexing) a file.
189  if (isset(‪$properties['identifier'])) {
190  $this->identifier = ‪$properties['identifier'];
191  }
192  if (isset(‪$properties['name'])) {
193  $this->name = ‪$properties['name'];
194  }
195 
196  if (isset(‪$properties['uid']) && $this->properties['uid'] != 0) {
197  unset(‪$properties['uid']);
198  }
199  foreach (‪$properties as $key => $value) {
200  if ($this->properties[$key] !== $value) {
201  if (!in_array($key, $this->updatedProperties)) {
202  $this->updatedProperties[] = $key;
203  }
204  $this->properties[$key] = $value;
205  }
206  }
207  // If the mime_type property should be updated and it was changed also update the type.
208  if (array_key_exists('mime_type', ‪$properties) && in_array('mime_type', $this->updatedProperties)) {
209  $this->updatedProperties[] = 'type';
210  unset($this->properties['type']);
211  $this->‪getType();
212  }
213  if (array_key_exists('storage', ‪$properties) && in_array('storage', $this->updatedProperties)) {
214  $this->storage = GeneralUtility::makeInstance(ResourceFactory::class)->getStorageObject(‪$properties['storage']);
215  }
216  }
217 
223  public function ‪getUpdatedProperties()
224  {
226  }
227 
228  /****************************************
229  * STORAGE AND MANAGEMENT RELATED METHODS
230  ****************************************/
237  public function ‪checkActionPermission($action)
238  {
239  return $this->‪getStorage()->‪checkFileActionPermission($action, $this);
240  }
241 
242  /*****************
243  * SPECIAL METHODS
244  *****************/
252  public function ‪calculateChecksum()
253  {
254  return md5(
255  $this->‪getCombinedIdentifier() . '|' .
256  $this->‪getMimeType() . '|' .
257  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
258  );
259  }
260 
268  public function ‪process($taskType, array $configuration)
269  {
270  return $this->‪getStorage()->‪processFile($this, $taskType, $configuration);
271  }
272 
279  public function ‪toArray()
280  {
281  $array = [
282  'id' => $this->‪getCombinedIdentifier(),
283  'name' => $this->‪getName(),
284  'extension' => $this->‪getExtension(),
285  'type' => $this->‪getType(),
286  'mimetype' => $this->‪getMimeType(),
287  'size' => $this->‪getSize(),
288  'url' => $this->‪getPublicUrl(),
289  'indexed' => true,
290  'uid' => $this->‪getUid(),
291  'permissions' => [
292  'read' => $this->‪checkActionPermission('read'),
293  'write' => $this->‪checkActionPermission('write'),
294  'delete' => $this->‪checkActionPermission('delete')
295  ],
296  'checksum' => $this->‪calculateChecksum()
297  ];
298  foreach ($this->properties as $key => $value) {
299  $array[$key] = $value;
300  }
301  $stat = $this->‪getStorage()->‪getFileInfo($this);
302  foreach ($stat as $key => $value) {
303  $array[$key] = $value;
304  }
305  return $array;
306  }
307 
311  public function ‪isMissing()
312  {
313  return (bool)$this->‪getProperty('missing');
314  }
315 
319  public function ‪setMissing($missing)
320  {
321  $this->‪updateProperties(['missing' => $missing ? 1 : 0]);
322  }
323 
335  public function ‪getPublicUrl($relativeToCurrentScript = false)
336  {
337  if ($this->‪isMissing() || $this->deleted) {
338  return null;
339  }
340  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
341  }
342 
348  public function ‪_getPropertyRaw($key)
349  {
350  return parent::getProperty($key);
351  }
352 
358  public function ‪getMetaData(): ‪MetaDataAspect
359  {
360  if ($this->metaDataAspect === null) {
361  $this->metaDataAspect = GeneralUtility::makeInstance(MetaDataAspect::class, $this);
362  }
364  }
365 }
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:152
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪MetaDataAspect getMetaData()
Definition: File.php:356
‪TYPO3\CMS\Core\Resource\File\$metaDataAspect
‪MetaDataAspect $metaDataAspect
Definition: File.php:34
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:286
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileInfo
‪array getFileInfo(FileInterface $fileObject)
Definition: ResourceStorage.php:1473
‪TYPO3\CMS\Core\Resource\File\setMissing
‪setMissing($missing)
Definition: File.php:317
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:333
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1369
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\File\isIndexed
‪bool null isIndexed()
Definition: File.php:166
‪TYPO3\CMS\Core\Resource\ResourceStorage\processFile
‪ProcessedFile processFile(FileInterface $fileObject, $context, array $configuration)
Definition: ResourceStorage.php:1428
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:268
‪TYPO3\CMS\Core\Resource\File\getUpdatedProperties
‪array getUpdatedProperties()
Definition: File.php:221
‪TYPO3\CMS\Core\Resource\File\__construct
‪__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:44
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1737
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:266
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1813
‪TYPO3\CMS\Core\Resource\File\hasProperty
‪bool hasProperty($key)
Definition: File.php:80
‪TYPO3\CMS\Core\Resource\File\getSha1
‪string getSha1()
Definition: File.php:138
‪TYPO3\CMS\Core\Resource\File\getProperties
‪array getProperties()
Definition: File.php:93
‪TYPO3\CMS\Core\Resource\File\_getMetaData
‪array _getMetaData()
Definition: File.php:111
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:26
‪TYPO3\CMS\Core\Resource\MetaDataAspect\add
‪self add(array $metaData)
Definition: MetaDataAspect.php:63
‪TYPO3\CMS\Core\Resource\File\$updatedProperties
‪array $updatedProperties
Definition: File.php:30
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage $storage
Definition: AbstractFile.php:41
‪TYPO3\CMS\Core\Resource\File\calculateChecksum
‪string calculateChecksum()
Definition: File.php:250
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\MetaDataAspect\get
‪array get()
Definition: MetaDataAspect.php:76
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:447
‪TYPO3\CMS\Core\Resource\File\toArray
‪array toArray()
Definition: File.php:277
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:202
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:128
‪TYPO3\CMS\Core\Resource\File\checkActionPermission
‪bool checkActionPermission($action)
Definition: File.php:235
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:184
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:254
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\File\getProperty
‪mixed getProperty($key)
Definition: File.php:65
‪TYPO3\CMS\Core\Resource\File\updateProperties
‪updateProperties(array $properties)
Definition: File.php:182
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:35
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFileActionPermission
‪bool checkFileActionPermission($action, FileInterface $file)
Definition: ResourceStorage.php:693
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\File\isMissing
‪bool isMissing()
Definition: File.php:309
‪TYPO3\CMS\Core\Resource\File\_getPropertyRaw
‪mixed _getPropertyRaw($key)
Definition: File.php:346
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:390