TYPO3 CMS  TYPO3_6-2
File.php
Go to the documentation of this file.
1 <?php
3 
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 
53  protected $indexerService = NULL;
54 
63  public function __construct(array $fileData, ResourceStorage $storage, $metaData = array()) {
64  $this->identifier = $fileData['identifier'];
65  $this->name = $fileData['name'];
66  $this->properties = $fileData;
67  $this->storage = $storage;
68  if ($metaData !== array()) {
69  $this->metaDataLoaded = TRUE;
70  $this->metaDataProperties = $metaData;
71  }
72  }
73 
74  /*******************************
75  * VARIOUS FILE PROPERTY GETTERS
76  *******************************/
83  public function getProperty($key) {
84  if (parent::hasProperty($key)) {
85  return parent::getProperty($key);
86  } else {
87  if (!$this->metaDataLoaded) {
88  $this->loadMetaData();
89  }
90  return array_key_exists($key, $this->metaDataProperties) ? $this->metaDataProperties[$key] : NULL;
91  }
92  }
93 
101  public function hasProperty($key) {
102  if (!parent::hasProperty($key)) {
103  if (!$this->metaDataLoaded) {
104  $this->loadMetaData();
105  }
106  return array_key_exists($key, $this->metaDataProperties);
107  }
108  return TRUE;
109  }
110 
111 
117  public function getProperties() {
118  if (!$this->metaDataLoaded) {
119  $this->loadMetaData();
120  }
121  return array_merge(parent::getProperties(), array_diff_key((array)$this->metaDataProperties, parent::getProperties()));
122  }
123 
130  public function _getMetaData() {
131  if (!$this->metaDataLoaded) {
132  $this->loadMetaData();
133  }
135  }
136 
137  /******************
138  * CONTENTS RELATED
139  ******************/
145  public function getContents() {
146  return $this->getStorage()->getFileContents($this);
147  }
148 
154  public function getSha1() {
155  if (empty($this->properties['sha1'])) {
156  $this->properties['sha1'] = parent::getSha1();
157  }
158  return $this->properties['sha1'];
159  }
160 
167  public function setContents($contents) {
168  $this->getStorage()->setFileContents($this, $contents);
169  return $this;
170  }
171 
172  /***********************
173  * INDEX RELATED METHODS
174  ***********************/
180  public function isIndexed() {
181  return TRUE;
182  }
183 
188  protected function loadMetaData() {
189  if (!$this->indexingInProgress) {
190  $this->indexingInProgress = TRUE;
191  $this->metaDataProperties = $this->getMetaDataRepository()->findByFile($this);
192  $this->metaDataLoaded = TRUE;
193  $this->indexingInProgress = FALSE;
194  }
195 
196  }
197 
210  public function updateProperties(array $properties) {
211  // Setting identifier and name to update values; we have to do this
212  // here because we might need a new identifier when loading
213  // (and thus possibly indexing) a file.
214  if (isset($properties['identifier'])) {
215  $this->identifier = $properties['identifier'];
216  }
217  if (isset($properties['name'])) {
218  $this->name = $properties['name'];
219  }
220 
221  if ($this->properties['uid'] != 0 && isset($properties['uid'])) {
222  unset($properties['uid']);
223  }
224  foreach ($properties as $key => $value) {
225  if ($this->properties[$key] !== $value) {
226  if (!in_array($key, $this->updatedProperties)) {
227  $this->updatedProperties[] = $key;
228  }
229  $this->properties[$key] = $value;
230  }
231  }
232  // If the mime_type property should be updated and it was changed also update the type.
233  if (array_key_exists('mime_type', $properties) && in_array('mime_type', $this->updatedProperties)) {
234  $this->updatedProperties[] = 'type';
235  unset($this->properties['type']);
236  $this->getType();
237  }
238  if (array_key_exists('storage', $properties) && in_array('storage', $this->updatedProperties)) {
239  $this->storage = ResourceFactory::getInstance()->getStorageObject($properties['storage']);
240  }
241  }
242 
251  public function _updateMetaDataProperties(array $properties) {
252  if ($this->metaDataProperties !== NULL) {
253  $this->metaDataProperties = array_merge($this->metaDataProperties, $properties);
254  } else {
255  $this->metaDataProperties = $properties;
256  }
257  }
258 
264  public function getUpdatedProperties() {
266  }
267 
268  /****************************************
269  * STORAGE AND MANAGEMENT RELATED METHODS
270  ****************************************/
277  public function checkActionPermission($action) {
278  return $this->getStorage()->checkFileActionPermission($action, $this);
279  }
280 
281  /*****************
282  * SPECIAL METHODS
283  *****************/
291  public function calculateChecksum() {
292  return md5(
293  $this->getCombinedIdentifier() . '|' .
294  $this->getMimeType() . '|' .
295  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
296  );
297  }
298 
306  public function process($taskType, array $configuration) {
307  return $this->getStorage()->processFile($this, $taskType, $configuration);
308  }
309 
316  public function toArray() {
317  $array = array(
318  'id' => $this->getCombinedIdentifier(),
319  'name' => $this->getName(),
320  'extension' => $this->getExtension(),
321  'type' => $this->getType(),
322  'mimetype' => $this->getMimeType(),
323  'size' => $this->getSize(),
324  'url' => $this->getPublicUrl(),
325  'indexed' => TRUE,
326  'uid' => $this->getUid(),
327  'permissions' => array(
328  'read' => $this->checkActionPermission('read'),
329  'write' => $this->checkActionPermission('write'),
330  'delete' => $this->checkActionPermission('delete')
331  ),
332  'checksum' => $this->calculateChecksum()
333  );
334  foreach ($this->properties as $key => $value) {
335  $array[$key] = $value;
336  }
337  $stat = $this->getStorage()->getFileInfo($this);
338  foreach ($stat as $key => $value) {
339  $array[$key] = $value;
340  }
341  return $array;
342  }
343 
347  public function isMissing() {
348  return (bool) $this->getProperty('missing');
349  }
350 
354  public function setMissing($missing) {
355  $this->updateProperties(array('missing' => $missing ? 1 : 0));
356  }
357 
369  public function getPublicUrl($relativeToCurrentScript = FALSE) {
370  if ($this->isMissing() || $this->deleted) {
371  return FALSE;
372  } else {
373  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
374  }
375  }
376 
380  protected function getMetaDataRepository() {
381  return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
382  }
383 
387  protected function getFileIndexRepository() {
388  return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
389  }
390 
397  protected function getIndexerService() {
398  if ($this->indexerService === NULL) {
399  $this->indexerService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\Indexer', $this->storage);
400  }
401  return $this->indexerService;
402  }
403 
408  public function setIndexingInProgess($indexingState) {
409  $this->indexingInProgress = (boolean)$indexingState;
410  }
411 
417  public function _getPropertyRaw($key) {
418  return parent::getProperty($key);
419  }
420 }
getPublicUrl($relativeToCurrentScript=FALSE)
Definition: File.php:369
__construct(array $fileData, ResourceStorage $storage, $metaData=array())
Definition: File.php:63
_updateMetaDataProperties(array $properties)
Definition: File.php:251
updateProperties(array $properties)
Definition: File.php:210
checkActionPermission($action)
Definition: File.php:277
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
setIndexingInProgess($indexingState)
Definition: File.php:408
process($taskType, array $configuration)
Definition: File.php:306