From b3b67ec6be324523162a520aa33b481a8c517a2c Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Fri, 13 Sep 2024 17:37:43 +0200 Subject: [PATCH 1/4] Youtube & Vimeo Thumbnail lokal zwischenspeichern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benötigt aktuell einen extra Effekt --- lib/Api/Iframe.php | 84 ++++++++++++++++++++++++++++++++++++++++++---- package.yml | 1 + 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/lib/Api/Iframe.php b/lib/Api/Iframe.php index c9b985c..0a03395 100644 --- a/lib/Api/Iframe.php +++ b/lib/Api/Iframe.php @@ -5,6 +5,10 @@ use rex_api_function; use rex_response; use rex_socket; +use rex_string; +use rex_path; +use rex_file; +use rex_url; use function array_key_exists; use function is_array; @@ -16,28 +20,94 @@ class ApiWsmIframe extends rex_api_function public function execute() { $service = rex_get('service', 'string', ''); - $param = rex_get('id', 'string', ''); - if ('' === $param) { + $id = rex_get('id', 'string', ''); + + if ('' === $id) { exit; } if ('vimeo' === $service) { - $response = rex_socket::factoryUrl('https://vimeo.com/api/v2/video/' . $param . '.json')->doGet(); + $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()); } - rex_response::setStatus('301'); - if (is_array($body) && array_key_exists(0, $body) && property_exists($body[0], 'thumbnail_large')) { - rex_response::sendRedirect($body[0]->thumbnail_large); + 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::setStatus('301'); + rex_response::sendRedirect($url); } } if ('youtube' === $service) { + $url = self::getImgFromYoutube($id); rex_response::setStatus('301'); - rex_response::sendRedirect('https://i3.ytimg.com/vi/' . $param . '/hqdefault.jpg'); + rex_response::sendRedirect($url); } 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; + } } diff --git a/package.yml b/package.yml index 9c5fe83..3df245a 100644 --- a/package.yml +++ b/package.yml @@ -77,6 +77,7 @@ default_config: iframemanager: 1 disable_page_interaction: 0 first_install: 1 + media_manager_type: 'wsm' # settings page: consent consent_modal_title: Diese Webseite verwendet Cookies und Drittanbieter-Dienste From 0d07398a63392b2994f25ed862762acd3c8fa908 Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Fri, 13 Sep 2024 17:41:07 +0200 Subject: [PATCH 2/4] Profile anlegen beim Installieren --- install.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/install.php b/install.php index 96bdb43..3d4a344 100644 --- a/install.php +++ b/install.php @@ -78,3 +78,44 @@ } rex_config::set('wenns_sein_muss', 'lastchange', date('Y-m-d H:i:s')); + + +rex_dir::create(rex_path::addonData('wenns_sein_muss')); + +/* via rex_sql überprüfen, ob es bereits ein Media Manager Profil gibt - wenn nicht, dann anlegen */ +$media_manager_types = rex_sql::factory()->setQuery('SELECT * FROM ' . rex::getTable('media_manager_type'))->getArray(); +$profile_exists = false; +foreach ($media_manager_types as $profile) { + if ($profile['name'] === rex_config::get('wenns_sein_muss', 'media_manager_type')) { + $profile_exists = true; + break; + } +} + +if(!$profile_exists) { + $sql = rex_sql::factory(); + $sql->setTable(rex::getTable('media_manager_type')); + $sql->setValue('name', 'wenns_sein_muss'); + $sql->setValue('status', 0); + $sql->setValue('description', ''); + $sql->setValue('createdate', date('Y-m-d H:i:s')); + $sql->setValue('createuser', 'wenns_sein_muss'); + $sql->setValue('updatedate', date('Y-m-d H:i:s')); + $sql->setValue('updateuser', 'wenns_sein_muss'); + $sql->insert(); + + /* ID des letzten Datensatzes ermitteln */ + $profile_id = rex_sql::factory()->getLastId(); + + $sql = rex_sql::factory(); + $sql->setTable(rex::getTable('media_manager_type_effect')); + $sql->setValue('type_id', $profile_id); + $sql->setValue('effect', 'mediapath'); + $sql->setValue('parameters', '{"rex_effect_rounded_corners":{"rex_effect_rounded_corners_topleft":"","rex_effect_rounded_corners_topright":"","rex_effect_rounded_corners_bottomleft":"","rex_effect_rounded_corners_bottomright":""},"rex_effect_workspace":{"rex_effect_workspace_set_transparent":"colored","rex_effect_workspace_width":"","rex_effect_workspace_height":"","rex_effect_workspace_hpos":"left","rex_effect_workspace_vpos":"top","rex_effect_workspace_padding_x":"0","rex_effect_workspace_padding_y":"0","rex_effect_workspace_bg_r":"","rex_effect_workspace_bg_g":"","rex_effect_workspace_bg_b":"","rex_effect_workspace_bgimage":""},"rex_effect_crop":{"rex_effect_crop_width":"","rex_effect_crop_height":"","rex_effect_crop_offset_width":"","rex_effect_crop_offset_height":"","rex_effect_crop_hpos":"center","rex_effect_crop_vpos":"middle"},"rex_effect_insert_image":{"rex_effect_insert_image_brandimage":"","rex_effect_insert_image_hpos":"left","rex_effect_insert_image_vpos":"top","rex_effect_insert_image_padding_x":"-10","rex_effect_insert_image_padding_y":"-10"},"rex_effect_rotate":{"rex_effect_rotate_rotate":"0"},"rex_effect_filter_colorize":{"rex_effect_filter_colorize_filter_r":"","rex_effect_filter_colorize_filter_g":"","rex_effect_filter_colorize_filter_b":""},"rex_effect_image_properties":{"rex_effect_image_properties_jpg_quality":"","rex_effect_image_properties_png_compression":"","rex_effect_image_properties_webp_quality":"","rex_effect_image_properties_avif_quality":"","rex_effect_image_properties_avif_speed":"","rex_effect_image_properties_interlace":null},"rex_effect_filter_brightness":{"rex_effect_filter_brightness_brightness":""},"rex_effect_flip":{"rex_effect_flip_flip":"X"},"rex_effect_image_format":{"rex_effect_image_format_convert_to":"webp"},"rex_effect_filter_contrast":{"rex_effect_filter_contrast_contrast":""},"rex_effect_filter_sharpen":{"rex_effect_filter_sharpen_amount":"80","rex_effect_filter_sharpen_radius":"0.5","rex_effect_filter_sharpen_threshold":"3"},"rex_effect_resize":{"rex_effect_resize_width":"","rex_effect_resize_height":"","rex_effect_resize_style":"maximum","rex_effect_resize_allow_enlarge":"enlarge"},"rex_effect_filter_blur":{"rex_effect_filter_blur_repeats":"10","rex_effect_filter_blur_type":"gaussian","rex_effect_filter_blur_smoothit":""},"rex_effect_mirror":{"rex_effect_mirror_height":"","rex_effect_mirror_opacity":"100","rex_effect_mirror_set_transparent":"colored","rex_effect_mirror_bg_r":"","rex_effect_mirror_bg_g":"","rex_effect_mirror_bg_b":""},"rex_effect_header":{"rex_effect_header_download":"open_media","rex_effect_header_cache":"no_cache","rex_effect_header_filename":"filename","rex_effect_header_index":"index"},"rex_effect_convert2img":{"rex_effect_convert2img_convert_to":"jpg","rex_effect_convert2img_density":"150","rex_effect_convert2img_color":""},"rex_effect_mediapath":{"rex_effect_mediapath_mediapath":"..\\/var\\/data\\/addons\\/wenns_sein_muss"}}'); + $sql->setValue('priority', 1); + $sql->setValue('createdate', date('Y-m-d H:i:s')); + $sql->setValue('createuser', 'wenns_sein_muss'); + $sql->setValue('updatedate', date('Y-m-d H:i:s')); + $sql->setValue('updateuser', 'wenns_sein_muss'); + $sql->insert(); +} From 14e0a1b9538b0e429cca7d8d160e0a5a06e1721e Mon Sep 17 00:00:00 2001 From: alxndr-w Date: Fri, 13 Sep 2024 15:43:31 +0000 Subject: [PATCH 3/4] Apply php-cs-fixer changes --- install.php | 3 +-- lib/Api/Iframe.php | 37 ++++++++++++++++++------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/install.php b/install.php index 3d4a344..d64d37e 100644 --- a/install.php +++ b/install.php @@ -79,7 +79,6 @@ rex_config::set('wenns_sein_muss', 'lastchange', date('Y-m-d H:i:s')); - rex_dir::create(rex_path::addonData('wenns_sein_muss')); /* via rex_sql überprüfen, ob es bereits ein Media Manager Profil gibt - wenn nicht, dann anlegen */ @@ -92,7 +91,7 @@ } } -if(!$profile_exists) { +if (!$profile_exists) { $sql = rex_sql::factory(); $sql->setTable(rex::getTable('media_manager_type')); $sql->setValue('name', 'wenns_sein_muss'); diff --git a/lib/Api/Iframe.php b/lib/Api/Iframe.php index 0a03395..de8d0e6 100644 --- a/lib/Api/Iframe.php +++ b/lib/Api/Iframe.php @@ -3,11 +3,13 @@ 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_path; -use rex_file; use rex_url; use function array_key_exists; @@ -32,7 +34,7 @@ public function execute() // 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')) { + 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::setStatus('301'); rex_response::sendRedirect($url); @@ -48,11 +50,9 @@ public function execute() exit; } - - private static function getImgFromYoutube(string $id) :string + private static function getImgFromYoutube(string $id): string { - - if (null !== \rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)))) { + if (null !== rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)))) { return self::getThumbUrl('youtube', $id); } @@ -60,7 +60,7 @@ private static function getImgFromYoutube(string $id) :string $socket = rex_socket::factory('i3.ytimg.com', 443, true); $socket->setPath('/vi/' . $id . '/hqdefault.jpg'); $socket->followRedirects(1); - + $response = $socket->doGet(); if ($response->isOk()) { @@ -68,16 +68,15 @@ private static function getImgFromYoutube(string $id) :string $response->writeBodyTo($image_path); return self::getThumbUrl('youtube', $id); } - } catch(\rex_socket_exception $e) { - \rex_logger::factory()->notice($e->getMessage()); + } catch (rex_socket_exception $e) { + rex_logger::factory()->notice($e->getMessage()); } return ''; } - private static function getImgFromVimeo(string $id, string $url) :string + private static function getImgFromVimeo(string $id, string $url): string { - - if (null !== \rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('vimeo', $id)))) { + if (null !== rex_file::get(rex_path::addonData('wenns_sein_muss', self::generateFileName('vimeo', $id)))) { return self::getThumbUrl('vimeo', $id); } @@ -90,24 +89,24 @@ private static function getImgFromVimeo(string $id, string $url) :string $response->writeBodyTo($image_path); return self::getThumbUrl('vimeo', $id); } - } catch(\rex_socket_exception $e) { - \rex_logger::factory()->notice($e->getMessage()); + } catch (rex_socket_exception $e) { + rex_logger::factory()->notice($e->getMessage()); } return ''; } - private static function generateFileName(string $service = '', string $id, string $filetype = '.jpg') :string + 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 + + 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; + $frontend_url = rex_url::media() . Wsm::getConfig('media_manager_type', 'string', 'wsm') . '/' . $filename . '?timestamp=' . $timestamp; return $frontend_url; } } From 26ee4cc5e70489b5851ad0d7e2696973f37cade0 Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Fri, 13 Sep 2024 18:18:50 +0200 Subject: [PATCH 4/4] final class name, api name and fix small errors --- boot.php | 2 +- lib/Api/{Iframe.php => ImThumbnail.php} | 11 +++++------ lib/Wsm.php | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) rename lib/Api/{Iframe.php => ImThumbnail.php} (89%) diff --git a/boot.php b/boot.php index 6989fee..2388d2e 100644 --- a/boot.php +++ b/boot.php @@ -57,7 +57,7 @@ */ if (rex::isFrontend()) { rex_api_function::register('wsm', ApiWsm::class); - rex_api_function::register('iframe', ApiWsmIframe::class); + rex_api_function::register('wsm_im_thumbnail', ImThumbnail::class); return; } diff --git a/lib/Api/Iframe.php b/lib/Api/ImThumbnail.php similarity index 89% rename from lib/Api/Iframe.php rename to lib/Api/ImThumbnail.php index 0a03395..ba6cbb0 100644 --- a/lib/Api/Iframe.php +++ b/lib/Api/ImThumbnail.php @@ -13,7 +13,7 @@ use function array_key_exists; use function is_array; -class ApiWsmIframe extends rex_api_function +class ImThumbnail extends rex_api_function { protected $published = true; @@ -32,19 +32,18 @@ public function execute() // 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')) { + 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::setStatus('301'); - rex_response::sendRedirect($url); + rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg'); } } if ('youtube' === $service) { $url = self::getImgFromYoutube($id); - rex_response::setStatus('301'); - rex_response::sendRedirect($url); + rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg'); } + rex_response::setStatus('404'); exit; } diff --git a/lib/Wsm.php b/lib/Wsm.php index 0553829..f68c05d 100644 --- a/lib/Wsm.php +++ b/lib/Wsm.php @@ -98,7 +98,7 @@ public static function getIframeServicesAsArray(): array $s = []; $s['embedUrl'] = $iframe->getValue('embedUrl'); - $s['thumbnail'] = urldecode(rex_getUrl(null, null, ['rex-api-call' => 'wsm_iframe', 'service' => rex_string::normalize($service->getValue('service')), 'id' => '{data_id}'], '&')); + $s['thumbnailUrl'] = urldecode(rex_getUrl(null, null, ['rex-api-call' => 'wsm_im_thumbnail', 'service' => rex_string::normalize($service->getValue('service')), 'id' => '{data-id}'], '&')); // $s['iframe'] = $iframe->getValue('attributes'); $s['languages'][rex_clang::getCurrent()->getCode()]['notice'] = rex_formatter::sprintf($service->getCompanyName(), self::getConfigText('iframe_notice')) . ' ' . self::getConfigText('iframe_notice_more') . ''; $s['languages'][rex_clang::getCurrent()->getCode()]['loadBtn'] = self::getConfigText('iframe_load_btn');