TYPO3 CMS  TYPO3_8-7
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'];
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 isset($metaData[$key]) ? $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 ($this->properties['uid'] != 0 && isset($properties['uid'])) {
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)) {
231  $this->storage = ResourceFactory::getInstance()->getStorageObject($properties['storage']);
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 }
_updateMetaDataProperties(array $properties)
Definition: File.php:242
updateProperties(array $properties)
Definition: File.php:201
static makeInstance($className,... $constructorArguments)
__construct(array $fileData, ResourceStorage $storage, array $metaData=[])
Definition: File.php:57
checkActionPermission($action)
Definition: File.php:266
setIndexingInProgess($indexingState)
Definition: File.php:392
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
process($taskType, array $configuration)
Definition: File.php:297
getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:364