‪TYPO3CMS  9.5
AbstractOnlineMediaHelper.php
Go to the documentation of this file.
1 <?php
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 
24 
29 {
35  protected ‪$onlineMediaIdCache = [];
36 
42  protected ‪$extension = '';
43 
49  public function ‪__construct(‪$extension)
50  {
51  $this->extension = ‪$extension;
52  }
53 
60  public function ‪getOnlineMediaId(‪File $file)
61  {
62  if (!isset($this->onlineMediaIdCache[$file->‪getUid()])) {
63  // Limiting media identifier to 2048 bytes
64  if ($file->‪getSize() > 2048) {
65  return '';
66  }
67  // By definition these files only contain the ID of the remote media source
68  $this->onlineMediaIdCache[$file->‪getUid()] = trim($file->‪getContents());
69  }
70  return $this->onlineMediaIdCache[$file->‪getUid()];
71  }
72 
81  protected function ‪findExistingFileByOnlineMediaId($onlineMediaId, ‪Folder $targetFolder, $fileExtension)
82  {
83  $file = null;
84  $fileHash = sha1($onlineMediaId);
85  $files = $this->‪getFileIndexRepository()->‪findByContentHash($fileHash);
86  if (!empty($files)) {
87  foreach ($files as $fileIndexEntry) {
88  if (
89  $fileIndexEntry['folder_hash'] === $targetFolder->‪getHashedIdentifier()
90  && (int)$fileIndexEntry['storage'] === $targetFolder->‪getStorage()->‪getUid()
91  && $fileIndexEntry['extension'] === $fileExtension
92  ) {
93  $file = $this->‪getResourceFactory()->‪getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
94  break;
95  }
96  }
97  }
98  return $file;
99  }
100 
110  protected function ‪createNewFile(‪Folder $targetFolder, $fileName, $onlineMediaId)
111  {
112  $temporaryFile = GeneralUtility::tempnam('online_media');
113  GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
114  $file = $targetFolder->‪addFile($temporaryFile, $fileName, ‪DuplicationBehavior::RENAME);
115  return $file;
116  }
117 
123  protected function ‪getTempFolderPath()
124  {
125  $path = ‪Environment::getVarPath() . '/transient/';
126  if (!is_dir($path)) {
127  GeneralUtility::mkdir_deep($path);
128  }
129  return $path;
130  }
131 
137  protected function ‪getFileIndexRepository()
138  {
140  }
141 
147  protected function ‪getResourceFactory()
148  {
150  }
151 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\findExistingFileByOnlineMediaId
‪File null findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOnlineMediaHelper.php:79
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:41
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getFileIndexRepository
‪FileIndexRepository getFileIndexRepository()
Definition: AbstractOnlineMediaHelper.php:135
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:271
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:23
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getTempFolderPath
‪string getTempFolderPath()
Definition: AbstractOnlineMediaHelper.php:121
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\__construct
‪__construct($extension)
Definition: AbstractOnlineMediaHelper.php:47
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$extension
‪string $extension
Definition: AbstractOnlineMediaHelper.php:40
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:58
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getResourceFactory
‪ResourceFactory getResourceFactory()
Definition: AbstractOnlineMediaHelper.php:145
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:146
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper
Definition: AbstractOnlineMediaHelper.php:29
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\findByContentHash
‪mixed findByContentHash($hash)
Definition: FileIndexRepository.php:174
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\createNewFile
‪File createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
Definition: AbstractOnlineMediaHelper.php:108
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\getInstance
‪static FileIndexRepository getInstance()
Definition: FileIndexRepository.php:70
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:31
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:130
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:182
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\$onlineMediaIdCache
‪array $onlineMediaIdCache
Definition: AbstractOnlineMediaHelper.php:34
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:24
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:399
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:304
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪string getHashedIdentifier()
Definition: Folder.php:167
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:2
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165