TYPO3  7.6
core/Classes/Resource/File.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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 = array();
34 
40  protected $indexingInProgress = false;
41 
48  protected $updatedProperties = array();
49 
58  public function __construct(array $fileData, ResourceStorage $storage, array $metaData = array())
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 
104 
110  public function getProperties()
111  {
112  return array_merge(parent::getProperties(), array_diff_key($this->_getMetaData(), parent::getProperties()));
113  }
114 
121  public function _getMetaData()
122  {
123  if (!$this->metaDataLoaded) {
124  $this->loadMetaData();
125  }
127  }
128 
129  /******************
130  * CONTENTS RELATED
131  ******************/
137  public function getContents()
138  {
139  return $this->getStorage()->getFileContents($this);
140  }
141 
147  public function getSha1()
148  {
149  if (empty($this->properties['sha1'])) {
150  $this->properties['sha1'] = parent::getSha1();
151  }
152  return $this->properties['sha1'];
153  }
154 
161  public function setContents($contents)
162  {
163  $this->getStorage()->setFileContents($this, $contents);
164  return $this;
165  }
166 
167  /***********************
168  * INDEX RELATED METHODS
169  ***********************/
175  public function isIndexed()
176  {
177  return true;
178  }
179 
184  protected function loadMetaData()
185  {
186  if (!$this->indexingInProgress) {
187  $this->indexingInProgress = true;
188  $this->metaDataProperties = $this->getMetaDataRepository()->findByFile($this);
189  $this->metaDataLoaded = true;
190  $this->indexingInProgress = false;
191  }
192  }
193 
206  public function updateProperties(array $properties)
207  {
208  // Setting identifier and name to update values; we have to do this
209  // here because we might need a new identifier when loading
210  // (and thus possibly indexing) a file.
211  if (isset($properties['identifier'])) {
212  $this->identifier = $properties['identifier'];
213  }
214  if (isset($properties['name'])) {
215  $this->name = $properties['name'];
216  }
217 
218  if ($this->properties['uid'] != 0 && isset($properties['uid'])) {
219  unset($properties['uid']);
220  }
221  foreach ($properties as $key => $value) {
222  if ($this->properties[$key] !== $value) {
223  if (!in_array($key, $this->updatedProperties)) {
224  $this->updatedProperties[] = $key;
225  }
226  $this->properties[$key] = $value;
227  }
228  }
229  // If the mime_type property should be updated and it was changed also update the type.
230  if (array_key_exists('mime_type', $properties) && in_array('mime_type', $this->updatedProperties)) {
231  $this->updatedProperties[] = 'type';
232  unset($this->properties['type']);
233  $this->getType();
234  }
235  if (array_key_exists('storage', $properties) && in_array('storage', $this->updatedProperties)) {
236  $this->storage = ResourceFactory::getInstance()->getStorageObject($properties['storage']);
237  }
238  }
239 
248  public function _updateMetaDataProperties(array $properties)
249  {
250  $this->metaDataProperties = array_merge($this->metaDataProperties, $properties);
251  }
252 
258  public function getUpdatedProperties()
259  {
261  }
262 
263  /****************************************
264  * STORAGE AND MANAGEMENT RELATED METHODS
265  ****************************************/
272  public function checkActionPermission($action)
273  {
274  return $this->getStorage()->checkFileActionPermission($action, $this);
275  }
276 
277  /*****************
278  * SPECIAL METHODS
279  *****************/
287  public function calculateChecksum()
288  {
289  return md5(
290  $this->getCombinedIdentifier() . '|' .
291  $this->getMimeType() . '|' .
292  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
293  );
294  }
295 
303  public function process($taskType, array $configuration)
304  {
305  return $this->getStorage()->processFile($this, $taskType, $configuration);
306  }
307 
314  public function toArray()
315  {
316  $array = array(
317  'id' => $this->getCombinedIdentifier(),
318  'name' => $this->getName(),
319  'extension' => $this->getExtension(),
320  'type' => $this->getType(),
321  'mimetype' => $this->getMimeType(),
322  'size' => $this->getSize(),
323  'url' => $this->getPublicUrl(),
324  'indexed' => true,
325  'uid' => $this->getUid(),
326  'permissions' => array(
327  'read' => $this->checkActionPermission('read'),
328  'write' => $this->checkActionPermission('write'),
329  'delete' => $this->checkActionPermission('delete')
330  ),
331  'checksum' => $this->calculateChecksum()
332  );
333  foreach ($this->properties as $key => $value) {
334  $array[$key] = $value;
335  }
336  $stat = $this->getStorage()->getFileInfo($this);
337  foreach ($stat as $key => $value) {
338  $array[$key] = $value;
339  }
340  return $array;
341  }
342 
346  public function isMissing()
347  {
348  return (bool)$this->getProperty('missing');
349  }
350 
354  public function setMissing($missing)
355  {
356  $this->updateProperties(array('missing' => $missing ? 1 : 0));
357  }
358 
370  public function getPublicUrl($relativeToCurrentScript = false)
371  {
372  if ($this->isMissing() || $this->deleted) {
373  return false;
374  } else {
375  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
376  }
377  }
378 
382  protected function getMetaDataRepository()
383  {
384  return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
385  }
386 
390  protected function getFileIndexRepository()
391  {
392  return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\FileIndexRepository::class);
393  }
394 
399  public function setIndexingInProgess($indexingState)
400  {
401  $this->indexingInProgress = (bool)$indexingState;
402  }
403 
409  public function _getPropertyRaw($key)
410  {
411  return parent::getProperty($key);
412  }
413 }