TYPO3 CMS  TYPO3_7-6
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 
23 class File extends AbstractFile
24 {
28  protected $metaDataLoaded = false;
29 
33  protected $metaDataProperties = [];
34 
40  protected $indexingInProgress = false;
41 
48  protected $updatedProperties = [];
49 
58  public function __construct(array $fileData, ResourceStorage $storage, array $metaData = [])
59  {
60  $this->identifier = $fileData['identifier'];
61  $this->name = $fileData['name'];
62  $this->properties = $fileData;
63  $this->storage = $storage;
64  if (!empty($metaData)) {
65  $this->metaDataLoaded = true;
66  $this->metaDataProperties = $metaData;
67  }
68  }
69 
70  /*******************************
71  * VARIOUS FILE PROPERTY GETTERS
72  *******************************/
79  public function getProperty($key)
80  {
81  if (parent::hasProperty($key)) {
82  return parent::getProperty($key);
83  } else {
84  $metaData = $this->_getMetaData();
85  return isset($metaData[$key]) ? $metaData[$key] : null;
86  }
87  }
88 
96  public function hasProperty($key)
97  {
98  if (!parent::hasProperty($key)) {
99  return array_key_exists($key, $this->_getMetaData());
100  }
101  return true;
102  }
103 
109  public function getProperties()
110  {
111  return array_merge(parent::getProperties(), array_diff_key($this->_getMetaData(), parent::getProperties()));
112  }
113 
120  public function _getMetaData()
121  {
122  if (!$this->metaDataLoaded) {
123  $this->loadMetaData();
124  }
126  }
127 
128  /******************
129  * CONTENTS RELATED
130  ******************/
136  public function getContents()
137  {
138  return $this->getStorage()->getFileContents($this);
139  }
140 
146  public function getSha1()
147  {
148  if (empty($this->properties['sha1'])) {
149  $this->properties['sha1'] = parent::getSha1();
150  }
151  return $this->properties['sha1'];
152  }
153 
160  public function setContents($contents)
161  {
162  $this->getStorage()->setFileContents($this, $contents);
163  return $this;
164  }
165 
166  /***********************
167  * INDEX RELATED METHODS
168  ***********************/
174  public function isIndexed()
175  {
176  return true;
177  }
178 
183  protected function loadMetaData()
184  {
185  if (!$this->indexingInProgress) {
186  $this->indexingInProgress = true;
187  $this->metaDataProperties = $this->getMetaDataRepository()->findByFile($this);
188  $this->metaDataLoaded = true;
189  $this->indexingInProgress = false;
190  }
191  }
192 
205  public function updateProperties(array $properties)
206  {
207  // Setting identifier and name to update values; we have to do this
208  // here because we might need a new identifier when loading
209  // (and thus possibly indexing) a file.
210  if (isset($properties['identifier'])) {
211  $this->identifier = $properties['identifier'];
212  }
213  if (isset($properties['name'])) {
214  $this->name = $properties['name'];
215  }
216 
217  if ($this->properties['uid'] != 0 && isset($properties['uid'])) {
218  unset($properties['uid']);
219  }
220  foreach ($properties as $key => $value) {
221  if ($this->properties[$key] !== $value) {
222  if (!in_array($key, $this->updatedProperties)) {
223  $this->updatedProperties[] = $key;
224  }
225  $this->properties[$key] = $value;
226  }
227  }
228  // If the mime_type property should be updated and it was changed also update the type.
229  if (array_key_exists('mime_type', $properties) && in_array('mime_type', $this->updatedProperties)) {
230  $this->updatedProperties[] = 'type';
231  unset($this->properties['type']);
232  $this->getType();
233  }
234  if (array_key_exists('storage', $properties) && in_array('storage', $this->updatedProperties)) {
235  $this->storage = ResourceFactory::getInstance()->getStorageObject($properties['storage']);
236  }
237  }
238 
247  public function _updateMetaDataProperties(array $properties)
248  {
249  $this->metaDataProperties = array_merge($this->metaDataProperties, $properties);
250  }
251 
257  public function getUpdatedProperties()
258  {
260  }
261 
262  /****************************************
263  * STORAGE AND MANAGEMENT RELATED METHODS
264  ****************************************/
271  public function checkActionPermission($action)
272  {
273  return $this->getStorage()->checkFileActionPermission($action, $this);
274  }
275 
276  /*****************
277  * SPECIAL METHODS
278  *****************/
286  public function calculateChecksum()
287  {
288  return md5(
289  $this->getCombinedIdentifier() . '|' .
290  $this->getMimeType() . '|' .
291  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
292  );
293  }
294 
302  public function process($taskType, array $configuration)
303  {
304  return $this->getStorage()->processFile($this, $taskType, $configuration);
305  }
306 
313  public function toArray()
314  {
315  $array = [
316  'id' => $this->getCombinedIdentifier(),
317  'name' => $this->getName(),
318  'extension' => $this->getExtension(),
319  'type' => $this->getType(),
320  'mimetype' => $this->getMimeType(),
321  'size' => $this->getSize(),
322  'url' => $this->getPublicUrl(),
323  'indexed' => true,
324  'uid' => $this->getUid(),
325  'permissions' => [
326  'read' => $this->checkActionPermission('read'),
327  'write' => $this->checkActionPermission('write'),
328  'delete' => $this->checkActionPermission('delete')
329  ],
330  'checksum' => $this->calculateChecksum()
331  ];
332  foreach ($this->properties as $key => $value) {
333  $array[$key] = $value;
334  }
335  $stat = $this->getStorage()->getFileInfo($this);
336  foreach ($stat as $key => $value) {
337  $array[$key] = $value;
338  }
339  return $array;
340  }
341 
345  public function isMissing()
346  {
347  return (bool)$this->getProperty('missing');
348  }
349 
353  public function setMissing($missing)
354  {
355  $this->updateProperties(['missing' => $missing ? 1 : 0]);
356  }
357 
369  public function getPublicUrl($relativeToCurrentScript = false)
370  {
371  if ($this->isMissing() || $this->deleted) {
372  return false;
373  } else {
374  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
375  }
376  }
377 
381  protected function getMetaDataRepository()
382  {
383  return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
384  }
385 
389  protected function getFileIndexRepository()
390  {
391  return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\FileIndexRepository::class);
392  }
393 
398  public function setIndexingInProgess($indexingState)
399  {
400  $this->indexingInProgress = (bool)$indexingState;
401  }
402 
408  public function _getPropertyRaw($key)
409  {
410  return parent::getProperty($key);
411  }
412 }
_updateMetaDataProperties(array $properties)
Definition: File.php:247
updateProperties(array $properties)
Definition: File.php:205
__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:58
checkActionPermission($action)
Definition: File.php:271
setIndexingInProgess($indexingState)
Definition: File.php:398
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
process($taskType, array $configuration)
Definition: File.php:302
getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:369