‪TYPO3CMS  9.5
File.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class ‪File extends ‪AbstractFile
23 {
27  protected ‪$metaDataLoaded = false;
28 
32  protected ‪$metaDataProperties = [];
33 
39  protected ‪$indexingInProgress = false;
40 
47  protected ‪$updatedProperties = [];
48 
57  public function ‪__construct(array $fileData, ‪ResourceStorage ‪$storage, array $metaData = [])
58  {
59  $this->identifier = $fileData['identifier'] ?? null;
60  $this->name = $fileData['name'] ?? '';
61  $this->properties = $fileData;
62  $this->storage = ‪$storage;
63  if (!empty($metaData)) {
64  $this->metaDataLoaded = true;
65  $this->metaDataProperties = $metaData;
66  }
67  }
68 
69  /*******************************
70  * VARIOUS FILE PROPERTY GETTERS
71  *******************************/
78  public function ‪getProperty($key)
79  {
80  if (parent::hasProperty($key)) {
81  return parent::getProperty($key);
82  }
83  $metaData = $this->‪_getMetaData();
84  return $metaData[$key] ?? null;
85  }
86 
94  public function ‪hasProperty($key)
95  {
96  if (!parent::hasProperty($key)) {
97  return array_key_exists($key, $this->‪_getMetaData());
98  }
99  return true;
100  }
101 
107  public function ‪getProperties()
108  {
109  return array_merge(parent::getProperties(), array_diff_key($this->‪_getMetaData(), parent::getProperties()));
110  }
111 
118  public function ‪_getMetaData()
119  {
120  if (!$this->metaDataLoaded) {
121  $this->‪loadMetaData();
122  }
124  }
125 
126  /******************
127  * CONTENTS RELATED
128  ******************/
134  public function ‪getContents()
135  {
136  return $this->‪getStorage()->‪getFileContents($this);
137  }
138 
144  public function ‪getSha1()
145  {
146  if (empty($this->properties['sha1'])) {
147  $this->properties['sha1'] = parent::getSha1();
148  }
149  return $this->properties['sha1'];
150  }
151 
158  public function ‪setContents($contents)
159  {
160  $this->‪getStorage()->‪setFileContents($this, $contents);
161  return $this;
162  }
163 
164  /***********************
165  * INDEX RELATED METHODS
166  ***********************/
172  public function ‪isIndexed()
173  {
174  return true;
175  }
176 
180  protected function ‪loadMetaData()
181  {
182  if (!$this->indexingInProgress) {
183  $this->indexingInProgress = true;
184  $this->metaDataProperties = $this->‪getMetaDataRepository()->findByFile($this);
185  $this->metaDataLoaded = true;
186  $this->indexingInProgress = false;
187  }
188  }
189 
201  public function ‪updateProperties(array ‪$properties)
202  {
203  // Setting identifier and name to update values; we have to do this
204  // here because we might need a new identifier when loading
205  // (and thus possibly indexing) a file.
206  if (isset(‪$properties['identifier'])) {
207  $this->identifier = ‪$properties['identifier'];
208  }
209  if (isset(‪$properties['name'])) {
210  $this->name = ‪$properties['name'];
211  }
212 
213  if (isset(‪$properties['uid']) && $this->properties['uid'] != 0) {
214  unset(‪$properties['uid']);
215  }
216  foreach (‪$properties as $key => $value) {
217  if ($this->properties[$key] !== $value) {
218  if (!in_array($key, $this->updatedProperties)) {
219  $this->updatedProperties[] = $key;
220  }
221  $this->properties[$key] = $value;
222  }
223  }
224  // If the mime_type property should be updated and it was changed also update the type.
225  if (array_key_exists('mime_type', ‪$properties) && in_array('mime_type', $this->updatedProperties)) {
226  $this->updatedProperties[] = 'type';
227  unset($this->properties['type']);
228  $this->‪getType();
229  }
230  if (array_key_exists('storage', ‪$properties) && in_array('storage', $this->updatedProperties)) {
232  }
233  }
234 
242  public function ‪_updateMetaDataProperties(array ‪$properties)
243  {
244  $this->metaDataProperties = array_merge($this->metaDataProperties, ‪$properties);
245  }
246 
252  public function ‪getUpdatedProperties()
253  {
255  }
256 
257  /****************************************
258  * STORAGE AND MANAGEMENT RELATED METHODS
259  ****************************************/
266  public function ‪checkActionPermission($action)
267  {
268  return $this->‪getStorage()->‪checkFileActionPermission($action, $this);
269  }
270 
271  /*****************
272  * SPECIAL METHODS
273  *****************/
281  public function ‪calculateChecksum()
282  {
283  return md5(
284  $this->‪getCombinedIdentifier() . '|' .
285  $this->‪getMimeType() . '|' .
286  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
287  );
288  }
289 
297  public function ‪process($taskType, array $configuration)
298  {
299  return $this->‪getStorage()->‪processFile($this, $taskType, $configuration);
300  }
301 
308  public function ‪toArray()
309  {
310  $array = [
311  'id' => $this->‪getCombinedIdentifier(),
312  'name' => $this->‪getName(),
313  'extension' => $this->‪getExtension(),
314  'type' => $this->‪getType(),
315  'mimetype' => $this->‪getMimeType(),
316  'size' => $this->‪getSize(),
317  'url' => $this->‪getPublicUrl(),
318  'indexed' => true,
319  'uid' => $this->‪getUid(),
320  'permissions' => [
321  'read' => $this->‪checkActionPermission('read'),
322  'write' => $this->‪checkActionPermission('write'),
323  'delete' => $this->‪checkActionPermission('delete')
324  ],
325  'checksum' => $this->‪calculateChecksum()
326  ];
327  foreach ($this->properties as $key => $value) {
328  $array[$key] = $value;
329  }
330  $stat = $this->‪getStorage()->‪getFileInfo($this);
331  foreach ($stat as $key => $value) {
332  $array[$key] = $value;
333  }
334  return $array;
335  }
336 
340  public function ‪isMissing()
341  {
342  return (bool)$this->‪getProperty('missing');
343  }
344 
348  public function ‪setMissing($missing)
349  {
350  $this->‪updateProperties(['missing' => $missing ? 1 : 0]);
351  }
352 
364  public function ‪getPublicUrl($relativeToCurrentScript = false)
365  {
366  if ($this->‪isMissing() || $this->deleted) {
367  return null;
368  }
369  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
370  }
371 
375  protected function ‪getMetaDataRepository()
376  {
377  return GeneralUtility::makeInstance(Index\MetaDataRepository::class);
378  }
379 
383  protected function ‪getFileIndexRepository()
384  {
385  return GeneralUtility::makeInstance(Index\FileIndexRepository::class);
386  }
387 
392  public function ‪setIndexingInProgess($indexingState)
393  {
394  $this->indexingInProgress = (bool)$indexingState;
395  }
396 
402  public function ‪_getPropertyRaw($key)
403  {
404  return parent::getProperty($key);
405  }
406 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:37
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:154
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:41
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:284
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileInfo
‪array getFileInfo(FileInterface $fileObject)
Definition: ResourceStorage.php:1421
‪TYPO3\CMS\Core\Resource\File\setMissing
‪setMissing($missing)
Definition: File.php:344
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:360
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1317
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:159
‪TYPO3\CMS\Core\Resource\File\isIndexed
‪bool null isIndexed()
Definition: File.php:168
‪TYPO3\CMS\Core\Resource\ResourceStorage\processFile
‪ProcessedFile processFile(FileInterface $fileObject, $context, array $configuration)
Definition: ResourceStorage.php:1376
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:266
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\File\getUpdatedProperties
‪array getUpdatedProperties()
Definition: File.php:248
‪TYPO3\CMS\Core\Resource\File\__construct
‪__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:53
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1672
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:293
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1785
‪TYPO3\CMS\Core\Resource\File\hasProperty
‪bool hasProperty($key)
Definition: File.php:90
‪TYPO3\CMS\Core\Resource\File\getSha1
‪string getSha1()
Definition: File.php:140
‪TYPO3\CMS\Core\Resource\File\getProperties
‪array getProperties()
Definition: File.php:103
‪TYPO3\CMS\Core\Resource\File\_getMetaData
‪array _getMetaData()
Definition: File.php:114
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:24
‪TYPO3\CMS\Core\Resource\File\$updatedProperties
‪array $updatedProperties
Definition: File.php:43
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage $storage
Definition: AbstractFile.php:39
‪TYPO3\CMS\Core\Resource\File\calculateChecksum
‪string calculateChecksum()
Definition: File.php:277
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:418
‪TYPO3\CMS\Core\Resource\File\toArray
‪array toArray()
Definition: File.php:304
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Core\Resource\File\loadMetaData
‪loadMetaData()
Definition: File.php:176
‪TYPO3\CMS\Core\Resource\File\getFileIndexRepository
‪Index FileIndexRepository getFileIndexRepository()
Definition: File.php:379
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:130
‪TYPO3\CMS\Core\Resource\File\getMetaDataRepository
‪Index MetaDataRepository getMetaDataRepository()
Definition: File.php:371
‪TYPO3\CMS\Core\Resource\File\checkActionPermission
‪bool checkActionPermission($action)
Definition: File.php:262
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Resource\File\_updateMetaDataProperties
‪_updateMetaDataProperties(array $properties)
Definition: File.php:238
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:182
‪TYPO3\CMS\Core\Resource\File\$indexingInProgress
‪bool $indexingInProgress
Definition: File.php:36
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:252
‪TYPO3\CMS\Core\Resource\ResourceFactory\getStorageObject
‪ResourceStorage getStorageObject($uid, array $recordData=[], &$fileIdentifier=null)
Definition: ResourceFactory.php:131
‪$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:74
‪TYPO3\CMS\Core\Resource\File\updateProperties
‪updateProperties(array $properties)
Definition: File.php:197
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:33
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFileActionPermission
‪bool checkFileActionPermission($action, FileInterface $file)
Definition: ResourceStorage.php:640
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\File\isMissing
‪bool isMissing()
Definition: File.php:336
‪TYPO3\CMS\Core\Resource\File\$metaDataLoaded
‪bool $metaDataLoaded
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\File\$metaDataProperties
‪array $metaDataProperties
Definition: File.php:30
‪TYPO3\CMS\Core\Resource\File\_getPropertyRaw
‪mixed _getPropertyRaw($key)
Definition: File.php:398
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:361
‪TYPO3\CMS\Core\Resource\File\setIndexingInProgess
‪setIndexingInProgess($indexingState)
Definition: File.php:388