1<?php 2namespace 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 17use TYPO3\CMS\Core\Resource\File; 18use TYPO3\CMS\Core\Resource\Folder; 19use TYPO3\CMS\Core\Utility\GeneralUtility; 20 21/** 22 * Class AbstractOEmbedHelper 23 * See http://oembed.com/ for more on OEmbed specification 24 */ 25abstract class AbstractOEmbedHelper extends AbstractOnlineMediaHelper 26{ 27 /** 28 * @param string $mediaId 29 * @param string $format 30 * @return string 31 */ 32 abstract protected function getOEmbedUrl($mediaId, $format = 'json'); 33 34 /** 35 * Transform mediaId to File 36 * 37 * @param string $mediaId 38 * @param Folder $targetFolder 39 * @param string $fileExtension 40 * @return File 41 */ 42 protected function transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension) 43 { 44 $file = $this->findExistingFileByOnlineMediaId($mediaId, $targetFolder, $fileExtension); 45 46 // no existing file create new 47 if ($file === null) { 48 $oEmbed = $this->getOEmbedData($mediaId); 49 if (!empty($oEmbed['title'])) { 50 $fileName = $oEmbed['title'] . '.' . $fileExtension; 51 } else { 52 $fileName = $mediaId . '.' . $fileExtension; 53 } 54 $file = $this->createNewFile($targetFolder, $fileName, $mediaId); 55 } 56 return $file; 57 } 58 59 /** 60 * Get OEmbed data 61 * 62 * @param string $mediaId 63 * @return array|null 64 */ 65 protected function getOEmbedData($mediaId) 66 { 67 $oEmbed = GeneralUtility::getUrl( 68 $this->getOEmbedUrl($mediaId) 69 ); 70 if ($oEmbed) { 71 $oEmbed = json_decode($oEmbed, true); 72 } 73 return $oEmbed; 74 } 75 76 /** 77 * Get meta data for OnlineMedia item 78 * Using the meta data from oEmbed 79 * 80 * @param File $file 81 * @return array with metadata 82 */ 83 public function getMetaData(File $file) 84 { 85 $metadata = []; 86 87 $oEmbed = $this->getOEmbedData($this->getOnlineMediaId($file)); 88 89 if ($oEmbed) { 90 $metadata['width'] = (int)$oEmbed['width']; 91 $metadata['height'] = (int)$oEmbed['height']; 92 if (empty($file->getProperty('title'))) { 93 $metadata['title'] = strip_tags($oEmbed['title']); 94 } 95 $metadata['author'] = $oEmbed['author_name']; 96 } 97 98 return $metadata; 99 } 100} 101