‪TYPO3CMS  10.4
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  const ‪FILETYPE_UNKNOWN = 0;
72 
77  const ‪FILETYPE_TEXT = 1;
78 
83  const ‪FILETYPE_IMAGE = 2;
84 
89  const ‪FILETYPE_AUDIO = 3;
90 
95  const ‪FILETYPE_VIDEO = 4;
96 
101  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  $size = array_pop($this->‪getStorage()->getFileInfoByIdentifier($this->‪getIdentifier(), ['size']));
196  } else {
197  $size = $this->properties['size'];
198  }
199  return $size ? (int)$size : null;
200  }
201 
207  public function ‪getUid()
208  {
209  return (int)$this->‪getProperty('uid');
210  }
211 
218  public function ‪getSha1()
219  {
220  if ($this->deleted) {
221  throw new \RuntimeException('File has been deleted.', 1329821481);
222  }
223  return $this->‪getStorage()->‪hashFile($this, 'sha1');
224  }
225 
232  public function ‪getCreationTime()
233  {
234  if ($this->deleted) {
235  throw new \RuntimeException('File has been deleted.', 1329821487);
236  }
237  return (int)$this->‪getProperty('creation_date');
238  }
239 
246  public function ‪getModificationTime()
247  {
248  if ($this->deleted) {
249  throw new \RuntimeException('File has been deleted.', 1329821488);
250  }
251  return (int)$this->‪getProperty('modification_date');
252  }
253 
259  public function ‪getExtension()
260  {
261  $pathinfo = ‪PathUtility::pathinfo($this->‪getName());
262 
263  $extension = strtolower($pathinfo['extension'] ?? '');
264 
265  return $extension;
266  }
267 
273  public function ‪getMimeType()
274  {
275  return $this->properties['mime_type'] ?: array_pop($this->‪getStorage()->getFileInfoByIdentifier($this->‪getIdentifier(), ['mimetype']));
276  }
277 
291  public function ‪getType()
292  {
293  // this basically extracts the mimetype and guess the filetype based
294  // on the first part of the mimetype works for 99% of all cases, and
295  // we don't need to make an SQL statement like EXT:media does currently
296  if (!$this->properties['type']) {
297  $mimeType = $this->‪getMimeType();
298  [$fileType] = explode('/', $mimeType);
299  switch (strtolower($fileType)) {
300  case 'text':
301  $this->properties['type'] = ‪self::FILETYPE_TEXT;
302  break;
303  case 'image':
304  $this->properties['type'] = ‪self::FILETYPE_IMAGE;
305  break;
306  case 'audio':
307  $this->properties['type'] = ‪self::FILETYPE_AUDIO;
308  break;
309  case 'video':
310  $this->properties['type'] = ‪self::FILETYPE_VIDEO;
311  break;
312  case 'application':
313 
314  case 'software':
315  $this->properties['type'] = ‪self::FILETYPE_APPLICATION;
316  break;
317  default:
318  $this->properties['type'] = ‪self::FILETYPE_UNKNOWN;
319  }
320  }
321  return (int)$this->properties['type'];
322  }
323 
328  public function ‪isImage(): bool
329  {
330  return GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] ?? ''), $this->‪getExtension()) && $this->‪getSize() > 0;
331  }
332 
337  public function ‪isMediaFile(): bool
338  {
339  return GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] ?? ''), $this->‪getExtension()) && $this->‪getSize() > 0;
340  }
341 
347  public function ‪isTextFile(): bool
348  {
349  return GeneralUtility::inList(strtolower(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? ''), $this->‪getExtension());
350  }
351  /******************
352  * CONTENTS RELATED
353  ******************/
360  public function ‪getContents()
361  {
362  if ($this->deleted) {
363  throw new \RuntimeException('File has been deleted.', 1329821479);
364  }
365  return $this->‪getStorage()->‪getFileContents($this);
366  }
367 
376  public function ‪setContents($contents)
377  {
378  if ($this->deleted) {
379  throw new \RuntimeException('File has been deleted.', 1329821478);
380  }
381  $this->‪getStorage()->‪setFileContents($this, $contents);
382  return $this;
383  }
384 
385  /****************************************
386  * STORAGE AND MANAGEMENT RELATED METHODS
387  ****************************************/
388 
395  public function ‪getStorage()
396  {
397  if ($this->storage === null) {
398  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
399  }
400  return ‪$this->storage;
401  }
402 
410  public function ‪exists()
411  {
412  if ($this->deleted) {
413  return false;
414  }
415  return $this->storage->hasFile($this->‪getIdentifier());
416  }
417 
426  public function ‪setStorage(ResourceStorage ‪$storage)
427  {
428  $this->storage = ‪$storage;
429  $this->properties['storage'] = ‪$storage->‪getUid();
430  return $this;
431  }
432 
440  public function ‪setIdentifier(‪$identifier)
441  {
442  $this->identifier = ‪$identifier;
443  return $this;
444  }
445 
452  public function ‪getCombinedIdentifier()
453  {
454  if (!empty($this->properties['storage']) && ‪MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
455  $combinedIdentifier = $this->properties['storage'] . ':' . $this->‪getIdentifier();
456  } else {
457  $combinedIdentifier = $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
458  }
459  return $combinedIdentifier;
460  }
461 
467  public function delete()
468  {
469  // The storage will mark this file as deleted
470  $wasDeleted = $this->‪getStorage()->‪deleteFile($this);
471 
472  // Unset all properties when deleting the file, as they will be stale anyway
473  // This needs to happen AFTER the storage deleted the file, because the storage
474  // emits a signal, which passes the file object to the slots, which may need
475  // all file properties of the deleted file.
476  $this->properties = [];
477 
478  return $wasDeleted;
479  }
480 
485  public function ‪setDeleted()
486  {
487  $this->deleted = true;
488  }
489 
495  public function ‪isDeleted()
496  {
497  return ‪$this->deleted;
498  }
499 
508  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
509  {
510  if ($this->deleted) {
511  throw new \RuntimeException('File has been deleted.', 1329821482);
512  }
513  return $this->‪getStorage()->‪renameFile($this, $newName, $conflictMode);
514  }
515 
526  public function ‪copyTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
527  {
528  if ($this->deleted) {
529  throw new \RuntimeException('File has been deleted.', 1329821483);
530  }
531  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
532  }
533 
544  public function ‪moveTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
545  {
546  if ($this->deleted) {
547  throw new \RuntimeException('File has been deleted.', 1329821484);
548  }
549  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
550  }
551 
552  /*****************
553  * SPECIAL METHODS
554  *****************/
564  public function ‪getPublicUrl($relativeToCurrentScript = false)
565  {
566  if ($this->deleted) {
567  return null;
568  }
569  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
570  }
571 
582  public function ‪getForLocalProcessing($writable = true)
583  {
584  if ($this->deleted) {
585  throw new \RuntimeException('File has been deleted.', 1329821486);
586  }
587  return $this->‪getStorage()->‪getFileForLocalProcessing($this, $writable);
588  }
589 
590  /***********************
591  * INDEX RELATED METHODS
592  ***********************/
600  abstract public function ‪updateProperties(array ‪$properties);
601 
607  public function ‪getParentFolder()
608  {
609  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
610  }
611 }
‪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:286
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Resource\AbstractFile\$identifier
‪string $identifier
Definition: AbstractFile.php:49
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Resource\AbstractFile\hasProperty
‪bool hasProperty($key)
Definition: AbstractFile.php:107
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Core\Resource\AbstractFile\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: AbstractFile.php:559
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪string getIdentifier()
Definition: AbstractFile.php:141
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_VIDEO
‪const FILETYPE_VIDEO
Definition: AbstractFile.php:90
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1369
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\AbstractFile\getCreationTime
‪int getCreationTime()
Definition: AbstractFile.php:227
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:577
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\AbstractFile\exists
‪bool exists()
Definition: AbstractFile.php:405
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:268
‪TYPO3\CMS\Core\Resource\AbstractFile\rename
‪FileInterface rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:503
‪TYPO3\CMS\Core\Resource\AbstractFile\setStorage
‪File setStorage(ResourceStorage $storage)
Definition: AbstractFile.php:421
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2541
‪TYPO3\CMS\Core\Resource\AbstractFile\getModificationTime
‪int getModificationTime()
Definition: AbstractFile.php:241
‪TYPO3\CMS\Core\Resource\ResourceStorage\renameFile
‪FileInterface renameFile($file, $targetFileName, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2038
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFile
‪FileInterface copyFile(FileInterface $file, Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1918
‪TYPO3\CMS\Core\Resource\AbstractFile\setContents
‪File setContents($contents)
Definition: AbstractFile.php:371
‪TYPO3\CMS\Core\Resource\AbstractFile\getHashedIdentifier
‪string getHashedIdentifier()
Definition: AbstractFile.php:151
‪TYPO3\CMS\Core\Resource\AbstractFile\isImage
‪bool isImage()
Definition: AbstractFile.php:323
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1737
‪TYPO3\CMS\Core\Resource\AbstractFile\isDeleted
‪bool isDeleted()
Definition: AbstractFile.php:490
‪TYPO3\CMS\Core\Resource\AbstractFile\moveTo
‪File moveTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:539
‪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:1813
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:149
‪TYPO3\CMS\Core\Resource\AbstractFile\setDeleted
‪setDeleted()
Definition: AbstractFile.php:480
‪TYPO3\CMS\Core\Resource\ResourceStorage\moveFile
‪FileInterface moveFile($file, $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1971
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:26
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileForLocalProcessing
‪string getFileForLocalProcessing(FileInterface $fileObject, $writable=true)
Definition: ResourceStorage.php:1445
‪TYPO3\CMS\Core\Resource\AbstractFile\$deleted
‪bool $deleted
Definition: AbstractFile.php:61
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage $storage
Definition: AbstractFile.php:41
‪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:521
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:447
‪TYPO3\CMS\Core\Resource\AbstractFile\$name
‪string $name
Definition: AbstractFile.php:55
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:202
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string string[] pathinfo($path, $options=null)
Definition: PathUtility.php:208
‪TYPO3\CMS\Core\Resource\AbstractFile\getContents
‪string getContents()
Definition: AbstractFile.php:355
‪TYPO3\CMS\Core\Resource\AbstractFile\getParentFolder
‪FolderInterface getParentFolder()
Definition: AbstractFile.php:602
‪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:435
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:184
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:254
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪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:1320
‪TYPO3\CMS\Core\Resource\AbstractFile\getProperties
‪array getProperties()
Definition: AbstractFile.php:131
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪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:342
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:35
‪TYPO3\CMS\Core\Resource\AbstractFile\updateProperties
‪updateProperties(array $properties)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\AbstractFile\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: AbstractFile.php:173
‪TYPO3\CMS\Core\Resource\AbstractFile\getSha1
‪string getSha1()
Definition: AbstractFile.php:213
‪TYPO3\CMS\Core\Resource\AbstractFile\isMediaFile
‪bool isMediaFile()
Definition: AbstractFile.php:332
‪TYPO3\CMS\Core\Resource\ResourceStorage\deleteFile
‪bool deleteFile($fileObject)
Definition: ResourceStorage.php:1864
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:390