Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YouTube und Vimeo Thumbnail-Proxy für Iframe Manager #68

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
40 changes: 40 additions & 0 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,43 @@
}

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();
}
43 changes: 0 additions & 43 deletions lib/Api/Iframe.php

This file was deleted.

111 changes: 111 additions & 0 deletions lib/Api/ImThumbnail.php
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;
}
}
2 changes: 1 addition & 1 deletion lib/Wsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) . ' <a rel="noreferrer noopener" href="' . $service->getValue('privacy_policy_url') . '" target="_blank">' . self::getConfigText('iframe_notice_more') . '</a>';
$s['languages'][rex_clang::getCurrent()->getCode()]['loadBtn'] = self::getConfigText('iframe_load_btn');
Expand Down
1 change: 1 addition & 0 deletions package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading