[TASK] Move publicly accessible files to typo3temp/assets/
[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 * This is created inside typo3temp/ and then moved from FAL to the proper storage.
98 *
99 * @param Folder $targetFolder
100 * @param string $fileName
101 * @param string $onlineMediaId
102 * @return File
103 */
104 protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
105 {
106 $temporaryFile = PATH_site . 'typo3temp/assets/transient/' . GeneralUtility::tempnam('online_media');
107 GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
108 return $targetFolder->addFile($temporaryFile, $fileName, 'changeName');
109 }
110
111 /**
112 * Returns an instance of the FileIndexRepository
113 *
114 * @return FileIndexRepository
115 */
116 protected function getFileIndexRepository()
117 {
118 return FileIndexRepository::getInstance();
119 }
120
121 /**
122 * Returns the ResourceFactory
123 *
124 * @return ResourceFactory
125 */
126 protected function getResourceFactory()
127 {
128 return ResourceFactory::getInstance();
129 }
130 }