TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
23 
28 {
34  protected $onlineMediaIdCache = [];
35 
41  protected $extension = '';
42 
48  public function __construct($extension)
49  {
50  $this->extension = $extension;
51  }
52 
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 
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 
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 
119  protected function getTempFolderPath()
120  {
121  $path = PATH_site . 'typo3temp/var/transient/';
122  if (!is_dir($path)) {
124  }
125  return $path;
126  }
127 
133  protected function getFileIndexRepository()
134  {
136  }
137 
143  protected function getResourceFactory()
144  {
146  }
147 }
findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
static mkdir_deep($directory, $deepDirectory= '')
static unlink_tempfile($uploadedTempFileName)
static writeFileToTypo3tempDir($filepath, $content)
static tempnam($filePrefix, $fileSuffix= '')
addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)