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\Core\Environment
;
18 use TYPO3\CMS\Core\
Resource\DuplicationBehavior
;
19 use TYPO3\CMS\Core\
Resource\File
;
20 use TYPO3\CMS\Core\
Resource\Folder
;
21 use TYPO3\CMS\Core\
Resource\Index\FileIndexRepository
;
22 use TYPO3\CMS\Core\
Resource\ResourceFactory
;
23 use TYPO3\CMS\Core\Utility\GeneralUtility
;
26 * Class AbstractOnlineMediaHelper
28 abstract class AbstractOnlineMediaHelper
implements OnlineMediaHelperInterface
31 * Cached OnlineMediaIds [fileUid => id]
35 protected $onlineMediaIdCache = [];
38 * File extension bind to the OnlineMedia helper
42 protected $extension = '';
47 * @param string $extension file extension bind to the OnlineMedia helper
49 public function __construct($extension)
51 $this->extension
= $extension;
55 * Get Online Media item id
60 public function getOnlineMediaId(File
$file)
62 if (!isset($this->onlineMediaIdCache
[$file->getUid()])) {
63 // Limiting media identifier to 2048 bytes
64 if ($file->getSize() > 2048) {
67 // By definition these files only contain the ID of the remote media source
68 $this->onlineMediaIdCache
[$file->getUid()] = trim($file->getContents());
70 return $this->onlineMediaIdCache
[$file->getUid()];
74 * Search for files with same onlineMediaId by content hash in indexed storage
76 * @param string $onlineMediaId
77 * @param Folder $targetFolder
78 * @param string $fileExtension
81 protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder
$targetFolder, $fileExtension)
84 $fileHash = sha1($onlineMediaId);
85 $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
87 foreach ($files as $fileIndexEntry) {
89 $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
90 && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
91 && $fileIndexEntry['extension'] === $fileExtension
93 $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
102 * Create new OnlineMedia item container file.
103 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
105 * @param Folder $targetFolder
106 * @param string $fileName
107 * @param string $onlineMediaId
110 protected function createNewFile(Folder
$targetFolder, $fileName, $onlineMediaId)
112 $temporaryFile = GeneralUtility
::tempnam('online_media');
113 GeneralUtility
::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
114 $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior
::RENAME
);
115 GeneralUtility
::unlink_tempfile($temporaryFile);
120 * Get temporary folder path to save preview images
124 protected function getTempFolderPath()
126 $path = Environment
::getVarPath() . '/transient/';
127 if (!is_dir($path)) {
128 GeneralUtility
::mkdir_deep($path);
134 * Returns an instance of the FileIndexRepository
136 * @return FileIndexRepository
138 protected function getFileIndexRepository()
140 return FileIndexRepository
::getInstance();
144 * Returns the ResourceFactory
146 * @return ResourceFactory
148 protected function getResourceFactory()
150 return ResourceFactory
::getInstance();