‪TYPO3CMS  ‪main
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 
42  public function ‪__construct(array $fileData, ‪ResourceStorage ‪$storage, array $metaData = [])
43  {
44  $this->identifier = $fileData['identifier'] ?? null;
45  $this->name = $fileData['name'] ?? '';
46  $this->properties = $fileData;
47  $this->storage = ‪$storage;
48 
49  if (!empty($metaData)) {
50  $this->‪getMetaData()->add($metaData);
51  }
52  }
53 
54  /*******************************
55  * VARIOUS FILE PROPERTY GETTERS
56  *******************************/
63  public function ‪getProperty($key)
64  {
65  if (parent::hasProperty($key)) {
66  return parent::getProperty($key);
67  }
68  return $this->‪getMetaData()[$key];
69  }
70 
77  public function ‪hasProperty($key): bool
78  {
79  if (!parent::hasProperty($key)) {
80  return isset($this->‪getMetaData()[$key]);
81  }
82  return true;
83  }
84 
88  public function ‪getProperties(): array
89  {
90  return array_merge(
91  parent::getProperties(),
92  array_diff_key($this->‪getMetaData()->get(), parent::getProperties()),
93  [
94  'metadata_uid' => $this->‪getMetaData()->get()['uid'] ?? 0,
95  ]
96  );
97  }
98 
99  /******************
100  * CONTENTS RELATED
101  ******************/
107  public function ‪getContents()
108  {
109  return $this->‪getStorage()->‪getFileContents($this);
110  }
111 
117  public function ‪getSha1()
118  {
119  if (empty($this->properties['sha1'])) {
120  $this->properties['sha1'] = parent::getSha1();
121  }
122  return $this->properties['sha1'];
123  }
124 
131  public function ‪setContents($contents)
132  {
133  $this->‪getStorage()->‪setFileContents($this, $contents);
134  return $this;
135  }
136 
137  /***********************
138  * INDEX RELATED METHODS
139  ***********************/
145  public function ‪isIndexed()
146  {
147  return true;
148  }
149 
161  public function ‪updateProperties(array ‪$properties)
162  {
163  // Setting identifier and name to update values; we have to do this
164  // here because we might need a new identifier when loading
165  // (and thus possibly indexing) a file.
166  if (isset(‪$properties['identifier'])) {
167  $this->identifier = ‪$properties['identifier'];
168  }
169  if (isset(‪$properties['name'])) {
170  $this->name = ‪$properties['name'];
171  }
172 
173  if (isset(‪$properties['uid']) && $this->properties['uid'] != 0) {
174  unset(‪$properties['uid']);
175  }
176  foreach (‪$properties as $key => $value) {
177  if (!isset($this->properties[$key]) || $this->properties[$key] !== $value) {
178  if (!in_array($key, $this->updatedProperties)) {
179  $this->updatedProperties[] = $key;
180  }
181  $this->properties[$key] = $value;
182  }
183  }
184  // If the mime_type property should be updated and it was changed also update the type.
185  if (array_key_exists('mime_type', ‪$properties) && in_array('mime_type', $this->updatedProperties)) {
186  $this->updatedProperties[] = 'type';
187  unset($this->properties['type']);
188  $this->‪getType();
189  }
190  if (array_key_exists('storage', ‪$properties) && in_array('storage', $this->updatedProperties)) {
191  $this->storage = GeneralUtility::makeInstance(StorageRepository::class)->findByUid((int)‪$properties['storage']);
192  }
193  }
194 
200  public function ‪getUpdatedProperties()
201  {
203  }
204 
205  /****************************************
206  * STORAGE AND MANAGEMENT RELATED METHODS
207  ****************************************/
214  public function ‪checkActionPermission($action)
215  {
216  return $this->‪getStorage()->‪checkFileActionPermission($action, $this);
217  }
218 
219  /*****************
220  * SPECIAL METHODS
221  *****************/
229  public function ‪calculateChecksum()
230  {
231  return md5(
232  $this->‪getCombinedIdentifier() . '|' .
233  $this->‪getMimeType() . '|' .
234  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
235  );
236  }
237 
245  public function ‪process($taskType, array $configuration)
246  {
247  return $this->‪getStorage()->‪processFile($this, $taskType, $configuration);
248  }
249 
256  public function ‪toArray()
257  {
258  $array = [
259  'id' => $this->‪getCombinedIdentifier(),
260  'name' => $this->‪getName(),
261  'extension' => $this->‪getExtension(),
262  'type' => $this->‪getType(),
263  'mimetype' => $this->‪getMimeType(),
264  'size' => $this->‪getSize(),
265  'url' => $this->‪getPublicUrl(),
266  'indexed' => true,
267  'uid' => $this->‪getUid(),
268  'permissions' => [
269  'read' => $this->‪checkActionPermission('read'),
270  'write' => $this->‪checkActionPermission('write'),
271  'delete' => $this->‪checkActionPermission('delete'),
272  ],
273  'checksum' => $this->‪calculateChecksum(),
274  ];
275  foreach ($this->properties as $key => $value) {
276  $array[$key] = $value;
277  }
278  $stat = $this->‪getStorage()->‪getFileInfo($this);
279  foreach ($stat as $key => $value) {
280  $array[$key] = $value;
281  }
282  return $array;
283  }
284 
288  public function ‪isMissing()
289  {
290  return (bool)$this->‪getProperty('missing');
291  }
292 
296  public function ‪setMissing($missing)
297  {
298  $this->‪updateProperties(['missing' => $missing ? 1 : 0]);
299  }
300 
310  public function ‪getPublicUrl()
311  {
312  if ($this->‪isMissing() || $this->deleted) {
313  return null;
314  }
315  return $this->‪getStorage()->‪getPublicUrl($this);
316  }
317 
323  public function ‪_getPropertyRaw($key)
324  {
325  return parent::getProperty($key);
326  }
327 
331  public function ‪getMetaData(): ‪MetaDataAspect
332  {
333  if ($this->metaDataAspect === null) {
334  $this->metaDataAspect = GeneralUtility::makeInstance(MetaDataAspect::class, $this);
335  }
337  }
338 }
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:129
‪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:1443
‪TYPO3\CMS\Core\Resource\File\setMissing
‪setMissing($missing)
Definition: File.php:294
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\File\isIndexed
‪bool null isIndexed()
Definition: File.php:143
‪TYPO3\CMS\Core\Resource\ResourceStorage\processFile
‪ProcessedFile processFile(FileInterface $fileObject, $context, array $configuration)
Definition: ResourceStorage.php:1376
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪string null getPublicUrl()
Definition: File.php:308
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:269
‪TYPO3\CMS\Core\Resource\File\getUpdatedProperties
‪array getUpdatedProperties()
Definition: File.php:198
‪TYPO3\CMS\Core\Resource\File\__construct
‪__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:40
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1699
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:243
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1772
‪TYPO3\CMS\Core\Resource\File\getSha1
‪string getSha1()
Definition: File.php:115
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:26
‪TYPO3\CMS\Core\Resource\File\$updatedProperties
‪array $updatedProperties
Definition: File.php:30
‪TYPO3\CMS\Core\Resource\File\calculateChecksum
‪string calculateChecksum()
Definition: File.php:227
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:451
‪TYPO3\CMS\Core\Resource\File\toArray
‪array toArray()
Definition: File.php:254
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:203
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:105
‪TYPO3\CMS\Core\Resource\File\getProperties
‪getProperties()
Definition: File.php:86
‪TYPO3\CMS\Core\Resource\File\checkActionPermission
‪bool checkActionPermission($action)
Definition: File.php:212
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪getMetaData()
Definition: File.php:329
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:127
‪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:61
‪TYPO3\CMS\Core\Resource\File\updateProperties
‪updateProperties(array $properties)
Definition: File.php:159
‪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:696
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Resource\File\isMissing
‪bool isMissing()
Definition: File.php:286
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject)
Definition: ResourceStorage.php:1327
‪TYPO3\CMS\Core\Resource\File\_getPropertyRaw
‪mixed _getPropertyRaw($key)
Definition: File.php:321
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395
‪TYPO3\CMS\Core\Resource\File\hasProperty
‪hasProperty($key)
Definition: File.php:75