TYPO3  7.6
AbstractFile.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 
22 abstract class AbstractFile implements FileInterface
23 {
33  protected $properties;
34 
40  protected $storage = null;
41 
49  protected $identifier;
50 
56  protected $name;
57 
63  protected $deleted = false;
64 
68  const FILETYPE_UNKNOWN = 0;
69 
74  const FILETYPE_TEXT = 1;
75 
80  const FILETYPE_IMAGE = 2;
81 
86  const FILETYPE_AUDIO = 3;
87 
92  const FILETYPE_VIDEO = 4;
93 
99 
100  /******************
101  * VARIOUS FILE PROPERTY GETTERS
102  ******************/
109  public function hasProperty($key)
110  {
111  return array_key_exists($key, $this->properties);
112  }
113 
120  public function getProperty($key)
121  {
122  if ($this->hasProperty($key)) {
123  return $this->properties[$key];
124  } else {
125  return null;
126  }
127  }
128 
134  public function getProperties()
135  {
136  return $this->properties;
137  }
138 
144  public function getIdentifier()
145  {
146  return $this->identifier;
147  }
148 
154  public function getHashedIdentifier()
155  {
156  return $this->properties['identifier_hash'];
157  }
158 
164  public function getName()
165  {
166  // Do not check if file has been deleted because we might need the
167  // name for undeleting it.
168  return $this->name;
169  }
170 
176  public function getNameWithoutExtension()
177  {
178  return PathUtility::pathinfo($this->getName(), PATHINFO_FILENAME);
179  }
180 
187  public function getSize()
188  {
189  if ($this->deleted) {
190  throw new \RuntimeException('File has been deleted.', 1329821480);
191  }
192  return $this->properties['size'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), array('size')));
193  }
194 
200  public function getUid()
201  {
202  return $this->getProperty('uid');
203  }
204 
211  public function getSha1()
212  {
213  if ($this->deleted) {
214  throw new \RuntimeException('File has been deleted.', 1329821481);
215  }
216  return $this->getStorage()->hashFile($this, 'sha1');
217  }
218 
225  public function getCreationTime()
226  {
227  if ($this->deleted) {
228  throw new \RuntimeException('File has been deleted.', 1329821487);
229  }
230  return $this->getProperty('creation_date');
231  }
232 
239  public function getModificationTime()
240  {
241  if ($this->deleted) {
242  throw new \RuntimeException('File has been deleted.', 1329821488);
243  }
244  return $this->getProperty('modification_date');
245  }
246 
252  public function getExtension()
253  {
254  $pathinfo = PathUtility::pathinfo($this->getName());
255 
256  $extension = strtolower($pathinfo['extension']);
257 
258  return $extension;
259  }
260 
266  public function getMimeType()
267  {
268  return $this->properties['mime_type'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), array('mimetype')));
269  }
270 
284  public function getType()
285  {
286  // this basically extracts the mimetype and guess the filetype based
287  // on the first part of the mimetype works for 99% of all cases, and
288  // we don't need to make an SQL statement like EXT:media does currently
289  if (!$this->properties['type']) {
290  $mimeType = $this->getMimeType();
291  list($fileType) = explode('/', $mimeType);
292  switch (strtolower($fileType)) {
293  case 'text':
294  $this->properties['type'] = self::FILETYPE_TEXT;
295  break;
296  case 'image':
297  $this->properties['type'] = self::FILETYPE_IMAGE;
298  break;
299  case 'audio':
300  $this->properties['type'] = self::FILETYPE_AUDIO;
301  break;
302  case 'video':
303  $this->properties['type'] = self::FILETYPE_VIDEO;
304  break;
305  case 'application':
306 
307  case 'software':
308  $this->properties['type'] = self::FILETYPE_APPLICATION;
309  break;
310  default:
311  $this->properties['type'] = self::FILETYPE_UNKNOWN;
312  }
313  }
314  return (int)$this->properties['type'];
315  }
316 
317  /******************
318  * CONTENTS RELATED
319  ******************/
326  public function getContents()
327  {
328  if ($this->deleted) {
329  throw new \RuntimeException('File has been deleted.', 1329821479);
330  }
331  return $this->getStorage()->getFileContents($this);
332  }
333 
342  public function setContents($contents)
343  {
344  if ($this->deleted) {
345  throw new \RuntimeException('File has been deleted.', 1329821478);
346  }
347  $this->getStorage()->setFileContents($this, $contents);
348  return $this;
349  }
350 
351  /****************************************
352  * STORAGE AND MANAGEMENT RELATED METHDOS
353  ****************************************/
354 
361  public function getStorage()
362  {
363  if ($this->storage === null) {
364  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
365  }
366  return $this->storage;
367  }
368 
376  public function exists()
377  {
378  if ($this->deleted) {
379  return false;
380  }
381  return $this->storage->hasFile($this->getIdentifier());
382  }
383 
393  {
394  $this->storage = $storage;
395  $this->properties['storage'] = $storage->getUid();
396  return $this;
397  }
398 
406  public function setIdentifier($identifier)
407  {
408  $this->identifier = $identifier;
409  return $this;
410  }
411 
418  public function getCombinedIdentifier()
419  {
420  if (is_array($this->properties) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
421  $combinedIdentifier = $this->properties['storage'] . ':' . $this->getIdentifier();
422  } else {
423  $combinedIdentifier = $this->getStorage()->getUid() . ':' . $this->getIdentifier();
424  }
425  return $combinedIdentifier;
426  }
427 
433  public function delete()
434  {
435  // The storage will mark this file as deleted
436  return $this->getStorage()->deleteFile($this);
437  }
438 
445  public function setDeleted()
446  {
447  $this->deleted = true;
448  }
449 
455  public function isDeleted()
456  {
457  return $this->deleted;
458  }
459 
468  public function rename($newName)
469  {
470  if ($this->deleted) {
471  throw new \RuntimeException('File has been deleted.', 1329821482);
472  }
473  return $this->getStorage()->renameFile($this, $newName);
474  }
475 
486  public function copyTo(Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
487  {
488  if ($this->deleted) {
489  throw new \RuntimeException('File has been deleted.', 1329821483);
490  }
491  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
492  }
493 
504  public function moveTo(Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
505  {
506  if ($this->deleted) {
507  throw new \RuntimeException('File has been deleted.', 1329821484);
508  }
509  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
510  }
511 
512  /*****************
513  * SPECIAL METHODS
514  *****************/
524  public function getPublicUrl($relativeToCurrentScript = false)
525  {
526  if ($this->deleted) {
527  return null;
528  } else {
529  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
530  }
531  }
532 
543  public function getForLocalProcessing($writable = true)
544  {
545  if ($this->deleted) {
546  throw new \RuntimeException('File has been deleted.', 1329821486);
547  }
548  return $this->getStorage()->getFileForLocalProcessing($this, $writable);
549  }
550 
551  /***********************
552  * INDEX RELATED METHODS
553  ***********************/
561  abstract public function updateProperties(array $properties);
562 
568  public function getParentFolder()
569  {
570  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
571  }
572 }