2 namespace TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Resource\File
;
18 use TYPO3\CMS\Core\Resource\Folder
;
19 use TYPO3\CMS\Core\Utility\GeneralUtility
;
22 * SlideShare helper class
24 class YouTubeHelper
extends AbstractOEmbedHelper
30 * @param bool $relativeToCurrentScript
33 public function getPublicUrl(File
$file, $relativeToCurrentScript = false
)
35 $videoId = $this->getOnlineMediaId($file);
36 return sprintf('https://www.youtube.com/watch?v=%s', $videoId);
40 * Get local absolute file path to preview image
45 public function getPreviewImage(File
$file)
47 $videoId = $this->getOnlineMediaId($file);
48 $temporaryFileName = $this->getTempFolderPath() . 'youtube_' . md5($videoId) . '.jpg';
50 if (!file_exists($temporaryFileName)) {
51 $tryNames = ['maxresdefault.jpg', '0.jpg'];
52 foreach ($tryNames as $tryName) {
53 $previewImage = GeneralUtility
::getUrl(
54 sprintf('https://img.youtube.com/vi/%s/%s', $videoId, $tryName)
56 if ($previewImage !== false
) {
57 file_put_contents($temporaryFileName, $previewImage);
58 GeneralUtility
::fixPermissions($temporaryFileName);
64 return $temporaryFileName;
68 * Try to transform given URL to a File
71 * @param Folder $targetFolder
74 public function transformUrlToFile($url, Folder
$targetFolder)
77 // Try to get the YouTube code from given url.
78 // These formats are supported with and without http(s)://
79 // - youtu.be/<code> # Share URL
80 // - www.youtube.com/watch?v=<code> # Normal web link
81 // - www.youtube.com/v/<code>
82 // - www.youtube-nocookie.com/v/<code> # youtube-nocookie.com web link
83 // - www.youtube.com/embed/<code> # URL form iframe embed code, can also get code from full iframe snippet
84 if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
87 if (empty($videoId)) {
90 return $this->transformMediaIdToFile($videoId, $targetFolder, $this->extension
);
94 * Get oEmbed url to retrieve oEmbed data
96 * @param string $mediaId
97 * @param string $format
100 protected function getOEmbedUrl($mediaId, $format = 'json')
103 'https://www.youtube.com/oembed?url=%s&format=%s',
104 urlencode(sprintf('https://www.youtube.com/watch?v=%s', $mediaId)),
105 rawurlencode($format)