2 namespace TYPO3\CMS\Core\Resource
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
16 use TYPO3\CMS\Core\Utility\ArrayUtility
;
19 * Representation of a specific usage of a file with possibilities to override certain
20 * properties of the original file just for this usage of the file.
22 * It acts as a decorator over the original file in the way that most method calls are
23 * directly passed along to the original file object.
25 * All file related methods are directly passed along; only meta data functionality is adopted
26 * in this decorator class to priorities possible overrides for the metadata for this specific usage
29 class FileReference
implements FileInterface
32 * Various properties of the FileReference. Note that these information can be different
33 * to the ones found in the originalFile.
37 protected $propertiesOfFileReference;
40 * The identifier of this file to identify it on the storage.
41 * On some drivers, this is the path to the file, but drivers could also just
42 * provide any other unique identifier for this file on the specific storage.
46 protected $uidOfFileReference;
49 * The file name of this file. It's either the fileName of the original underlying file,
50 * or the overlay file name supplied by the user for this particular usage (FileReference) of the file.
57 * Reference to the original File object underlying this FileReference.
61 protected $originalFile;
64 * Properties merged with the parent object (File) if
65 * the value is not defined (NULL). Thus, FileReference properties act
66 * as overlays for the defined File properties.
70 protected $mergedProperties = [];
73 * Constructor for a file in use object. Should normally not be used
74 * directly, use the corresponding factory methods instead.
76 * @param array $fileReferenceData
77 * @param ResourceFactory $factory
79 * @throws \RuntimeException
80 * @throws \InvalidArgumentException
82 public function __construct(array $fileReferenceData, $factory = null
)
84 $this->propertiesOfFileReference
= $fileReferenceData;
85 if (!$fileReferenceData['uid_local']) {
86 throw new \
InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
89 /** @var ResourceFactory $factory */
90 $factory = ResourceFactory
::getInstance();
92 $this->originalFile
= $factory->getFileObject($fileReferenceData['uid_local']);
93 if (!is_object($this->originalFile
)) {
94 throw new \
RuntimeException(
95 'Original file not found for FileReference. UID given: "' . $fileReferenceData['uid_local'] . '"',
99 $this->name
= $fileReferenceData['name'] !== '' ?
$fileReferenceData['name'] : $this->originalFile
->getName();
102 /*******************************
103 * VARIOUS FILE PROPERTY GETTERS
104 *******************************/
106 * Returns true if the given key exists for this file.
108 * @param string $key The property to be looked up
111 public function hasProperty($key)
113 return array_key_exists($key, $this->getProperties());
117 * Gets a property, falling back to values of the parent.
119 * @param string $key The property to be looked up
121 * @throws \InvalidArgumentException
123 public function getProperty($key)
125 if (!$this->hasProperty($key)) {
126 throw new \
InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
128 $properties = $this->getProperties();
129 return $properties[$key];
133 * Gets a property of the file reference.
135 * @param string $key The property to be looked up
137 * @throws \InvalidArgumentException
139 public function getReferenceProperty($key)
141 if (!array_key_exists($key, $this->propertiesOfFileReference
)) {
142 throw new \
InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
144 return $this->propertiesOfFileReference
[$key];
148 * Gets all properties, falling back to values of the parent.
152 public function getProperties()
154 if (empty($this->mergedProperties
)) {
155 $this->mergedProperties
= $this->propertiesOfFileReference
;
156 ArrayUtility
::mergeRecursiveWithOverrule(
157 $this->mergedProperties
,
158 $this->originalFile
->getProperties(),
163 array_walk($this->mergedProperties
, [$this, 'restoreNonNullValuesCallback']);
166 return $this->mergedProperties
;
170 * Callback to handle the NULL value feature
172 * @param mixed $value
175 protected function restoreNonNullValuesCallback(&$value, $key)
177 if (array_key_exists($key, $this->propertiesOfFileReference
) && $this->propertiesOfFileReference
[$key] !== null
) {
178 $value = $this->propertiesOfFileReference
[$key];
183 * Gets all properties of the file reference.
187 public function getReferenceProperties()
189 return $this->propertiesOfFileReference
;
193 * Returns the name of this file
197 public function getName()
199 return $this->originalFile
->getName();
203 * Returns the title text to this image
205 * @todo Possibly move this to the image domain object instead
209 public function getTitle()
211 return $this->getProperty('title');
215 * Returns the alternative text to this image
217 * @todo Possibly move this to the image domain object instead
221 public function getAlternative()
223 return $this->getProperty('alternative');
227 * Returns the description text to this file
229 * @todo Possibly move this to the image domain object instead
233 public function getDescription()
235 return $this->getProperty('description');
239 * Returns the link that should be active when clicking on this image
241 * @todo Move this to the image domain object instead
245 public function getLink()
247 return $this->propertiesOfFileReference
['link'];
251 * Returns the uid of this File In Use
255 public function getUid()
257 return (int)$this->propertiesOfFileReference
['uid'];
261 * Returns the size of this file
265 public function getSize()
267 return (int)$this->originalFile
->getSize();
271 * Returns the Sha1 of this file
275 public function getSha1()
277 return $this->originalFile
->getSha1();
281 * Get the file extension of this file
283 * @return string The file extension
285 public function getExtension()
287 return $this->originalFile
->getExtension();
291 * Returns the basename (the name without extension) of this file.
295 public function getNameWithoutExtension()
297 return $this->originalFile
->getNameWithoutExtension();
301 * Get the MIME type of this file
303 * @return string mime type
305 public function getMimeType()
307 return $this->originalFile
->getMimeType();
311 * Returns the modification time of the file as Unix timestamp
315 public function getModificationTime()
317 return (int)$this->originalFile
->getModificationTime();
321 * Returns the creation time of the file as Unix timestamp
325 public function getCreationTime()
327 return (int)$this->originalFile
->getCreationTime();
331 * Returns the fileType of this file
333 * @return int $fileType
335 public function getType()
337 return (int)$this->originalFile
->getType();
341 * Check if file is marked as missing by indexer
345 public function isMissing()
347 return (bool
)$this->originalFile
->getProperty('missing');
354 * Get the contents of this file
356 * @return string File contents
358 public function getContents()
360 return $this->originalFile
->getContents();
364 * Replace the current file contents with the given string
366 * @param string $contents The contents to write to the file.
367 * @return File The file object (allows chaining).
369 public function setContents($contents)
371 return $this->originalFile
->setContents($contents);
374 /****************************************
375 * STORAGE AND MANAGEMENT RELATED METHDOS
376 ****************************************/
378 * Get the storage the original file is located in
380 * @return ResourceStorage
382 public function getStorage()
384 return $this->originalFile
->getStorage();
388 * Returns the identifier of the underlying original file
392 public function getIdentifier()
394 return $this->originalFile
->getIdentifier();
398 * Returns a combined identifier of the underlying original file
400 * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
402 public function getCombinedIdentifier()
404 return $this->originalFile
->getCombinedIdentifier();
408 * Deletes only this particular FileReference from the persistence layer
409 * (database table sys_file_reference) but leaves the original file untouched.
411 * @throws \BadMethodCallException
413 public function delete()
415 // @todo Implement this function. This should only delete the
416 // FileReference (sys_file_reference) record, not the file itself.
417 throw new \
BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
418 //return $this->fileRepository->removeUsageRecord($this);
422 * Renames the fileName in this particular usage.
424 * @param string $newName The new name
425 * @param string $conflictMode
427 public function rename($newName, $conflictMode = DuplicationBehavior
::RENAME
)
429 // @todo Implement this function. This should only rename the
430 // FileReference (sys_file_reference) record, not the file itself.
431 throw new \
BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
432 //return $this->fileRepository->renameUsageRecord($this, $newName);
439 * Returns a publicly accessible URL for this file
441 * WARNING: Access to the file may be restricted by further means, e.g.
442 * some web-based authentication. You have to take care of this yourself.
444 * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
445 * @return string|null NULL if file is missing or deleted, the generated url otherwise
447 public function getPublicUrl($relativeToCurrentScript = false
)
449 return $this->originalFile
->getPublicUrl($relativeToCurrentScript);
453 * Returns TRUE if this file is indexed.
454 * This is always true for FileReference objects, as they rely on a
455 * sys_file_reference record to be present, which in turn can only exist if
456 * the original file is indexed.
460 public function isIndexed()
466 * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
467 * If the file is normally located on a remote storages, this creates a local copy.
468 * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
470 * @param bool $writable Set this to FALSE if you only want to do read operations on the file.
473 public function getForLocalProcessing($writable = true
)
475 return $this->originalFile
->getForLocalProcessing($writable);
479 * Returns an array representation of the file.
480 * (This is used by the generic listing module vidi when displaying file records.)
482 * @return array Array of main data of the file. Don't rely on all data to be present here, it's just a selection of the most relevant information.
484 public function toArray()
486 $array = array_merge($this->originalFile
->toArray(), $this->propertiesOfFileReference
);
491 * Gets the original file being referenced.
495 public function getOriginalFile()
497 return $this->originalFile
;
501 * Get hashed identifier
505 public function getHashedIdentifier()
507 return $this->getStorage()->hashFileIdentifier($this->getIdentifier());
511 * Returns the parent folder.
513 * @return FolderInterface
515 public function getParentFolder()
517 return $this->originalFile
->getParentFolder();