TYPO3  7.6
AbstractOnlineMediaHelper.php
Go to the documentation of this file.
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 
22 
27 {
33  protected $onlineMediaIdCache = array();
34 
40  protected $extension = '';
41 
47  public function __construct($extension)
48  {
49  $this->extension = $extension;
50  }
51 
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 
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 
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 
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 
129  protected function getFileIndexRepository()
130  {
132  }
133 
139  protected function getResourceFactory()
140  {
142  }
143 }