‪TYPO3CMS  ‪main
AbstractOEmbedHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
27 {
33  abstract protected function ‪getOEmbedUrl($mediaId, $format = 'json');
34 
42  protected function ‪transformMediaIdToFile($mediaId, ‪Folder $targetFolder, $fileExtension)
43  {
44  $file = $this->‪findExistingFileByOnlineMediaId($mediaId, $targetFolder, $fileExtension);
45  if ($file !== null) {
46  throw new ‪OnlineMediaAlreadyExistsException($file, 1695236851);
47  }
48  // no existing file create new
49  $oEmbed = $this->‪getOEmbedData($mediaId);
50  if (!empty($oEmbed['title'])) {
51  $fileName = $oEmbed['title'] . '.' . $fileExtension;
52  } else {
53  $fileName = $mediaId . '.' . $fileExtension;
54  }
55  return $this->‪createNewFile($targetFolder, $fileName, $mediaId);
56  }
57 
64  protected function ‪getOEmbedData($mediaId)
65  {
66  $oEmbed = (string)‪GeneralUtility::getUrl(
67  $this->‪getOEmbedUrl($mediaId)
68  );
69  if ($oEmbed !== '') {
70  $oEmbed = json_decode($oEmbed, true);
71  if (is_array($oEmbed)) {
72  return $oEmbed;
73  }
74  }
75  return null;
76  }
77 
84  public function ‪getMetaData(‪File $file)
85  {
86  $metadata = [];
87 
88  $oEmbed = $this->‪getOEmbedData($this->‪getOnlineMediaId($file));
89 
90  if (is_array($oEmbed) && $oEmbed !== []) {
91  $metadata['width'] = (int)($oEmbed['width'] ?? 0);
92  $metadata['height'] = (int)($oEmbed['height'] ?? 0);
93  if (empty($file->‪getProperty('title'))) {
94  $metadata['title'] = strip_tags($oEmbed['title'] ?? '');
95  }
96  $metadata['author'] = $oEmbed['author_name'] ?? '';
97  }
98 
99  return $metadata;
100  }
101 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\findExistingFileByOnlineMediaId
‪File null findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOnlineMediaHelper.php:82
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\getOEmbedUrl
‪string getOEmbedUrl($mediaId, $format='json')
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:57
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\getMetaData
‪array getMetaData(File $file)
Definition: AbstractOEmbedHelper.php:84
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\getOEmbedData
‪array null getOEmbedData($mediaId)
Definition: AbstractOEmbedHelper.php:64
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\transformMediaIdToFile
‪File transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOEmbedHelper.php:42
‪TYPO3\CMS\Core\Resource\File\getProperty
‪getProperty(string $key)
Definition: File.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static string false getUrl(string $url)
Definition: GeneralUtility.php:1442
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper
Definition: AbstractOnlineMediaHelper.php:29
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\createNewFile
‪File createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
Definition: AbstractOnlineMediaHelper.php:110
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\Exception\OnlineMediaAlreadyExistsException
Definition: OnlineMediaAlreadyExistsException.php:27
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper
Definition: AbstractOEmbedHelper.php:27