7409a8693f7878200352a0cd69b9fd882b437eca
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / OnlineMedia / Helpers / AbstractOnlineMediaHelper.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\OnlineMedia\Helpers;
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
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;
24
25 /**
26 * Class AbstractOnlineMediaHelper
27 */
28 abstract class AbstractOnlineMediaHelper implements OnlineMediaHelperInterface
29 {
30 /**
31 * Cached OnlineMediaIds [fileUid => id]
32 *
33 * @var array
34 */
35 protected $onlineMediaIdCache = [];
36
37 /**
38 * File extension bind to the OnlineMedia helper
39 *
40 * @var string
41 */
42 protected $extension = '';
43
44 /**
45 * Constructor
46 *
47 * @param string $extension file extension bind to the OnlineMedia helper
48 */
49 public function __construct($extension)
50 {
51 $this->extension = $extension;
52 }
53
54 /**
55 * Get Online Media item id
56 *
57 * @param File $file
58 * @return string
59 */
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
73 /**
74 * Search for files with same onlineMediaId by content hash in indexed storage
75 *
76 * @param string $onlineMediaId
77 * @param Folder $targetFolder
78 * @param string $fileExtension
79 * @return File|null
80 */
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
101 /**
102 * Create new OnlineMedia item container file.
103 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
104 *
105 * @param Folder $targetFolder
106 * @param string $fileName
107 * @param string $onlineMediaId
108 * @return File
109 */
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 GeneralUtility::unlink_tempfile($temporaryFile);
116 return $file;
117 }
118
119 /**
120 * Get temporary folder path to save preview images
121 *
122 * @return string
123 */
124 protected function getTempFolderPath()
125 {
126 $path = Environment::getVarPath() . '/transient/';
127 if (!is_dir($path)) {
128 GeneralUtility::mkdir_deep($path);
129 }
130 return $path;
131 }
132
133 /**
134 * Returns an instance of the FileIndexRepository
135 *
136 * @return FileIndexRepository
137 */
138 protected function getFileIndexRepository()
139 {
140 return FileIndexRepository::getInstance();
141 }
142
143 /**
144 * Returns the ResourceFactory
145 *
146 * @return ResourceFactory
147 */
148 protected function getResourceFactory()
149 {
150 return ResourceFactory::getInstance();
151 }
152 }