TYPO3 CMS  TYPO3_7-6
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 
22 
27 {
33  protected $onlineMediaIdCache = [];
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  // Limiting media identifier to 2048 bytes
62  if ($file->getSize() > 2048) {
63  return '';
64  }
65  // By definition these files only contain the ID of the remote media source
66  $this->onlineMediaIdCache[$file->getUid()] = trim($file->getContents());
67  }
68  return $this->onlineMediaIdCache[$file->getUid()];
69  }
70 
79  protected function findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
80  {
81  $file = null;
82  $fileHash = sha1($onlineMediaId);
83  $files = $this->getFileIndexRepository()->findByContentHash($fileHash);
84  if (!empty($files)) {
85  foreach ($files as $fileIndexEntry) {
86  if (
87  $fileIndexEntry['folder_hash'] === $targetFolder->getHashedIdentifier()
88  && (int)$fileIndexEntry['storage'] === $targetFolder->getStorage()->getUid()
89  && $fileIndexEntry['extension'] === $fileExtension
90  ) {
91  $file = $this->getResourceFactory()->getFileObject($fileIndexEntry['uid'], $fileIndexEntry);
92  break;
93  }
94  }
95  }
96  return $file;
97  }
98 
107  protected function createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
108  {
109  $tempFilePath = GeneralUtility::tempnam('online_media');
110  file_put_contents($tempFilePath, $onlineMediaId);
111  return $targetFolder->addFile($tempFilePath, $fileName, 'changeName');
112  }
113 
119  protected function getTempFolderPath()
120  {
121  $path = PATH_site . 'typo3temp/online_media/';
122  if (!is_dir($path)) {
123  GeneralUtility::mkdir($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)
addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:290
static tempnam($filePrefix, $fileSuffix='')