TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
19 
23 abstract class AbstractFile implements FileInterface
24 {
34  protected $properties;
35 
41  protected $storage = null;
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 
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  } else {
126  return null;
127  }
128  }
129 
135  public function getProperties()
136  {
137  return $this->properties;
138  }
139 
145  public function getIdentifier()
146  {
147  return $this->identifier;
148  }
149 
155  public function getHashedIdentifier()
156  {
157  return $this->properties['identifier_hash'];
158  }
159 
165  public function getName()
166  {
167  // Do not check if file has been deleted because we might need the
168  // name for undeleting it.
169  return $this->name;
170  }
171 
177  public function getNameWithoutExtension()
178  {
179  return PathUtility::pathinfo($this->getName(), PATHINFO_FILENAME);
180  }
181 
188  public function getSize()
189  {
190  if ($this->deleted) {
191  throw new \RuntimeException('File has been deleted.', 1329821480);
192  }
193  if (empty($this->properties['size'])) {
194  $size = array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), ['size']));
195  } else {
196  $size = $this->properties['size'];
197  }
198  return $size ? (int)$size : null;
199  }
200 
206  public function getUid()
207  {
208  return (int)$this->getProperty('uid');
209  }
210 
217  public function getSha1()
218  {
219  if ($this->deleted) {
220  throw new \RuntimeException('File has been deleted.', 1329821481);
221  }
222  return $this->getStorage()->hashFile($this, 'sha1');
223  }
224 
231  public function getCreationTime()
232  {
233  if ($this->deleted) {
234  throw new \RuntimeException('File has been deleted.', 1329821487);
235  }
236  return (int)$this->getProperty('creation_date');
237  }
238 
245  public function getModificationTime()
246  {
247  if ($this->deleted) {
248  throw new \RuntimeException('File has been deleted.', 1329821488);
249  }
250  return (int)$this->getProperty('modification_date');
251  }
252 
258  public function getExtension()
259  {
260  $pathinfo = PathUtility::pathinfo($this->getName());
261 
262  $extension = strtolower($pathinfo['extension']);
263 
264  return $extension;
265  }
266 
272  public function getMimeType()
273  {
274  return $this->properties['mime_type'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), ['mimetype']));
275  }
276 
290  public function getType()
291  {
292  // this basically extracts the mimetype and guess the filetype based
293  // on the first part of the mimetype works for 99% of all cases, and
294  // we don't need to make an SQL statement like EXT:media does currently
295  if (!$this->properties['type']) {
296  $mimeType = $this->getMimeType();
297  list($fileType) = explode('/', $mimeType);
298  switch (strtolower($fileType)) {
299  case 'text':
300  $this->properties['type'] = self::FILETYPE_TEXT;
301  break;
302  case 'image':
303  $this->properties['type'] = self::FILETYPE_IMAGE;
304  break;
305  case 'audio':
306  $this->properties['type'] = self::FILETYPE_AUDIO;
307  break;
308  case 'video':
309  $this->properties['type'] = self::FILETYPE_VIDEO;
310  break;
311  case 'application':
312 
313  case 'software':
314  $this->properties['type'] = self::FILETYPE_APPLICATION;
315  break;
316  default:
317  $this->properties['type'] = self::FILETYPE_UNKNOWN;
318  }
319  }
320  return (int)$this->properties['type'];
321  }
322 
323  /******************
324  * CONTENTS RELATED
325  ******************/
332  public function getContents()
333  {
334  if ($this->deleted) {
335  throw new \RuntimeException('File has been deleted.', 1329821479);
336  }
337  return $this->getStorage()->getFileContents($this);
338  }
339 
348  public function setContents($contents)
349  {
350  if ($this->deleted) {
351  throw new \RuntimeException('File has been deleted.', 1329821478);
352  }
353  $this->getStorage()->setFileContents($this, $contents);
354  return $this;
355  }
356 
357  /****************************************
358  * STORAGE AND MANAGEMENT RELATED METHDOS
359  ****************************************/
360 
367  public function getStorage()
368  {
369  if ($this->storage === null) {
370  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
371  }
372  return $this->storage;
373  }
374 
382  public function exists()
383  {
384  if ($this->deleted) {
385  return false;
386  }
387  return $this->storage->hasFile($this->getIdentifier());
388  }
389 
399  {
400  $this->storage = $storage;
401  $this->properties['storage'] = $storage->getUid();
402  return $this;
403  }
404 
412  public function setIdentifier($identifier)
413  {
414  $this->identifier = $identifier;
415  return $this;
416  }
417 
424  public function getCombinedIdentifier()
425  {
426  if (is_array($this->properties) && MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
427  $combinedIdentifier = $this->properties['storage'] . ':' . $this->getIdentifier();
428  } else {
429  $combinedIdentifier = $this->getStorage()->getUid() . ':' . $this->getIdentifier();
430  }
431  return $combinedIdentifier;
432  }
433 
439  public function delete()
440  {
441  // The storage will mark this file as deleted
442  return $this->getStorage()->deleteFile($this);
443  }
444 
451  public function setDeleted()
452  {
453  $this->deleted = true;
454  }
455 
461  public function isDeleted()
462  {
463  return $this->deleted;
464  }
465 
474  public function rename($newName)
475  {
476  if ($this->deleted) {
477  throw new \RuntimeException('File has been deleted.', 1329821482);
478  }
479  return $this->getStorage()->renameFile($this, $newName);
480  }
481 
492  public function copyTo(Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
493  {
494  if ($this->deleted) {
495  throw new \RuntimeException('File has been deleted.', 1329821483);
496  }
497  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
498  }
499 
510  public function moveTo(Folder $targetFolder, $targetFileName = null, $conflictMode = DuplicationBehavior::RENAME)
511  {
512  if ($this->deleted) {
513  throw new \RuntimeException('File has been deleted.', 1329821484);
514  }
515  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
516  }
517 
518  /*****************
519  * SPECIAL METHODS
520  *****************/
530  public function getPublicUrl($relativeToCurrentScript = false)
531  {
532  if ($this->deleted) {
533  return null;
534  } else {
535  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
536  }
537  }
538 
549  public function getForLocalProcessing($writable = true)
550  {
551  if ($this->deleted) {
552  throw new \RuntimeException('File has been deleted.', 1329821486);
553  }
554  return $this->getStorage()->getFileForLocalProcessing($this, $writable);
555  }
556 
557  /***********************
558  * INDEX RELATED METHODS
559  ***********************/
567  abstract public function updateProperties(array $properties);
568 
574  public function getParentFolder()
575  {
576  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
577  }
578 }
getPublicUrl($relativeToCurrentScript=false)
copyTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
setStorage(ResourceStorage $storage)
moveTo(Folder $targetFolder, $targetFileName=null, $conflictMode=DuplicationBehavior::RENAME)
static pathinfo($path, $options=null)