‪TYPO3CMS  ‪main
AbstractOnlineMediaHelper.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 
27 
32 {
38  protected ‪$onlineMediaIdCache = [];
39 
45  protected ‪$extension = '';
46 
52  public function ‪__construct(‪$extension)
53  {
54  $this->extension = ‪$extension;
55  }
56 
62  public function ‪getOnlineMediaId(‪File $file)
63  {
64  if (!isset($this->onlineMediaIdCache[$file->‪getUid()])) {
65  // Limiting media identifier to 2048 bytes
66  if ($file->getSize() > 2048) {
67  return '';
68  }
69  try {
70  // By definition these files only contain the ID of the remote media source
71  $this->onlineMediaIdCache[$file->‪getUid()] = trim($file->‪getContents());
73  // User has no access to the file - online media id can not be fetched
74  return '';
75  }
76  }
77  return $this->onlineMediaIdCache[$file->‪getUid()];
78  }
79 
87  protected function ‪findExistingFileByOnlineMediaId($onlineMediaId, ‪Folder $targetFolder, $fileExtension)
88  {
89  $file = null;
90  $fileHash = sha1($onlineMediaId);
91  $files = $this->‪getFileIndexRepository()->‪findByContentHash($fileHash);
92  if (!empty($files)) {
93  foreach ($files as $fileIndexEntry) {
94  if (
95  $fileIndexEntry['folder_hash'] === $targetFolder->‪getHashedIdentifier()
96  && (int)$fileIndexEntry['storage'] === $targetFolder->‪getStorage()->getUid()
97  && $fileIndexEntry['extension'] === $fileExtension
98  ) {
99  $file = $this->‪getResourceFactory()->‪getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
100  break;
101  }
102  }
103  }
104  return $file;
105  }
106 
115  protected function ‪createNewFile(‪Folder $targetFolder, $fileName, $onlineMediaId)
116  {
117  $temporaryFile = GeneralUtility::tempnam('online_media');
118  ‪GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
119  $file = $targetFolder->‪addFile($temporaryFile, $fileName, ‪DuplicationBehavior::RENAME);
120  return $file;
121  }
122 
130  protected function ‪getTempFolderPath()
131  {
132  $path = ‪Environment::getPublicPath() . '/typo3temp/assets/online_media/';
133  if (!is_dir($path)) {
135  }
136  return $path;
137  }
138 
144  protected function ‪getFileIndexRepository()
145  {
146  return GeneralUtility::makeInstance(FileIndexRepository::class);
147  }
148 
154  protected function ‪getResourceFactory()
155  {
156  return GeneralUtility::makeInstance(ResourceFactory::class);
157  }
158 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\findExistingFileByOnlineMediaId
‪File null findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOnlineMediaHelper.php:85
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:44
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getFileIndexRepository
‪FileIndexRepository getFileIndexRepository()
Definition: AbstractOnlineMediaHelper.php:142
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getTempFolderPath
‪string getTempFolderPath()
Definition: AbstractOnlineMediaHelper.php:128
‪TYPO3\CMS\Core\Resource\File\getContents
‪getContents()
Definition: File.php:104
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\__construct
‪__construct($extension)
Definition: AbstractOnlineMediaHelper.php:50
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$extension
‪string $extension
Definition: AbstractOnlineMediaHelper.php:43
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:60
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getResourceFactory
‪ResourceFactory getResourceFactory()
Definition: AbstractOnlineMediaHelper.php:152
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1649
‪TYPO3\CMS\Core\Resource\Exception\IllegalFileExtensionException
Definition: IllegalFileExtensionException.php:23
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪getHashedIdentifier()
Definition: Folder.php:155
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFileToTypo3tempDir
‪static string null writeFileToTypo3tempDir(string $filepath, string $content)
Definition: GeneralUtility.php:1556
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper
Definition: AbstractOnlineMediaHelper.php:32
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\findByContentHash
‪mixed findByContentHash($hash)
Definition: FileIndexRepository.php:148
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\createNewFile
‪File createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
Definition: AbstractOnlineMediaHelper.php:113
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:33
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪getStorage()
Definition: Folder.php:139
‪TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior
‪DuplicationBehavior
Definition: DuplicationBehavior.php:28
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪return MathUtility::canBeInterpretedAsInteger($size) ?(int) $size int getUid()
Definition: AbstractFile.php:195
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$onlineMediaIdCache
‪array $onlineMediaIdCache
Definition: AbstractOnlineMediaHelper.php:37
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:25
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:193
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:285
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16