‪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 
28 {
34  abstract protected function ‪getOEmbedUrl($mediaId, $format = 'json');
35 
43  protected function ‪transformMediaIdToFile($mediaId, ‪Folder $targetFolder, $fileExtension)
44  {
45  $file = $this->‪findExistingFileByOnlineMediaId($mediaId, $targetFolder, $fileExtension);
46  if ($file !== null) {
47  throw new ‪OnlineMediaAlreadyExistsException($file, 1695236851);
48  }
49  // no existing file create new
50  $oEmbed = $this->‪getOEmbedData($mediaId);
51  if (!empty($oEmbed['title'])) {
52  $fileName = $oEmbed['title'] . '.' . $fileExtension;
53  } else {
54  $fileName = $mediaId . '.' . $fileExtension;
55  }
56  return $this->‪createNewFile($targetFolder, $fileName, $mediaId);
57  }
58 
65  protected function ‪getOEmbedData($mediaId)
66  {
67  $oEmbed = (string)‪GeneralUtility::getUrl(
68  $this->‪getOEmbedUrl($mediaId)
69  );
70  if ($oEmbed !== '') {
71  $oEmbed = json_decode($oEmbed, true);
72  if (is_array($oEmbed)) {
73  return $oEmbed;
74  }
75  }
76  return null;
77  }
78 
85  public function ‪getMetaData(‪File $file)
86  {
87  $metadata = [];
88 
89  $oEmbed = $this->‪getOEmbedData($this->‪getOnlineMediaId($file));
90 
91  if (is_array($oEmbed) && $oEmbed !== []) {
92  $metadata['width'] = (int)($oEmbed['width'] ?? 0);
93  $metadata['height'] = (int)($oEmbed['height'] ?? 0);
94  if (empty($file->‪getProperty('title'))) {
95  $metadata['title'] = strip_tags($oEmbed['title'] ?? '');
96  }
97  $metadata['author'] = $oEmbed['author_name'] ?? '';
98  }
99 
100  return $metadata;
101  }
102 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\findExistingFileByOnlineMediaId
‪File null findExistingFileByOnlineMediaId($onlineMediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOnlineMediaHelper.php:85
‪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:60
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\getMetaData
‪array getMetaData(File $file)
Definition: AbstractOEmbedHelper.php:85
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\getOEmbedData
‪array null getOEmbedData($mediaId)
Definition: AbstractOEmbedHelper.php:65
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\transformMediaIdToFile
‪File transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOEmbedHelper.php:43
‪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:1439
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper
Definition: AbstractOnlineMediaHelper.php:32
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\createNewFile
‪File createNewFile(Folder $targetFolder, $fileName, $onlineMediaId)
Definition: AbstractOnlineMediaHelper.php:113
‪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:28