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