‪TYPO3CMS  ‪main
AbstractFile.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
25 abstract class ‪AbstractFile implements ‪FileInterface
26 {
36  protected ‪$properties;
37 
43  protected ‪$storage;
44 
52  protected ‪$identifier;
53 
59  protected ‪$name;
60 
66  protected ‪$deleted = false;
67 
71  public const ‪FILETYPE_UNKNOWN = 0;
72 
77  public const ‪FILETYPE_TEXT = 1;
78 
83  public const ‪FILETYPE_IMAGE = 2;
84 
89  public const ‪FILETYPE_AUDIO = 3;
90 
95  public const ‪FILETYPE_VIDEO = 4;
96 
101  public const ‪FILETYPE_APPLICATION = 5;
102 
103  /******************
104  * VARIOUS FILE PROPERTY GETTERS
105  ******************/
112  public function ‪hasProperty($key)
113  {
114  return array_key_exists($key, $this->properties);
115  }
116 
123  public function ‪getProperty($key)
124  {
125  if ($this->‪hasProperty($key)) {
126  return $this->properties[$key];
127  }
128  return null;
129  }
130 
136  public function ‪getProperties()
137  {
138  return ‪$this->properties;
139  }
140 
146  public function ‪getIdentifier()
147  {
148  return ‪$this->identifier;
149  }
150 
156  public function ‪getHashedIdentifier()
157  {
158  return $this->properties['identifier_hash'];
159  }
160 
166  public function ‪getName()
167  {
168  // Do not check if file has been deleted because we might need the
169  // name for undeleting it.
170  return ‪$this->name;
171  }
172 
178  public function ‪getNameWithoutExtension()
179  {
180  return ‪PathUtility::pathinfo($this->‪getName(), PATHINFO_FILENAME);
181  }
182 
189  public function ‪getSize()
190  {
191  if ($this->deleted) {
192  throw new \RuntimeException('File has been deleted.', 1329821480);
193  }
194  if (empty($this->properties['size'])) {
195  $fileInfo = $this->‪getStorage()->‪getFileInfoByIdentifier($this->‪getIdentifier(), ['size']);
196  $size = array_pop($fileInfo);
197  } else {
198  $size = $this->properties['size'];
199  }
200  return $size ? (int)$size : null;
201  }
202 
208  public function ‪getUid()
209  {
210  return (int)$this->‪getProperty('uid');
211  }
212 
219  public function ‪getSha1()
220  {
221  if ($this->deleted) {
222  throw new \RuntimeException('File has been deleted.', 1329821481);
223  }
224  return $this->‪getStorage()->‪hashFile($this, 'sha1');
225  }
226 
233  public function ‪getCreationTime()
234  {
235  if ($this->deleted) {
236  throw new \RuntimeException('File has been deleted.', 1329821487);
237  }
238  return (int)$this->‪getProperty('creation_date');
239  }
240 
247  public function ‪getModificationTime()
248  {
249  if ($this->deleted) {
250  throw new \RuntimeException('File has been deleted.', 1329821488);
251  }
252  return (int)$this->‪getProperty('modification_date');
253  }
254 
260  public function ‪getExtension()
261  {
262  $pathinfo = ‪PathUtility::pathinfo($this->‪getName());
263 
264  $extension = strtolower($pathinfo['extension'] ?? '');
265 
266  return $extension;
267  }
268 
274  public function ‪getMimeType()
275  {
276  if ($this->properties['mime_type'] ?? false) {
277  return $this->properties['mime_type'];
278  }
279  $fileInfo = $this->‪getStorage()->‪getFileInfoByIdentifier($this->‪getIdentifier(), ['mimetype']);
280  return array_pop($fileInfo);
281  }
282 
296  public function ‪getType()
297  {
298  // this basically extracts the mimetype and guess the filetype based
299  // on the first part of the mimetype works for 99% of all cases, and
300  // we don't need to make an SQL statement like EXT:media does currently
301  if (!($this->properties['type'] ?? false)) {
302  $mimeType = $this->‪getMimeType();
303  [$fileType] = explode('/', $mimeType);
304  switch (strtolower($fileType)) {
305  case 'text':
306  $this->properties['type'] = ‪self::FILETYPE_TEXT;
307  break;
308  case 'image':
309  $this->properties['type'] = ‪self::FILETYPE_IMAGE;
310  break;
311  case 'audio':
312  $this->properties['type'] = ‪self::FILETYPE_AUDIO;
313  break;
314  case 'video':
315  $this->properties['type'] = ‪self::FILETYPE_VIDEO;
316  break;
317  case 'application':
318 
319  case 'software':
320  $this->properties['type'] = ‪self::FILETYPE_APPLICATION;
321  break;
322  default:
323  $this->properties['type'] = ‪self::FILETYPE_UNKNOWN;
324  }
325  }
326  return (int)$this->properties['type'];
327  }
328 
333  public function ‪isImage(): bool
334  {
335  return ‪GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] ?? ''), $this->‪getExtension()) && $this->‪getSize() > 0;
336  }
337 
342  public function ‪isMediaFile(): bool
343  {
344  return ‪GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] ?? ''), $this->‪getExtension()) && $this->‪getSize() > 0;
345  }
346 
352  public function ‪isTextFile(): bool
353  {
354  return ‪GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? ''), $this->‪getExtension());
355  }
356  /******************
357  * CONTENTS RELATED
358  ******************/
365  public function ‪getContents()
366  {
367  if ($this->deleted) {
368  throw new \RuntimeException('File has been deleted.', 1329821479);
369  }
370  return $this->‪getStorage()->‪getFileContents($this);
371  }
372 
381  public function ‪setContents($contents)
382  {
383  if ($this->deleted) {
384  throw new \RuntimeException('File has been deleted.', 1329821478);
385  }
386  $this->‪getStorage()->‪setFileContents($this, $contents);
387  return $this;
388  }
389 
390  /****************************************
391  * STORAGE AND MANAGEMENT RELATED METHODS
392  ****************************************/
393 
400  public function ‪getStorage()
401  {
402  if ($this->storage === null) {
403  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
404  }
405  return ‪$this->storage;
406  }
407 
415  public function ‪exists()
416  {
417  if ($this->deleted) {
418  return false;
419  }
420  return $this->storage->hasFile($this->‪getIdentifier());
421  }
422 
430  public function ‪setStorage(ResourceStorage ‪$storage)
431  {
432  $this->storage = ‪$storage;
433  $this->properties['storage'] = ‪$storage->‪getUid();
434  return $this;
435  }
436 
444  public function ‪setIdentifier(‪$identifier)
445  {
446  $this->identifier = ‪$identifier;
447  return $this;
448  }
449 
456  public function ‪getCombinedIdentifier()
457  {
458  if (!empty($this->properties['storage']) && ‪MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
459  $combinedIdentifier = $this->properties['storage'] . ':' . $this->‪getIdentifier();
460  } else {
461  $combinedIdentifier = $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
462  }
463  return $combinedIdentifier;
464  }
465 
471  public function delete()
472  {
473  // The storage will mark this file as deleted
474  $wasDeleted = $this->‪getStorage()->‪deleteFile($this);
475 
476  // Unset all properties when deleting the file, as they will be stale anyway
477  // This needs to happen AFTER the storage deleted the file, because the storage
478  // emits a signal, which passes the file object to the slots, which may need
479  // all file properties of the deleted file.
480  $this->properties = [];
481 
482  return $wasDeleted;
483  }
484 
489  public function ‪setDeleted()
490  {
491  $this->deleted = true;
492  }
493 
499  public function ‪isDeleted()
500  {
501  return ‪$this->deleted;
502  }
503 
512  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
513  {
514  if ($this->deleted) {
515  throw new \RuntimeException('File has been deleted.', 1329821482);
516  }
517  return $this->‪getStorage()->‪renameFile($this, $newName, $conflictMode);
518  }
519 
530  public function ‪copyTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
531  {
532  if ($this->deleted) {
533  throw new \RuntimeException('File has been deleted.', 1329821483);
534  }
535  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
536  }
537 
548  public function ‪moveTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
549  {
550  if ($this->deleted) {
551  throw new \RuntimeException('File has been deleted.', 1329821484);
552  }
553  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
554  }
555 
556  /*****************
557  * SPECIAL METHODS
558  *****************/
567  public function ‪getPublicUrl()
568  {
569  if ($this->deleted) {
570  return null;
571  }
572  return $this->‪getStorage()->‪getPublicUrl($this);
573  }
574 
585  public function ‪getForLocalProcessing($writable = true)
586  {
587  if ($this->deleted) {
588  throw new \RuntimeException('File has been deleted.', 1329821486);
589  }
590  return $this->‪getStorage()->‪getFileForLocalProcessing($this, $writable);
591  }
592 
593  /***********************
594  * INDEX RELATED METHODS
595  ***********************/
601  abstract public function ‪updateProperties(array ‪$properties);
602 
608  public function ‪getParentFolder()
609  {
610  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
611  }
612 }
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_UNKNOWN
‪const FILETYPE_UNKNOWN
Definition: AbstractFile.php:66
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:291
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Resource\AbstractFile\$identifier
‪string $identifier
Definition: AbstractFile.php:49
‪TYPO3\CMS\Core\Resource\AbstractFile\hasProperty
‪bool hasProperty($key)
Definition: AbstractFile.php:107
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:328
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪string getIdentifier()
Definition: AbstractFile.php:141
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileInfoByIdentifier
‪array getFileInfoByIdentifier($identifier, array $propertiesToExtract=[])
Definition: ResourceStorage.php:1455
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_VIDEO
‪const FILETYPE_VIDEO
Definition: AbstractFile.php:90
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\AbstractFile\getCreationTime
‪int getCreationTime()
Definition: AbstractFile.php:228
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:580
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\AbstractFile\exists
‪bool exists()
Definition: AbstractFile.php:410
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:269
‪TYPO3\CMS\Core\Resource\AbstractFile\rename
‪FileInterface rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:507
‪TYPO3\CMS\Core\Resource\AbstractFile\setStorage
‪File setStorage(ResourceStorage $storage)
Definition: AbstractFile.php:425
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2494
‪TYPO3\CMS\Core\Resource\AbstractFile\getModificationTime
‪int getModificationTime()
Definition: AbstractFile.php:242
‪TYPO3\CMS\Core\Resource\ResourceStorage\renameFile
‪FileInterface renameFile($file, $targetFileName, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1995
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFile
‪FileInterface copyFile(FileInterface $file, Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1875
‪TYPO3\CMS\Core\Resource\AbstractFile\setContents
‪File setContents($contents)
Definition: AbstractFile.php:376
‪TYPO3\CMS\Core\Resource\AbstractFile\getHashedIdentifier
‪string getHashedIdentifier()
Definition: AbstractFile.php:151
‪TYPO3\CMS\Core\Resource\AbstractFile\isImage
‪bool isImage()
Definition: AbstractFile.php:328
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1699
‪TYPO3\CMS\Core\Resource\AbstractFile\isDeleted
‪bool isDeleted()
Definition: AbstractFile.php:494
‪TYPO3\CMS\Core\Resource\AbstractFile\moveTo
‪File moveTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:543
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:78
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1772
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:148
‪TYPO3\CMS\Core\Resource\AbstractFile\setDeleted
‪setDeleted()
Definition: AbstractFile.php:484
‪TYPO3\CMS\Core\Resource\ResourceStorage\moveFile
‪FileInterface moveFile($file, $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1928
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:26
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileForLocalProcessing
‪string getFileForLocalProcessing(FileInterface $fileObject, $writable=true)
Definition: ResourceStorage.php:1392
‪TYPO3\CMS\Core\Resource\AbstractFile\getPublicUrl
‪string null getPublicUrl()
Definition: AbstractFile.php:562
‪TYPO3\CMS\Core\Resource\AbstractFile\$deleted
‪bool $deleted
Definition: AbstractFile.php:61
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:32
‪TYPO3\CMS\Core\Resource\AbstractFile\copyTo
‪File copyTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:525
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:451
‪TYPO3\CMS\Core\Resource\AbstractFile\$name
‪string $name
Definition: AbstractFile.php:55
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:203
‪TYPO3\CMS\Core\Resource\AbstractFile\getContents
‪string getContents()
Definition: AbstractFile.php:360
‪TYPO3\CMS\Core\Resource\AbstractFile\getParentFolder
‪FolderInterface getParentFolder()
Definition: AbstractFile.php:603
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_AUDIO
‪const FILETYPE_AUDIO
Definition: AbstractFile.php:84
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_TEXT
‪const FILETYPE_TEXT
Definition: AbstractFile.php:72
‪TYPO3\CMS\Core\Resource\AbstractFile\setIdentifier
‪File setIdentifier($identifier)
Definition: AbstractFile.php:439
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:127
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:184
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:255
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\AbstractFile\getProperty
‪mixed getProperty($key)
Definition: AbstractFile.php:118
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Core\Resource\ResourceStorage\hashFile
‪string hashFile(FileInterface $fileObject, $hash)
Definition: ResourceStorage.php:1279
‪TYPO3\CMS\Core\Resource\AbstractFile\getProperties
‪array getProperties()
Definition: AbstractFile.php:131
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_APPLICATION
‪const FILETYPE_APPLICATION
Definition: AbstractFile.php:96
‪TYPO3\CMS\Core\Resource\AbstractFile\isTextFile
‪bool isTextFile()
Definition: AbstractFile.php:347
‪TYPO3\CMS\Core\Utility\GeneralUtility\inList
‪static bool inList($list, $item)
Definition: GeneralUtility.php:532
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:35
‪TYPO3\CMS\Core\Resource\AbstractFile\updateProperties
‪updateProperties(array $properties)
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage null $storage
Definition: AbstractFile.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Resource\AbstractFile\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: AbstractFile.php:173
‪TYPO3\CMS\Core\Resource\AbstractFile\getSha1
‪string getSha1()
Definition: AbstractFile.php:214
‪TYPO3\CMS\Core\Resource\AbstractFile\isMediaFile
‪bool isMediaFile()
Definition: AbstractFile.php:337
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject)
Definition: ResourceStorage.php:1327
‪TYPO3\CMS\Core\Resource\ResourceStorage\deleteFile
‪bool deleteFile($fileObject)
Definition: ResourceStorage.php:1823
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string string[] pathinfo(string $path, int $options=PATHINFO_ALL)
Definition: PathUtility.php:270
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395