TYPO3 CMS  TYPO3_6-2
AbstractFile.php
Go to the documentation of this file.
1 <?php
3 
18 
24 abstract class AbstractFile implements FileInterface {
25 
35  protected $properties;
36 
42  protected $storage = NULL;
43 
51  protected $identifier;
52 
58  protected $name;
59 
65  protected $deleted = FALSE;
66 
70  const FILETYPE_UNKNOWN = 0;
71 
76  const FILETYPE_TEXT = 1;
77 
82  const FILETYPE_IMAGE = 2;
83 
88  const FILETYPE_AUDIO = 3;
89 
94  const FILETYPE_VIDEO = 4;
95 
101 
106  const FILETYPE_SOFTWARE = 5;
107 
108  /******************
109  * VARIOUS FILE PROPERTY GETTERS
110  ******************/
117  public function hasProperty($key) {
118  return array_key_exists($key, $this->properties);
119  }
120 
127  public function getProperty($key) {
128  if ($this->hasProperty($key)) {
129  return $this->properties[$key];
130  } else {
131  return NULL;
132  }
133  }
134 
140  public function getProperties() {
141  return $this->properties;
142  }
143 
149  public function getIdentifier() {
150  return $this->identifier;
151  }
152 
158  public function getHashedIdentifier() {
159  return $this->properties['identifier_hash'];
160  }
161 
167  public function getName() {
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  return PathUtility::pathinfo($this->getName(), PATHINFO_FILENAME);
180  }
181 
188  public function getSize() {
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  return $this->getProperty('uid');
202  }
203 
210  public function getSha1() {
211  if ($this->deleted) {
212  throw new \RuntimeException('File has been deleted.', 1329821481);
213  }
214  return $this->getStorage()->hashFile($this, 'sha1');
215  }
216 
223  public function getCreationTime() {
224  if ($this->deleted) {
225  throw new \RuntimeException('File has been deleted.', 1329821487);
226  }
227  return $this->getProperty('creation_date');
228  }
229 
236  public function getModificationTime() {
237  if ($this->deleted) {
238  throw new \RuntimeException('File has been deleted.', 1329821488);
239  }
240  return $this->getProperty('modification_date');
241  }
242 
248  public function getExtension() {
249  $pathinfo = PathUtility::pathinfo($this->getName());
250 
251  $extension = strtolower($pathinfo['extension']);
252 
253  return $extension;
254  }
255 
261  public function getMimeType() {
262  return $this->properties['mime_type'] ?: array_pop($this->getStorage()->getFileInfoByIdentifier($this->getIdentifier(), array('mimetype')));
263  }
264 
278  public function getType() {
279  // this basically extracts the mimetype and guess the filetype based
280  // on the first part of the mimetype works for 99% of all cases, and
281  // we don't need to make an SQL statement like EXT:media does currently
282  if (!$this->properties['type']) {
283  $mimeType = $this->getMimeType();
284  list($fileType) = explode('/', $mimeType);
285  switch (strtolower($fileType)) {
286  case 'text':
287  $this->properties['type'] = self::FILETYPE_TEXT;
288  break;
289  case 'image':
290  $this->properties['type'] = self::FILETYPE_IMAGE;
291  break;
292  case 'audio':
293  $this->properties['type'] = self::FILETYPE_AUDIO;
294  break;
295  case 'video':
296  $this->properties['type'] = self::FILETYPE_VIDEO;
297  break;
298  case 'application':
299 
300  case 'software':
301  $this->properties['type'] = self::FILETYPE_APPLICATION;
302  break;
303  default:
304  $this->properties['type'] = self::FILETYPE_UNKNOWN;
305  }
306  }
307  return (int)$this->properties['type'];
308  }
309 
310  /******************
311  * CONTENTS RELATED
312  ******************/
319  public function getContents() {
320  if ($this->deleted) {
321  throw new \RuntimeException('File has been deleted.', 1329821479);
322  }
323  return $this->getStorage()->getFileContents($this);
324  }
325 
334  public function setContents($contents) {
335  if ($this->deleted) {
336  throw new \RuntimeException('File has been deleted.', 1329821478);
337  }
338  $this->getStorage()->setFileContents($this, $contents);
339  return $this;
340  }
341 
342  /****************************************
343  * STORAGE AND MANAGEMENT RELATED METHDOS
344  ****************************************/
345 
352  public function getStorage() {
353  if ($this->storage === NULL) {
354  throw new \RuntimeException('You\'re using fileObjects without a storage.', 1381570091);
355  }
356  return $this->storage;
357  }
358 
366  public function exists() {
367  if ($this->deleted) {
368  return FALSE;
369  }
370  return $this->storage->hasFile($this->getIdentifier());
371  }
372 
382  $this->storage = $storage;
383  $this->properties['storage'] = $storage->getUid();
384  return $this;
385  }
386 
394  public function setIdentifier($identifier) {
395  $this->identifier = $identifier;
396  return $this;
397  }
398 
405  public function getCombinedIdentifier() {
406  if (is_array($this->properties) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
407  $combinedIdentifier = $this->properties['storage'] . ':' . $this->getIdentifier();
408  } else {
409  $combinedIdentifier = $this->getStorage()->getUid() . ':' . $this->getIdentifier();
410  }
411  return $combinedIdentifier;
412  }
413 
419  public function delete() {
420  // The storage will mark this file as deleted
421  return $this->getStorage()->deleteFile($this);
422  }
423 
430  public function setDeleted() {
431  $this->deleted = TRUE;
432  }
433 
439  public function isDeleted() {
440  return $this->deleted;
441  }
442 
451  public function rename($newName) {
452  if ($this->deleted) {
453  throw new \RuntimeException('File has been deleted.', 1329821482);
454  }
455  return $this->getStorage()->renameFile($this, $newName);
456  }
457 
468  public function copyTo(Folder $targetFolder, $targetFileName = NULL, $conflictMode = 'renameNewFile') {
469  if ($this->deleted) {
470  throw new \RuntimeException('File has been deleted.', 1329821483);
471  }
472  return $targetFolder->getStorage()->copyFile($this, $targetFolder, $targetFileName, $conflictMode);
473  }
474 
485  public function moveTo(Folder $targetFolder, $targetFileName = NULL, $conflictMode = 'renameNewFile') {
486  if ($this->deleted) {
487  throw new \RuntimeException('File has been deleted.', 1329821484);
488  }
489  return $targetFolder->getStorage()->moveFile($this, $targetFolder, $targetFileName, $conflictMode);
490  }
491 
492  /*****************
493  * SPECIAL METHODS
494  *****************/
504  public function getPublicUrl($relativeToCurrentScript = FALSE) {
505  if ($this->deleted) {
506  return NULL;
507  } else {
508  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
509  }
510  }
511 
522  public function getForLocalProcessing($writable = TRUE) {
523  if ($this->deleted) {
524  throw new \RuntimeException('File has been deleted.', 1329821486);
525  }
526  return $this->getStorage()->getFileForLocalProcessing($this, $writable);
527  }
528 
529  /***********************
530  * INDEX RELATED METHODS
531  ***********************/
539  abstract public function updateProperties(array $properties);
540 
546  public function getParentFolder() {
547  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
548  }
549 }
static pathinfo($path, $options=NULL)
setStorage(ResourceStorage $storage)
getPublicUrl($relativeToCurrentScript=FALSE)
moveTo(Folder $targetFolder, $targetFileName=NULL, $conflictMode='renameNewFile')
copyTo(Folder $targetFolder, $targetFileName=NULL, $conflictMode='renameNewFile')