TYPO3 CMS  TYPO3_8-7
AbstractOnlineMediaHelper.php
Go to the documentation of this file.
1 <?php
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  // Limiting media identifier to 2048 bytes
63  if ($file->getSize() > 2048) {
64  return '';
65  }
66  // By definition these files only contain the ID of the remote media source
67  $this->onlineMediaIdCache[$file->getUid()] = trim($file->getContents());
68  }
69  return $this->onlineMediaIdCache[$file->getUid()];
70  }
71 
80  protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
81  {
82  $file = null;
83  $fileHash = sha1($onlineMediaId);
84  $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
85  if (!empty($files)) {
86  foreach ($files as $fileIndexEntry) {
87  if (
88  $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
89  && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
90  && $fileIndexEntry['extension'] === $fileExtension
91  ) {
92  $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
93  break;
94  }
95  }
96  }
97  return $file;
98  }
99 
109  protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
110  {
111  $temporaryFile = GeneralUtility::tempnam('online_media');
112  GeneralUtility::writeFileToTypo3tempDir($temporaryFile, $onlineMediaId);
113  $file = $targetFolder->addFile($temporaryFile, $fileName, DuplicationBehavior::RENAME);
114  return $file;
115  }
116 
122  protected function getTempFolderPath()
123  {
124  $path = PATH_site . 'typo3temp/var/transient/';
125  if (!is_dir($path)) {
127  }
128  return $path;
129  }
130 
136  protected function getFileIndexRepository()
137  {
139  }
140 
146  protected function getResourceFactory()
147  {
149  }
150 }
static mkdir_deep($directory, $deepDirectory='')
static writeFileToTypo3tempDir($filepath, $content)
findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:289
static tempnam($filePrefix, $fileSuffix='')