[FEATURE] Use dynamic path for typo3temp/var/
[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 // By definition these files only contain the ID of the remote media source
64 $this->onlineMediaIdCache[$file->getUid()] = trim($file->getContents());
65 }
66 return $this->onlineMediaIdCache[$file->getUid()];
67 }
68
69 /**
70 * Search for files with same onlineMediaId by content hash in indexed storage
71 *
72 * @param string $onlineMediaId
73 * @param Folder $targetFolder
74 * @param string $fileExtension
75 * @return File|null
76 */
77 protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
78 {
79 $file = null;
80 $fileHash = sha1($onlineMediaId);
81 $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
82 if (!empty($files)) {
83 foreach ($files as $fileIndexEntry) {
84 if (
85 $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
86 && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
87 && $fileIndexEntry['extension'] === $fileExtension
88 ) {
89 $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
90 break;
91 }
92 }
93 }
94 return $file;
95 }
96
97 /**
98 * Create new OnlineMedia item container file.
99 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
100 *
101 * @param Folder $targetFolder
102 * @param string $fileName
103 * @param string $onlineMediaId
104 * @return File
105 */
106 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
107 {
108 $temporaryFile = GeneralUtility::tempnam('online_media');
109 GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
110 $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior::RENAME);
111 GeneralUtility::unlink_tempfile($temporaryFile);
112 return $file;
113 }
114
115 /**
116 * Get temporary folder path to save preview images
117 *
118 * @return string
119 */
120 protected function getTempFolderPath()
121 {
122 $path = Environment::getVarPath() . '/transient/';
123 if (!is_dir($path)) {
124 GeneralUtility::mkdir_deep($path);
125 }
126 return $path;
127 }
128
129 /**
130 * Returns an instance of the FileIndexRepository
131 *
132 * @return FileIndexRepository
133 */
134 protected function getFileIndexRepository()
135 {
136 return FileIndexRepository::getInstance();
137 }
138
139 /**
140 * Returns the ResourceFactory
141 *
142 * @return ResourceFactory
143 */
144 protected function getResourceFactory()
145 {
146 return ResourceFactory::getInstance();
147 }
148 }