‪TYPO3CMS  11.5
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'] ?? 0,
102  ]
103  );
104  }
105 
106  /******************
107  * CONTENTS RELATED
108  ******************/
114  public function ‪getContents()
115  {
116  return $this->‪getStorage()->‪getFileContents($this);
117  }
118 
124  public function ‪getSha1()
125  {
126  if (empty($this->properties['sha1'])) {
127  $this->properties['sha1'] = parent::getSha1();
128  }
129  return $this->properties['sha1'];
130  }
131 
138  public function ‪setContents($contents)
139  {
140  $this->‪getStorage()->‪setFileContents($this, $contents);
141  return $this;
142  }
143 
144  /***********************
145  * INDEX RELATED METHODS
146  ***********************/
152  public function ‪isIndexed()
153  {
154  return true;
155  }
156 
168  public function ‪updateProperties(array ‪$properties)
169  {
170  // Setting identifier and name to update values; we have to do this
171  // here because we might need a new identifier when loading
172  // (and thus possibly indexing) a file.
173  if (isset(‪$properties['identifier'])) {
174  $this->identifier = ‪$properties['identifier'];
175  }
176  if (isset(‪$properties['name'])) {
177  $this->name = ‪$properties['name'];
178  }
179 
180  if (isset(‪$properties['uid']) && $this->properties['uid'] != 0) {
181  unset(‪$properties['uid']);
182  }
183  foreach (‪$properties as $key => $value) {
184  if (!isset($this->properties[$key]) || $this->properties[$key] !== $value) {
185  if (!in_array($key, $this->updatedProperties)) {
186  $this->updatedProperties[] = $key;
187  }
188  $this->properties[$key] = $value;
189  }
190  }
191  // If the mime_type property should be updated and it was changed also update the type.
192  if (array_key_exists('mime_type', ‪$properties) && in_array('mime_type', $this->updatedProperties)) {
193  $this->updatedProperties[] = 'type';
194  unset($this->properties['type']);
195  $this->‪getType();
196  }
197  if (array_key_exists('storage', ‪$properties) && in_array('storage', $this->updatedProperties)) {
198  $this->storage = GeneralUtility::makeInstance(StorageRepository::class)->findByUid((int)‪$properties['storage']);
199  }
200  }
201 
207  public function ‪getUpdatedProperties()
208  {
210  }
211 
212  /****************************************
213  * STORAGE AND MANAGEMENT RELATED METHODS
214  ****************************************/
221  public function ‪checkActionPermission($action)
222  {
223  return $this->‪getStorage()->‪checkFileActionPermission($action, $this);
224  }
225 
226  /*****************
227  * SPECIAL METHODS
228  *****************/
236  public function ‪calculateChecksum()
237  {
238  return md5(
239  $this->‪getCombinedIdentifier() . '|' .
240  $this->‪getMimeType() . '|' .
241  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
242  );
243  }
244 
252  public function ‪process($taskType, array $configuration)
253  {
254  return $this->‪getStorage()->‪processFile($this, $taskType, $configuration);
255  }
256 
263  public function ‪toArray()
264  {
265  $array = [
266  'id' => $this->‪getCombinedIdentifier(),
267  'name' => $this->‪getName(),
268  'extension' => $this->‪getExtension(),
269  'type' => $this->‪getType(),
270  'mimetype' => $this->‪getMimeType(),
271  'size' => $this->‪getSize(),
272  'url' => $this->‪getPublicUrl(),
273  'indexed' => true,
274  'uid' => $this->‪getUid(),
275  'permissions' => [
276  'read' => $this->‪checkActionPermission('read'),
277  'write' => $this->‪checkActionPermission('write'),
278  'delete' => $this->‪checkActionPermission('delete'),
279  ],
280  'checksum' => $this->‪calculateChecksum(),
281  ];
282  foreach ($this->properties as $key => $value) {
283  $array[$key] = $value;
284  }
285  $stat = $this->‪getStorage()->‪getFileInfo($this);
286  foreach ($stat as $key => $value) {
287  $array[$key] = $value;
288  }
289  return $array;
290  }
291 
295  public function ‪isMissing()
296  {
297  return (bool)$this->‪getProperty('missing');
298  }
299 
303  public function ‪setMissing($missing)
304  {
305  $this->‪updateProperties(['missing' => $missing ? 1 : 0]);
306  }
307 
319  public function ‪getPublicUrl($relativeToCurrentScript = false)
320  {
321  if ($this->‪isMissing() || $this->deleted) {
322  return null;
323  }
324  // @deprecated $relativeToCurrentScript since v11, will be removed in TYPO3 v12.0
325  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
326  }
327 
333  public function ‪_getPropertyRaw($key)
334  {
335  return parent::getProperty($key);
336  }
337 
343  public function ‪getMetaData(): ‪MetaDataAspect
344  {
345  if ($this->metaDataAspect === null) {
346  $this->metaDataAspect = GeneralUtility::makeInstance(MetaDataAspect::class, $this);
347  }
349  }
350 }
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:136
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪MetaDataAspect getMetaData()
Definition: File.php:341
‪TYPO3\CMS\Core\Resource\File\$metaDataAspect
‪MetaDataAspect $metaDataAspect
Definition: File.php:34
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:291
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileInfo
‪array getFileInfo(FileInterface $fileObject)
Definition: ResourceStorage.php:1506
‪TYPO3\CMS\Core\Resource\File\setMissing
‪setMissing($missing)
Definition: File.php:301
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:317
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1374
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\File\isIndexed
‪bool null isIndexed()
Definition: File.php:150
‪TYPO3\CMS\Core\Resource\ResourceStorage\processFile
‪ProcessedFile processFile(FileInterface $fileObject, $context, array $configuration)
Definition: ResourceStorage.php:1436
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:269
‪TYPO3\CMS\Core\Resource\File\getUpdatedProperties
‪array getUpdatedProperties()
Definition: File.php:205
‪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:1769
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:250
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1847
‪TYPO3\CMS\Core\Resource\File\hasProperty
‪bool hasProperty($key)
Definition: File.php:80
‪TYPO3\CMS\Core\Resource\File\getSha1
‪string getSha1()
Definition: File.php:122
‪TYPO3\CMS\Core\Resource\File\getProperties
‪array getProperties()
Definition: File.php:93
‪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\File\calculateChecksum
‪string calculateChecksum()
Definition: File.php:234
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:452
‪TYPO3\CMS\Core\Resource\File\toArray
‪array toArray()
Definition: File.php:261
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:203
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:112
‪TYPO3\CMS\Core\Resource\File\checkActionPermission
‪bool checkActionPermission($action)
Definition: File.php:219
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:54
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:125
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:184
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:255
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪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:166
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:35
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage null $storage
Definition: AbstractFile.php:41
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFileActionPermission
‪bool checkFileActionPermission($action, FileInterface $file)
Definition: ResourceStorage.php:715
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\File\isMissing
‪bool isMissing()
Definition: File.php:293
‪TYPO3\CMS\Core\Resource\File\_getPropertyRaw
‪mixed _getPropertyRaw($key)
Definition: File.php:331
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395