2 namespace TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
;
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!
17 use TYPO3\CMS\Core\Resource\DuplicationBehavior
;
18 use TYPO3\CMS\Core\Resource\File
;
19 use TYPO3\CMS\Core\Resource\Folder
;
20 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository
;
21 use TYPO3\CMS\Core\Resource\ResourceFactory
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
25 * Class AbstractOnlineMediaHelper
27 abstract class AbstractOnlineMediaHelper
implements OnlineMediaHelperInterface
30 * Cached OnlineMediaIds [fileUid => id]
34 protected $onlineMediaIdCache = [];
37 * File extension bind to the OnlineMedia helper
41 protected $extension = '';
46 * @param string $extension file extension bind to the OnlineMedia helper
48 public function __construct($extension)
50 $this->extension
= $extension;
54 * Get Online Media item id
59 public function getOnlineMediaId(File
$file)
61 if (!isset($this->onlineMediaIdCache
[$file->getUid()])) {
62 // By definition these files only contain the ID of the remote media source
63 $this->onlineMediaIdCache
[$file->getUid()] = trim($file->getContents());
65 return $this->onlineMediaIdCache
[$file->getUid()];
69 * Search for files with same onlineMediaId by content hash in indexed storage
71 * @param string $onlineMediaId
72 * @param Folder $targetFolder
73 * @param string $fileExtension
76 protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder
$targetFolder, $fileExtension)
79 $fileHash = sha1($onlineMediaId);
80 $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
82 foreach ($files as $fileIndexEntry) {
84 $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
85 && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
86 && $fileIndexEntry['extension'] === $fileExtension
88 $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
97 * Create new OnlineMedia item container file.
98 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
100 * @param Folder $targetFolder
101 * @param string $fileName
102 * @param string $onlineMediaId
105 protected function createNewFile(Folder
$targetFolder, $fileName, $onlineMediaId)
107 $temporaryFile = GeneralUtility
::tempnam('online_media');
108 GeneralUtility
::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
109 $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior
::RENAME
);
110 GeneralUtility
::unlink_tempfile($temporaryFile);
115 * Get temporary folder path to save preview images
119 protected function getTempFolderPath()
121 $path = PATH_site
. 'typo3temp/var/transient/';
122 if (!is_dir($path)) {
123 GeneralUtility
::mkdir_deep($path);
129 * Returns an instance of the FileIndexRepository
131 * @return FileIndexRepository
133 protected function getFileIndexRepository()
135 return FileIndexRepository
::getInstance();
139 * Returns the ResourceFactory
141 * @return ResourceFactory
143 protected function getResourceFactory()
145 return ResourceFactory
::getInstance();