[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / OnlineMedia / Helpers / VimeoHelper.php
1 <?php
2 namespace 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
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Vimeo helper class
23 */
24 class VimeoHelper extends AbstractOEmbedHelper
25 {
26 /**
27 * Get public url
28 * Return NULL if you want to use core default behaviour
29 *
30 * @param File $file
31 * @param bool $relativeToCurrentScript
32 * @return string|null
33 */
34 public function getPublicUrl(File $file, $relativeToCurrentScript = false)
35 {
36 $videoId = $this->getOnlineMediaId($file);
37 return sprintf('https://vimeo.com/%s', $videoId);
38 }
39
40 /**
41 * Get local absolute file path to preview image
42 *
43 * @param File $file
44 * @return string
45 */
46 public function getPreviewImage(File $file)
47 {
48 $videoId = $this->getOnlineMediaId($file);
49 $temporaryFileName = $this->getTempFolderPath() . 'vimeo_' . md5($videoId) . '.jpg';
50 if (!file_exists($temporaryFileName)) {
51 $oEmbedData = $this->getOEmbedData($videoId);
52 $previewImage = GeneralUtility::getUrl($oEmbedData['thumbnail_url']);
53 if ($previewImage !== false) {
54 file_put_contents($temporaryFileName, $previewImage);
55 GeneralUtility::fixPermissions($temporaryFileName);
56 }
57 }
58 return $temporaryFileName;
59 }
60
61 /**
62 * Try to transform given URL to a File
63 *
64 * @param string $url
65 * @param Folder $targetFolder
66 * @return File|null
67 */
68 public function transformUrlToFile($url, Folder $targetFolder)
69 {
70 $videoId = null;
71 // Try to get the Vimeo code from given url.
72 // Next formats are supported with and without http(s)://
73 // - vimeo.com/<code> # Share URL
74 // - player.vimeo.com/video/<code> # URL form iframe embed code, can also get code from full iframe snippet
75 if (preg_match('/vimeo\.com\/(video\/)*([0-9]+)/i', $url, $matches)) {
76 $videoId = $matches[2];
77 }
78 if (empty($videoId)) {
79 return null;
80 }
81 return $this->transformMediaIdToFile($videoId, $targetFolder, $this->extension);
82 }
83
84 /**
85 * Get oEmbed data url
86 *
87 * @param string $mediaId
88 * @param string $format
89 * @return string
90 */
91 protected function getOEmbedUrl($mediaId, $format = 'json')
92 {
93 return sprintf(
94 'https://vimeo.com/api/oembed.%s?url=%s',
95 urlencode($format),
96 urlencode(sprintf('https://vimeo.com/%s', $mediaId))
97 );
98 }
99 }