generated from alexplusde/blaupause
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from alexplusde/iframe_manager_thumb
YouTube und Vimeo Thumbnail-Proxy für Iframe Manager
- Loading branch information
Showing
6 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Alexplusde\Wsm; | ||
|
||
use rex_api_function; | ||
use rex_file; | ||
use rex_logger; | ||
use rex_path; | ||
use rex_response; | ||
use rex_socket; | ||
use rex_socket_exception; | ||
use rex_string; | ||
use rex_url; | ||
|
||
use function array_key_exists; | ||
use function is_array; | ||
|
||
class ImThumbnail extends rex_api_function | ||
{ | ||
protected $published = true; | ||
|
||
public function execute() | ||
{ | ||
$service = rex_get('service', 'string', ''); | ||
$id = rex_get('id', 'string', ''); | ||
|
||
if ('' === $id) { | ||
exit; | ||
} | ||
|
||
if ('vimeo' === $service) { | ||
$response = rex_socket::factoryUrl('https://vimeo.com/api/v2/video/' . $id . '.json')->doGet(); | ||
if ($response->isOk()) { | ||
// liest die Informationen aus der Datei | ||
$body = json_decode($response->getBody()); | ||
} | ||
if (is_array($body) && array_key_exists(0, $body) && property_exists($body[0], 'thumbnail_large')) { | ||
$url = self::getImgFromVimeo($id, $body[0]->thumbnail_large); | ||
rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg'); | ||
} | ||
} | ||
|
||
if ('youtube' === $service) { | ||
$url = self::getImgFromYoutube($id); | ||
rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg'); | ||
} | ||
|
||
rex_response::setStatus('404'); | ||
exit; | ||
} | ||
|
||
private static function getImgFromYoutube(string $id): string | ||
{ | ||
if (null !== rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)))) { | ||
return self::getThumbUrl('youtube', $id); | ||
} | ||
|
||
try { | ||
$socket = rex_socket::factory('i3.ytimg.com', 443, true); | ||
$socket->setPath('/vi/' . $id . '/hqdefault.jpg'); | ||
$socket->followRedirects(1); | ||
|
||
$response = $socket->doGet(); | ||
|
||
if ($response->isOk()) { | ||
$image_path = rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)); | ||
$response->writeBodyTo($image_path); | ||
return self::getThumbUrl('youtube', $id); | ||
} | ||
} catch (rex_socket_exception $e) { | ||
rex_logger::factory()->notice($e->getMessage()); | ||
} | ||
return ''; | ||
} | ||
|
||
private static function getImgFromVimeo(string $id, string $url): string | ||
{ | ||
if (null !== rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('vimeo', $id)))) { | ||
return self::getThumbUrl('vimeo', $id); | ||
} | ||
|
||
try { | ||
$socket = rex_socket::factoryUrl($url); | ||
$response = $socket->doGet(); | ||
|
||
if ($response->isOk()) { | ||
$image_path = rex_path::addonData('wenns_sein_muss', self::generateFileName('vimeo', $id)); | ||
$response->writeBodyTo($image_path); | ||
return self::getThumbUrl('vimeo', $id); | ||
} | ||
} catch (rex_socket_exception $e) { | ||
rex_logger::factory()->notice($e->getMessage()); | ||
} | ||
return ''; | ||
} | ||
|
||
private static function generateFileName(string $service, string $id, string $filetype = '.jpg'): string | ||
{ | ||
return rex_string::normalize($service . '_' . $id) . $filetype; | ||
} | ||
|
||
public static function getThumbUrl(string $service, string $id, string $filetype = '.jpg'): string | ||
{ | ||
$filename = self::generateFilename($service, $id, $filetype); | ||
$file = rex_path::addonData('wenns_sein_muss', $filename); | ||
|
||
$timestamp = filemtime($file); | ||
$frontend_url = rex_url::media() . Wsm::getConfig('media_manager_type', 'string', 'wsm') . '/' . $filename . '?timestamp=' . $timestamp; | ||
return $frontend_url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters