‪TYPO3CMS  9.5
AbstractFile.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 
19 
23 abstract class ‪AbstractFile implements ‪FileInterface
24 {
34  protected ‪$properties;
35 
41  protected ‪$storage;
42 
50  protected ‪$identifier;
51 
57  protected ‪$name;
58 
64  protected ‪$deleted = false;
65 
69  const ‪FILETYPE_UNKNOWN = 0;
70 
75  const ‪FILETYPE_TEXT = 1;
76 
81  const ‪FILETYPE_IMAGE = 2;
82 
87  const ‪FILETYPE_AUDIO = 3;
88 
93  const ‪FILETYPE_VIDEO = 4;
94 
99  const ‪FILETYPE_APPLICATION = 5;
100 
101  /******************
102  * VARIOUS FILE PROPERTY GETTERS
103  ******************/
110  public function ‪hasProperty($key)
111  {
112  return array_key_exists($key, $this->properties);
113  }
114 
121  public function ‪getProperty($key)
122  {
123  if ($this->‪hasProperty($key)) {
124  return $this->properties[$key];
125  }
126  return null;
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  if (empty($this->properties['size'])) {
193  $size = array_pop($this->‪getStorage()->getFileInfoByIdentifier($this->‪getIdentifier(), ['size']));
194  } else {
195  $size = $this->properties['size'];
196  }
197  return $size ? (int)$size : null;
198  }
199 
205  public function ‪getUid()
206  {
207  return (int)$this->‪getProperty('uid');
208  }
209 
216  public function ‪getSha1()
217  {
218  if ($this->deleted) {
219  throw new \RuntimeException('File has been deleted.', 1329821481);
220  }
221  return $this->‪getStorage()->‪hashFile($this, 'sha1');
222  }
223 
230  public function ‪getCreationTime()
231  {
232  if ($this->deleted) {
233  throw new \RuntimeException('File has been deleted.', 1329821487);
234  }
235  return (int)$this->‪getProperty('creation_date');
236  }
237 
244  public function ‪getModificationTime()
245  {
246  if ($this->deleted) {
247  throw new \RuntimeException('File has been deleted.', 1329821488);
248  }
249  return (int)$this->‪getProperty('modification_date');
250  }
251 
257  public function ‪getExtension()
258  {
259  $pathinfo = ‪PathUtility::pathinfo($this->‪getName());
260 
261  $extension = strtolower($pathinfo['extension'] ?? '');
262 
263  return $extension;
264  }
265 
271  public function ‪getMimeType()
272  {
273  return $this->properties['mime_type'] ?: array_pop($this->‪getStorage()->getFileInfoByIdentifier($this->‪getIdentifier(), ['mimetype']));
274  }
275 
289  public function ‪getType()
290  {
291  // this basically extracts the mimetype and guess the filetype based
292  // on the first part of the mimetype works for 99% of all cases, and
293  // we don't need to make an SQL statement like EXT:media does currently
294  if (!$this->properties['type']) {
295  $mimeType = $this->‪getMimeType();
296  list($fileType) = explode('/', $mimeType);
297  switch (strtolower($fileType)) {
298  case 'text':
299  $this->properties['type'] = ‪self::FILETYPE_TEXT;
300  break;
301  case 'image':
302  $this->properties['type'] = ‪self::FILETYPE_IMAGE;
303  break;
304  case 'audio':
305  $this->properties['type'] = ‪self::FILETYPE_AUDIO;
306  break;
307  case 'video':
308  $this->properties['type'] = ‪self::FILETYPE_VIDEO;
309  break;
310  case 'application':
311 
312  case 'software':
313  $this->properties['type'] = ‪self::FILETYPE_APPLICATION;
314  break;
315  default:
316  $this->properties['type'] = ‪self::FILETYPE_UNKNOWN;
317  }
318  }
319  return (int)$this->properties['type'];
320  }
321 
322  /******************
323  * CONTENTS RELATED
324  ******************/
331  public function ‪getContents()
332  {
333  if ($this->deleted) {
334  throw new \RuntimeException('File has been deleted.', 1329821479);
335  }
336  return $this->‪getStorage()->‪getFileContents($this);
337  }
338 
347  public function ‪setContents($contents)
348  {
349  if ($this->deleted) {
350  throw new \RuntimeException('File has been deleted.', 1329821478);
351  }
352  $this->‪getStorage()->‪setFileContents($this, $contents);
353  return $this;
354  }
355 
356  /****************************************
357  * STORAGE AND MANAGEMENT RELATED METHDOS
358  ****************************************/
359 
366  public function ‪getStorage()
367  {
368  if ($this->storage === null) {
369  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
370  }
371  return ‪$this->storage;
372  }
373 
381  public function ‪exists()
382  {
383  if ($this->deleted) {
384  return false;
385  }
386  return $this->storage->hasFile($this->‪getIdentifier());
387  }
388 
397  public function ‪setStorage(ResourceStorage ‪$storage)
398  {
399  $this->storage = ‪$storage;
400  $this->properties['storage'] = ‪$storage->‪getUid();
401  return $this;
402  }
403 
411  public function ‪setIdentifier(‪$identifier)
412  {
413  $this->identifier = ‪$identifier;
414  return $this;
415  }
416 
423  public function ‪getCombinedIdentifier()
424  {
425  if (!empty($this->properties['storage']) && ‪MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
426  $combinedIdentifier = $this->properties['storage'] . ':' . $this->‪getIdentifier();
427  } else {
428  $combinedIdentifier = $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
429  }
430  return $combinedIdentifier;
431  }
432 
438  public function delete()
439  {
440  // The storage will mark this file as deleted
441  $wasDeleted = $this->‪getStorage()->‪deleteFile($this);
442 
443  // Unset all properties when deleting the file, as they will be stale anyway
444  // This needs to happen AFTER the storage deleted the file, because the storage
445  // emits a signal, which passes the file object to the slots, which may need
446  // all file properties of the deleted file.
447  $this->properties = [];
448 
449  return $wasDeleted;
450  }
451 
456  public function ‪setDeleted()
457  {
458  $this->deleted = true;
459  }
460 
466  public function ‪isDeleted()
467  {
468  return ‪$this->deleted;
469  }
470 
479  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
480  {
481  if ($this->deleted) {
482  throw new \RuntimeException('File has been deleted.', 1329821482);
483  }
484  return $this->‪getStorage()->‪renameFile($this, $newName, $conflictMode);
485  }
486 
497  public function ‪copyTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
498  {
499  if ($this->deleted) {
500  throw new \RuntimeException('File has been deleted.', 1329821483);
501  }
502  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
503  }
504 
515  public function ‪moveTo(Folder $targetFolder, $targetFileName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
516  {
517  if ($this->deleted) {
518  throw new \RuntimeException('File has been deleted.', 1329821484);
519  }
520  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
521  }
522 
523  /*****************
524  * SPECIAL METHODS
525  *****************/
535  public function ‪getPublicUrl($relativeToCurrentScript = false)
536  {
537  if ($this->deleted) {
538  return null;
539  }
540  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
541  }
542 
553  public function ‪getForLocalProcessing($writable = true)
554  {
555  if ($this->deleted) {
556  throw new \RuntimeException('File has been deleted.', 1329821486);
557  }
558  return $this->‪getStorage()->‪getFileForLocalProcessing($this, $writable);
559  }
560 
561  /***********************
562  * INDEX RELATED METHODS
563  ***********************/
571  abstract public function ‪updateProperties(array ‪$properties);
572 
578  public function ‪getParentFolder()
579  {
580  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
581  }
582 }
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_UNKNOWN
‪const FILETYPE_UNKNOWN
Definition: AbstractFile.php:64
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:284
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Resource\AbstractFile\$identifier
‪string $identifier
Definition: AbstractFile.php:47
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\Resource\AbstractFile\hasProperty
‪bool hasProperty($key)
Definition: AbstractFile.php:105
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:271
‪TYPO3\CMS\Core\Resource\AbstractFile\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: AbstractFile.php:530
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪string getIdentifier()
Definition: AbstractFile.php:139
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_VIDEO
‪const FILETYPE_VIDEO
Definition: AbstractFile.php:88
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1317
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\AbstractFile\getCreationTime
‪int getCreationTime()
Definition: AbstractFile.php:225
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:548
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:159
‪TYPO3\CMS\Core\Resource\AbstractFile\exists
‪bool exists()
Definition: AbstractFile.php:376
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:266
‪TYPO3\CMS\Core\Resource\AbstractFile\rename
‪FileInterface rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:474
‪TYPO3\CMS\Core\Resource\AbstractFile\setStorage
‪File setStorage(ResourceStorage $storage)
Definition: AbstractFile.php:392
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2462
‪TYPO3\CMS\Core\Resource\AbstractFile\getModificationTime
‪int getModificationTime()
Definition: AbstractFile.php:239
‪TYPO3\CMS\Core\Resource\ResourceStorage\renameFile
‪FileInterface renameFile($file, $targetFileName, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1987
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFile
‪FileInterface copyFile(FileInterface $file, Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1878
‪TYPO3\CMS\Core\Resource\AbstractFile\setContents
‪File setContents($contents)
Definition: AbstractFile.php:342
‪TYPO3\CMS\Core\Resource\AbstractFile\getHashedIdentifier
‪string getHashedIdentifier()
Definition: AbstractFile.php:149
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileContents
‪string getFileContents($file)
Definition: ResourceStorage.php:1672
‪TYPO3\CMS\Core\Resource\AbstractFile\isDeleted
‪bool isDeleted()
Definition: AbstractFile.php:461
‪TYPO3\CMS\Core\Resource\AbstractFile\moveTo
‪File moveTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:510
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string array pathinfo($path, $options=null)
Definition: PathUtility.php:207
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:76
‪TYPO3\CMS\Core\Resource\ResourceStorage\setFileContents
‪int setFileContents(AbstractFile $file, $contents)
Definition: ResourceStorage.php:1785
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:146
‪TYPO3\CMS\Core\Resource\AbstractFile\setDeleted
‪setDeleted()
Definition: AbstractFile.php:451
‪TYPO3\CMS\Core\Resource\ResourceStorage\moveFile
‪FileInterface moveFile($file, $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:1924
‪TYPO3\CMS\Core\Resource\AbstractFile
Definition: AbstractFile.php:24
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFileForLocalProcessing
‪string getFileForLocalProcessing(FileInterface $fileObject, $writable=true)
Definition: ResourceStorage.php:1393
‪TYPO3\CMS\Core\Resource\AbstractFile\$deleted
‪bool $deleted
Definition: AbstractFile.php:59
‪TYPO3\CMS\Core\Resource\AbstractFile\$storage
‪ResourceStorage $storage
Definition: AbstractFile.php:39
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:31
‪TYPO3\CMS\Core\Resource\AbstractFile\copyTo
‪File copyTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: AbstractFile.php:492
‪TYPO3\CMS\Core\Resource\AbstractFile\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: AbstractFile.php:418
‪TYPO3\CMS\Core\Resource\AbstractFile\$name
‪string $name
Definition: AbstractFile.php:53
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Core\Resource\AbstractFile\getContents
‪string getContents()
Definition: AbstractFile.php:326
‪TYPO3\CMS\Core\Resource\AbstractFile\getParentFolder
‪FolderInterface getParentFolder()
Definition: AbstractFile.php:573
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_AUDIO
‪const FILETYPE_AUDIO
Definition: AbstractFile.php:82
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_TEXT
‪const FILETYPE_TEXT
Definition: AbstractFile.php:70
‪TYPO3\CMS\Core\Resource\AbstractFile\setIdentifier
‪File setIdentifier($identifier)
Definition: AbstractFile.php:406
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:182
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:252
‪TYPO3\CMS\Core\Resource\AbstractFile\getProperty
‪mixed getProperty($key)
Definition: AbstractFile.php:116
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:21
‪TYPO3\CMS\Core\Resource\ResourceStorage\hashFile
‪string hashFile(FileInterface $fileObject, $hash)
Definition: ResourceStorage.php:1268
‪TYPO3\CMS\Core\Resource\AbstractFile\getProperties
‪array getProperties()
Definition: AbstractFile.php:129
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_APPLICATION
‪const FILETYPE_APPLICATION
Definition: AbstractFile.php:94
‪TYPO3\CMS\Core\Resource\AbstractFile\$properties
‪array $properties
Definition: AbstractFile.php:33
‪TYPO3\CMS\Core\Resource\AbstractFile\updateProperties
‪updateProperties(array $properties)
‪TYPO3\CMS\Core\Resource\AbstractFile\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: AbstractFile.php:171
‪TYPO3\CMS\Core\Resource\AbstractFile\getSha1
‪string getSha1()
Definition: AbstractFile.php:211
‪TYPO3\CMS\Core\Resource\ResourceStorage\deleteFile
‪bool deleteFile($fileObject)
Definition: ResourceStorage.php:1828
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:361