4079557ad66039bbe7e53cc3eeef6a15c3118930
[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\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;
23
24 /**
25 * Class AbstractOnlineMediaHelper
26 */
27 abstract class AbstractOnlineMediaHelper implements OnlineMediaHelperInterface
28 {
29 /**
30 * Cached OnlineMediaIds [fileUid => id]
31 *
32 * @var array
33 */
34 protected $onlineMediaIdCache = [];
35
36 /**
37 * File extension bind to the OnlineMedia helper
38 *
39 * @var string
40 */
41 protected $extension = '';
42
43 /**
44 * Constructor
45 *
46 * @param string $extension file extension bind to the OnlineMedia helper
47 */
48 public function __construct($extension)
49 {
50 $this->extension = $extension;
51 }
52
53 /**
54 * Get Online Media item id
55 *
56 * @param File $file
57 * @return string
58 */
59 public function getOnlineMediaId(File $file)
60 {
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());
64 }
65 return $this->onlineMediaIdCache[$file->getUid()];
66 }
67
68 /**
69 * Search for files with same onlineMediaId by content hash in indexed storage
70 *
71 * @param string $onlineMediaId
72 * @param Folder $targetFolder
73 * @param string $fileExtension
74 * @return File|NULL
75 */
76 protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
77 {
78 $file = null;
79 $fileHash = sha1($onlineMediaId);
80 $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
81 if (!empty($files)) {
82 foreach ($files as $fileIndexEntry) {
83 if (
84 $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
85 && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
86 && $fileIndexEntry['extension'] === $fileExtension
87 ) {
88 $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
89 break;
90 }
91 }
92 }
93 return $file;
94 }
95
96 /**
97 * Create new OnlineMedia item container file.
98 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
99 *
100 * @param Folder $targetFolder
101 * @param string $fileName
102 * @param string $onlineMediaId
103 * @return File
104 */
105 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
106 {
107 $temporaryFile = GeneralUtility::tempnam('online_media');
108 GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
109 $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior::RENAME);
110 GeneralUtility::unlink_tempfile($temporaryFile);
111 return $file;
112 }
113
114 /**
115 * Get temporary folder path to save preview images
116 *
117 * @return string
118 */
119 protected function getTempFolderPath()
120 {
121 $path = PATH_site . 'typo3temp/var/transient/';
122 if (!is_dir($path)) {
123 GeneralUtility::mkdir_deep($path);
124 }
125 return $path;
126 }
127
128 /**
129 * Returns an instance of the FileIndexRepository
130 *
131 * @return FileIndexRepository
132 */
133 protected function getFileIndexRepository()
134 {
135 return FileIndexRepository::getInstance();
136 }
137
138 /**
139 * Returns the ResourceFactory
140 *
141 * @return ResourceFactory
142 */
143 protected function getResourceFactory()
144 {
145 return ResourceFactory::getInstance();
146 }
147 }