139 lines
4.5 KiB
PHP
139 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Codappix\ExternalTypo3Multimedia\Facebook\Video;
|
|
|
|
/*
|
|
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
use Codappix\ExternalTypo3Multimedia\AbstractOnlineMediaHelper;
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
use TYPO3\CMS\Core\Http\RequestFactory;
|
|
use TYPO3\CMS\Core\Http\Uri;
|
|
use TYPO3\CMS\Core\Resource\File;
|
|
use TYPO3\CMS\Core\Resource\Folder;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
class OnlineMediaHelper extends AbstractOnlineMediaHelper
|
|
{
|
|
public function transformUrlToFile($url, Folder $targetFolder)
|
|
{
|
|
$mediaId = null;
|
|
$uri = new Uri($url);
|
|
if (
|
|
$uri->getHost() === Registration::DOMAIN
|
|
|| $uri->getHost() === 'www.facebook.com'
|
|
) {
|
|
$mediaId = trim($uri->getPath(), '/');
|
|
}
|
|
|
|
if ($mediaId === null) {
|
|
return null;
|
|
}
|
|
|
|
$file = $this->findExistingFileByOnlineMediaId($mediaId, $targetFolder, $this->extension);
|
|
if ($file !== null) {
|
|
return $file;
|
|
}
|
|
|
|
$fileName = $mediaId . '.' . $this->extension;
|
|
$remoteData = $this->getRemoteData($mediaId);
|
|
if (!empty($remoteData['title'])) {
|
|
$fileName = $remoteData['title'] . '.' . $this->extension;
|
|
}
|
|
|
|
return $this->createNewFile($targetFolder, $fileName, $mediaId);
|
|
}
|
|
|
|
public function getPublicUrl(File $file, $relativeToCurrentScript = false)
|
|
{
|
|
return sprintf(
|
|
'https://' . Registration::DOMAIN . '/%s',
|
|
$this->getOnlineMediaId($file)
|
|
);
|
|
}
|
|
|
|
public function getPreviewImage(File $file)
|
|
{
|
|
$data = $this->getRemoteData($this->getOnlineMediaId($file));
|
|
if (isset($data['thumbnail_url']) === false) {
|
|
return '';
|
|
}
|
|
|
|
$imageContent = GeneralUtility::getUrl($data['thumbnail_url']);
|
|
if ($imageContent === false) {
|
|
return '';
|
|
}
|
|
|
|
$tempName = GeneralUtility::tempnam($file->getNameWithoutExtension(), $file->getExtension());
|
|
GeneralUtility::writeFile($tempName, $imageContent);
|
|
|
|
return $tempName;
|
|
}
|
|
|
|
public function getMetaData(File $file)
|
|
{
|
|
$remoteData = $this->getRemoteData($this->getOnlineMediaId($file));
|
|
if ($remoteData === []) {
|
|
return [];
|
|
}
|
|
|
|
$metadata = [
|
|
'width' => (int)$remoteData['width'],
|
|
'height' => (int)$remoteData['height'],
|
|
];
|
|
|
|
if (empty($file->getProperty('title'))) {
|
|
$metadata['title'] = strip_tags($remoteData['title']);
|
|
}
|
|
|
|
if (empty($file->getProperty('description'))) {
|
|
$metadata['description'] = strip_tags($remoteData['description']);
|
|
}
|
|
|
|
return $metadata;
|
|
}
|
|
|
|
private function getRemoteData(string $mediaId): array
|
|
{
|
|
static $data = [];
|
|
|
|
if (isset($data[$mediaId]) !== false) {
|
|
return $data[$mediaId];
|
|
}
|
|
|
|
$videoWebsite = $this->getRequestFactory()->request(
|
|
'https://www.facebook.com/' . $mediaId
|
|
)->getBody()->getContents();
|
|
if ($videoWebsite === '') {
|
|
return [];
|
|
}
|
|
|
|
$domCrawler = new Crawler($videoWebsite);
|
|
$remoteData = [
|
|
'title' => $domCrawler->filter('meta[property="og:title"]')->first()->attr('content'),
|
|
'description' => $domCrawler->filter('meta[name="description"]')->first()->attr('content'),
|
|
'width' => (int) $domCrawler->filter('meta[property="og:video:width"]')->first()->attr('content'),
|
|
'height' => (int) $domCrawler->filter('meta[property="og:video:height"]')->first()->attr('content'),
|
|
'thumbnail_url' => $domCrawler->filter('meta[property="og:image"]')->first()->attr('content'),
|
|
];
|
|
|
|
$data[$mediaId] = $remoteData;
|
|
return $remoteData;
|
|
}
|
|
}
|