‪TYPO3CMS  10.4
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 
25 
30 {
36  protected ‪$onlineMediaIdCache = [];
37 
43  protected ‪$extension = '';
44 
50  public function ‪__construct(‪$extension)
51  {
52  $this->extension = ‪$extension;
53  }
54 
61  public function ‪getOnlineMediaId(‪File $file)
62  {
63  if (!isset($this->onlineMediaIdCache[$file->‪getUid()])) {
64  // Limiting media identifier to 2048 bytes
65  if ($file->‪getSize() > 2048) {
66  return '';
67  }
68  // By definition these files only contain the ID of the remote media source
69  $this->onlineMediaIdCache[$file->‪getUid()] = trim($file->‪getContents());
70  }
71  return $this->onlineMediaIdCache[$file->‪getUid()];
72  }
73 
82  protected function ‪findExistingFileByOnlineMediaId($onlineMediaId, ‪Folder $targetFolder, $fileExtension)
83  {
84  $file = null;
85  $fileHash = sha1($onlineMediaId);
86  $files = $this->‪getFileIndexRepository()->‪findByContentHash($fileHash);
87  if (!empty($files)) {
88  foreach ($files as $fileIndexEntry) {
89  if (
90  $fileIndexEntry['folder_hash'] === $targetFolder->‪getHashedIdentifier()
91  && (int)$fileIndexEntry['storage'] === $targetFolder->‪getStorage()->‪getUid()
92  && $fileIndexEntry['extension'] === $fileExtension
93  ) {
94  $file = $this->‪getResourceFactory()->‪getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
95  break;
96  }
97  }
98  }
99  return $file;
100  }
101 
111  protected function ‪createNewFile(‪Folder $targetFolder, $fileName, $onlineMediaId)
112  {
113  $temporaryFile = GeneralUtility::tempnam('online_media');
114  ‪GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
115  $file = $targetFolder->‪addFile($temporaryFile, $fileName, ‪DuplicationBehavior::RENAME);
116  return $file;
117  }
118 
126  protected function ‪getTempFolderPath()
127  {
128  $path = ‪Environment::getPublicPath() . '/typo3temp/assets/online_media/';
129  if (!is_dir($path)) {
131  }
132  return $path;
133  }
134 
140  protected function ‪getFileIndexRepository()
141  {
143  }
144 
150  protected function ‪getResourceFactory()
151  {
152  return GeneralUtility::makeInstance(ResourceFactory::class);
153  }
154 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\findExistingFileByOnlineMediaId
‪File null findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOnlineMediaHelper.php:80
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:45
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getFileIndexRepository
‪FileIndexRepository getFileIndexRepository()
Definition: AbstractOnlineMediaHelper.php:138
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:24
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getTempFolderPath
‪string getTempFolderPath()
Definition: AbstractOnlineMediaHelper.php:124
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\__construct
‪__construct($extension)
Definition: AbstractOnlineMediaHelper.php:48
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$extension
‪string $extension
Definition: AbstractOnlineMediaHelper.php:41
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:59
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getResourceFactory
‪ResourceFactory getResourceFactory()
Definition: AbstractOnlineMediaHelper.php:148
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:149
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:2022
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper
Definition: AbstractOnlineMediaHelper.php:30
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\findByContentHash
‪mixed findByContentHash($hash)
Definition: FileIndexRepository.php:187
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\createNewFile
‪File createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
Definition: AbstractOnlineMediaHelper.php:109
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\getInstance
‪static FileIndexRepository getInstance()
Definition: FileIndexRepository.php:78
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFileToTypo3tempDir
‪static string writeFileToTypo3tempDir($filepath, $content)
Definition: GeneralUtility.php:1928
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:202
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:128
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:184
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$onlineMediaIdCache
‪array $onlineMediaIdCache
Definition: AbstractOnlineMediaHelper.php:35
‪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:390
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:307
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪string getHashedIdentifier()
Definition: Folder.php:170
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16