Initial Upload
This commit is contained in:
517
lib/Widget/Render/WidgetDataProviderCache.php
Normal file
517
lib/Widget/Render/WidgetDataProviderCache.php
Normal file
@@ -0,0 +1,517 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2025 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - https://xibosignage.com
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Xibo\Widget\Render;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Stash\Interfaces\PoolInterface;
|
||||
use Stash\Invalidation;
|
||||
use Stash\Item;
|
||||
use Xibo\Entity\Display;
|
||||
use Xibo\Helper\DateFormatHelper;
|
||||
use Xibo\Helper\LinkSigner;
|
||||
use Xibo\Helper\ObjectVars;
|
||||
use Xibo\Service\ConfigServiceInterface;
|
||||
use Xibo\Support\Exception\GeneralException;
|
||||
use Xibo\Widget\Provider\DataProvider;
|
||||
use Xibo\Widget\Provider\DataProviderInterface;
|
||||
use Xibo\Xmds\Wsdl;
|
||||
|
||||
/**
|
||||
* Acts as a cache for the Widget data cache.
|
||||
*/
|
||||
class WidgetDataProviderCache
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
/** @var \Stash\Interfaces\PoolInterface */
|
||||
private $pool;
|
||||
|
||||
/** @var Item */
|
||||
private $lock;
|
||||
|
||||
/** @var Item */
|
||||
private $cache;
|
||||
|
||||
/** @var string The cache key */
|
||||
private $key;
|
||||
|
||||
/** @var bool Is the cache a miss or old */
|
||||
private $isMissOrOld = true;
|
||||
|
||||
private $cachedMediaIds;
|
||||
|
||||
/**
|
||||
* @param \Stash\Interfaces\PoolInterface $pool
|
||||
*/
|
||||
public function __construct(PoolInterface $pool)
|
||||
{
|
||||
$this->pool = $pool;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* @return $this
|
||||
*/
|
||||
public function useLogger(LoggerInterface $logger): WidgetDataProviderCache
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Psr\Log\LoggerInterface
|
||||
*/
|
||||
private function getLog(): LoggerInterface
|
||||
{
|
||||
if ($this->logger === null) {
|
||||
$this->logger = new NullLogger();
|
||||
}
|
||||
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate this data provider with cache
|
||||
* @param DataProvider $dataProvider
|
||||
* @param string $cacheKey
|
||||
* @param Carbon|null $dataModifiedDt The date any associated data was modified.
|
||||
* @param bool $isLockIfMiss Should the cache be locked if it's a miss? Defaults to true.
|
||||
* @return bool
|
||||
* @throws \Xibo\Support\Exception\GeneralException
|
||||
*/
|
||||
public function decorateWithCache(
|
||||
DataProvider $dataProvider,
|
||||
string $cacheKey,
|
||||
?Carbon $dataModifiedDt,
|
||||
bool $isLockIfMiss = true,
|
||||
): bool {
|
||||
// Construct a key
|
||||
$this->key = '/widget/'
|
||||
. ($dataProvider->getDataType() ?: $dataProvider->getDataSource())
|
||||
. '/' . md5($cacheKey);
|
||||
|
||||
$this->getLog()->debug('decorateWithCache: key is ' . $this->key);
|
||||
|
||||
// Get the cache
|
||||
$this->cache = $this->pool->getItem($this->key);
|
||||
|
||||
// Invalidation method old means that if this cache key is being regenerated concurrently to this request
|
||||
// we return the old data we have stored already.
|
||||
$this->cache->setInvalidationMethod(Invalidation::OLD);
|
||||
|
||||
// Get the data (this might be OLD data)
|
||||
$data = $this->cache->get();
|
||||
$cacheCreationDt = $this->cache->getCreation();
|
||||
|
||||
// Does the cache have data?
|
||||
// we keep data 50% longer than we need to, so that it has a chance to be regenerated out of band
|
||||
if ($data === null) {
|
||||
$this->getLog()->debug('decorateWithCache: miss, no data');
|
||||
$hasData = false;
|
||||
} else {
|
||||
$hasData = true;
|
||||
|
||||
// Clear the data provider and add the cached items back to it.
|
||||
$dataProvider->clearData();
|
||||
$dataProvider->clearMeta();
|
||||
$dataProvider->addItems($data->data ?? []);
|
||||
|
||||
// Record any cached mediaIds
|
||||
$this->cachedMediaIds = $data->media ?? [];
|
||||
|
||||
// Update any meta
|
||||
foreach (($data->meta ?? []) as $key => $item) {
|
||||
$dataProvider->addOrUpdateMeta($key, $item);
|
||||
}
|
||||
|
||||
// Determine whether this cache is a miss (i.e. expired and being regenerated, expired, out of date)
|
||||
// We use our own expireDt here because Stash will only return expired data with invalidation method OLD
|
||||
// if the data is currently being regenerated and another process has called lock() on it
|
||||
$expireDt = $dataProvider->getMeta()['expireDt'] ?? null;
|
||||
if ($expireDt !== null) {
|
||||
$expireDt = Carbon::createFromFormat('c', $expireDt);
|
||||
} else {
|
||||
$expireDt = $this->cache->getExpiration();
|
||||
}
|
||||
|
||||
// Determine if the cache returned is a miss or older than the modified/expired dates
|
||||
$this->isMissOrOld = $this->cache->isMiss()
|
||||
|| ($dataModifiedDt !== null && $cacheCreationDt !== false && $dataModifiedDt->isAfter($cacheCreationDt)
|
||||
|| ($expireDt->isBefore(Carbon::now()))
|
||||
);
|
||||
|
||||
$this->getLog()->debug('decorateWithCache: cache has data, is miss or old: '
|
||||
. var_export($this->isMissOrOld, true));
|
||||
}
|
||||
|
||||
// If we do not have data/we're old/missed cache, and we have requested a lock, then we will be refreshing
|
||||
// the cache, so lock the record
|
||||
if ($isLockIfMiss && (!$hasData || $this->isMissOrOld)) {
|
||||
$this->concurrentRequestLock();
|
||||
}
|
||||
|
||||
return $hasData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the cache a miss, or old data.
|
||||
* @return bool
|
||||
*/
|
||||
public function isCacheMissOrOld(): bool
|
||||
{
|
||||
return $this->isMissOrOld;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cache date for this data provider and key
|
||||
* @param DataProvider $dataProvider
|
||||
* @param string $cacheKey
|
||||
* @return Carbon|null
|
||||
*/
|
||||
public function getCacheDate(DataProvider $dataProvider, string $cacheKey): ?Carbon
|
||||
{
|
||||
// Construct a key
|
||||
$this->key = '/widget/'
|
||||
. ($dataProvider->getDataType() ?: $dataProvider->getDataSource())
|
||||
. '/' . md5($cacheKey);
|
||||
|
||||
$this->getLog()->debug('getCacheDate: key is ' . $this->key);
|
||||
|
||||
// Get the cache
|
||||
$this->cache = $this->pool->getItem($this->key);
|
||||
$cacheCreationDt = $this->cache->getCreation();
|
||||
|
||||
return $cacheCreationDt ? Carbon::instance($cacheCreationDt) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataProviderInterface $dataProvider
|
||||
* @throws \Xibo\Support\Exception\GeneralException
|
||||
*/
|
||||
public function saveToCache(DataProviderInterface $dataProvider): void
|
||||
{
|
||||
if ($this->cache === null) {
|
||||
throw new GeneralException('No cache to save');
|
||||
}
|
||||
|
||||
// Set some cache dates so that we can track when this data provider was cached and when it should expire.
|
||||
$dataProvider->addOrUpdateMeta('cacheDt', Carbon::now()->format('c'));
|
||||
$dataProvider->addOrUpdateMeta(
|
||||
'expireDt',
|
||||
Carbon::now()->addSeconds($dataProvider->getCacheTtl())->format('c')
|
||||
);
|
||||
|
||||
// Set our cache from the data provider.
|
||||
$object = new \stdClass();
|
||||
$object->data = $dataProvider->getData();
|
||||
$object->meta = $dataProvider->getMeta();
|
||||
$object->media = $dataProvider->getImageIds();
|
||||
$cached = $this->cache->set($object);
|
||||
|
||||
if (!$cached) {
|
||||
throw new GeneralException('Cache failure');
|
||||
}
|
||||
|
||||
// Keep the cache 50% longer than necessary
|
||||
// The expireDt must always be 15 minutes to allow plenty of time for the WidgetSyncTask to regenerate.
|
||||
$this->cache->expiresAfter(ceil(max($dataProvider->getCacheTtl() * 1.5, 900)));
|
||||
|
||||
// Save to the pool
|
||||
$this->pool->save($this->cache);
|
||||
|
||||
$this->getLog()->debug('saveToCache: cached ' . $this->key
|
||||
. ' for ' . $dataProvider->getCacheTtl() . ' seconds');
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalise the cache process
|
||||
*/
|
||||
public function finaliseCache(): void
|
||||
{
|
||||
$this->concurrentRequestRelease();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return any cached mediaIds
|
||||
* @return array
|
||||
*/
|
||||
public function getCachedMediaIds(): array
|
||||
{
|
||||
return $this->cachedMediaIds ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate for a preview
|
||||
* @param array $data The data
|
||||
* @param callable $urlFor
|
||||
* @return array
|
||||
*/
|
||||
public function decorateForPreview(array $data, callable $urlFor): array
|
||||
{
|
||||
foreach ($data as $row => $item) {
|
||||
// This is either an object or an array
|
||||
if (is_array($item)) {
|
||||
foreach ($item as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$data[$row][$key] = $this->decorateMediaForPreview($urlFor, $value);
|
||||
}
|
||||
}
|
||||
} else if (is_object($item)) {
|
||||
foreach (ObjectVars::getObjectVars($item) as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$item->{$key} = $this->decorateMediaForPreview($urlFor, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $urlFor
|
||||
* @param string|null $data
|
||||
* @return string|null
|
||||
*/
|
||||
private function decorateMediaForPreview(callable $urlFor, ?string $data): ?string
|
||||
{
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
$matches = [];
|
||||
preg_match_all('/\[\[(.*?)\]\]/', $data, $matches);
|
||||
foreach ($matches[1] as $match) {
|
||||
if (Str::startsWith($match, 'mediaId')) {
|
||||
$value = explode('=', $match);
|
||||
$data = str_replace(
|
||||
'[[' . $match . ']]',
|
||||
$urlFor('library.download', ['id' => $value[1], 'type' => 'image']),
|
||||
$data
|
||||
);
|
||||
} else if (Str::startsWith($match, 'connector')) {
|
||||
$value = explode('=', $match);
|
||||
$data = str_replace(
|
||||
'[[' . $match . ']]',
|
||||
$urlFor('layout.preview.connector', [], ['token' => $value[1], 'isDebug' => 1]),
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate for a player
|
||||
* @param \Xibo\Service\ConfigServiceInterface $configService
|
||||
* @param \Xibo\Entity\Display $display
|
||||
* @param string $encryptionKey
|
||||
* @param array $data The data
|
||||
* @param array $storedAs A keyed array of module files this widget has access to
|
||||
* @return array
|
||||
* @throws \Xibo\Support\Exception\NotFoundException
|
||||
*/
|
||||
public function decorateForPlayer(
|
||||
ConfigServiceInterface $configService,
|
||||
Display $display,
|
||||
string $encryptionKey,
|
||||
array $data,
|
||||
array $storedAs,
|
||||
): array {
|
||||
$this->getLog()->debug('decorateForPlayer');
|
||||
|
||||
$cdnUrl = $configService->getSetting('CDN_URL');
|
||||
|
||||
foreach ($data as $row => $item) {
|
||||
// Each data item can be an array or an object
|
||||
if (is_array($item)) {
|
||||
foreach ($item as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$data[$row][$key] = $this->decorateMediaForPlayer(
|
||||
$cdnUrl,
|
||||
$display,
|
||||
$encryptionKey,
|
||||
$storedAs,
|
||||
$value,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (is_object($item)) {
|
||||
foreach (ObjectVars::getObjectVars($item) as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$item->{$key} = $this->decorateMediaForPlayer(
|
||||
$cdnUrl,
|
||||
$display,
|
||||
$encryptionKey,
|
||||
$storedAs,
|
||||
$value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $cdnUrl
|
||||
* @param \Xibo\Entity\Display $display
|
||||
* @param string $encryptionKey
|
||||
* @param array $storedAs
|
||||
* @param string|null $data
|
||||
* @return string|null
|
||||
* @throws \Xibo\Support\Exception\NotFoundException
|
||||
*/
|
||||
private function decorateMediaForPlayer(
|
||||
?string $cdnUrl,
|
||||
Display $display,
|
||||
string $encryptionKey,
|
||||
array $storedAs,
|
||||
?string $data,
|
||||
): ?string {
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Do we need to add a URL prefix to the requests?
|
||||
$prefix = $display->isPwa() ? '/pwa/' : '';
|
||||
|
||||
// Media substitutes
|
||||
$matches = [];
|
||||
preg_match_all('/\[\[(.*?)\]\]/', $data, $matches);
|
||||
foreach ($matches[1] as $match) {
|
||||
if (Str::startsWith($match, 'mediaId')) {
|
||||
$value = explode('=', $match);
|
||||
if (array_key_exists($value[1], $storedAs)) {
|
||||
if ($display->isPwa()) {
|
||||
$url = LinkSigner::generateSignedLink(
|
||||
$display,
|
||||
$encryptionKey,
|
||||
$cdnUrl,
|
||||
'M',
|
||||
$value[1],
|
||||
$storedAs[$value[1]],
|
||||
null,
|
||||
true,
|
||||
);
|
||||
} else {
|
||||
$url = $storedAs[$value[1]];
|
||||
}
|
||||
$data = str_replace('[[' . $match . ']]', $prefix . $url, $data);
|
||||
} else {
|
||||
$data = str_replace('[[' . $match . ']]', '', $data);
|
||||
}
|
||||
} else if (Str::startsWith($match, 'connector')) {
|
||||
// We have WSDL here because this is only called from XMDS.
|
||||
$value = explode('=', $match);
|
||||
$data = str_replace(
|
||||
'[[' . $match . ']]',
|
||||
Wsdl::getRoot() . '?connector=true&token=' . $value[1],
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
// <editor-fold desc="Request locking">
|
||||
|
||||
/**
|
||||
* Hold a lock on concurrent requests
|
||||
* blocks if the request is locked
|
||||
* @param int $ttl seconds
|
||||
* @param int $wait seconds
|
||||
* @param int $tries
|
||||
* @throws \Xibo\Support\Exception\GeneralException
|
||||
*/
|
||||
private function concurrentRequestLock(int $ttl = 300, int $wait = 2, int $tries = 5)
|
||||
{
|
||||
if ($this->cache === null) {
|
||||
throw new GeneralException('No cache to lock');
|
||||
}
|
||||
|
||||
$this->lock = $this->pool->getItem('locks/concurrency/' . $this->cache->getKey());
|
||||
|
||||
// Set the invalidation method to simply return the value (not that we use it, but it gets us a miss on expiry)
|
||||
// isMiss() returns false if the item is missing or expired, no exceptions.
|
||||
$this->lock->setInvalidationMethod(Invalidation::NONE);
|
||||
|
||||
// Get the lock
|
||||
// other requests will wait here until we're done, or we've timed out
|
||||
$locked = $this->lock->get();
|
||||
|
||||
// Did we get a lock?
|
||||
// if we're a miss, then we're not already locked
|
||||
if ($this->lock->isMiss() || $locked === false) {
|
||||
$this->getLog()->debug('Lock miss or false. Locking for ' . $ttl
|
||||
. ' seconds. $locked is '. var_export($locked, true)
|
||||
. ', key = ' . $this->cache->getKey());
|
||||
|
||||
// so lock now
|
||||
$this->lock->set(true);
|
||||
$this->lock->expiresAfter($ttl);
|
||||
$this->lock->save();
|
||||
} else {
|
||||
// We are a hit - we must be locked
|
||||
$this->getLog()->debug('LOCK hit for ' . $this->cache->getKey() . ' expires '
|
||||
. $this->lock->getExpiration()->format(DateFormatHelper::getSystemFormat())
|
||||
. ', created ' . $this->lock->getCreation()->format(DateFormatHelper::getSystemFormat()));
|
||||
|
||||
// Try again?
|
||||
$tries--;
|
||||
|
||||
if ($tries <= 0) {
|
||||
// We've waited long enough
|
||||
throw new GeneralException('Concurrent record locked, time out.');
|
||||
} else {
|
||||
$this->getLog()->debug('Unable to get a lock, trying again. Remaining retries: ' . $tries);
|
||||
|
||||
// Hang about waiting for the lock to be released.
|
||||
sleep($wait);
|
||||
|
||||
// Recursive request (we've decremented the number of tries)
|
||||
$this->concurrentRequestLock($ttl, $wait, $tries);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a lock on concurrent requests
|
||||
*/
|
||||
private function concurrentRequestRelease()
|
||||
{
|
||||
if ($this->lock !== null) {
|
||||
$this->getLog()->debug('Releasing lock ' . $this->lock->getKey());
|
||||
|
||||
// Release lock
|
||||
$this->lock->set(false);
|
||||
$this->lock->expiresAfter(10); // Expire straight away (but give time to save)
|
||||
|
||||
$this->pool->save($this->lock);
|
||||
}
|
||||
}
|
||||
|
||||
// </editor-fold>
|
||||
}
|
||||
340
lib/Widget/Render/WidgetDownloader.php
Normal file
340
lib/Widget/Render/WidgetDownloader.php
Normal file
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2025 Xibo Signage Ltd
|
||||
*
|
||||
* Xibo - Digital Signage - https://xibosignage.com
|
||||
*
|
||||
* This file is part of Xibo.
|
||||
*
|
||||
* Xibo is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* Xibo 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Xibo\Widget\Render;
|
||||
|
||||
use GuzzleHttp\Psr7\LimitStream;
|
||||
use GuzzleHttp\Psr7\Stream;
|
||||
use Intervention\Image\ImageManagerStatic as Img;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Slim\Http\Response as Response;
|
||||
use Slim\Http\ServerRequest as Request;
|
||||
use Xibo\Entity\Media;
|
||||
use Xibo\Helper\HttpCacheProvider;
|
||||
use Xibo\Support\Exception\InvalidArgumentException;
|
||||
use Xibo\Support\Exception\NotFoundException;
|
||||
use Xibo\Support\Sanitizer\SanitizerInterface;
|
||||
|
||||
/**
|
||||
* A helper class to download widgets from the library (as media files)
|
||||
*/
|
||||
class WidgetDownloader
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* @param string $libraryLocation Library location
|
||||
* @param string $sendFileMode Send file mode
|
||||
* @param int $resizeLimit CMS resize limit
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $libraryLocation,
|
||||
private readonly string $sendFileMode,
|
||||
private readonly int $resizeLimit
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* @return $this
|
||||
*/
|
||||
public function useLogger(LoggerInterface $logger): WidgetDownloader
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return File
|
||||
* @param \Xibo\Entity\Media $media
|
||||
* @param \Slim\Http\ServerRequest $request
|
||||
* @param \Slim\Http\Response $response
|
||||
* @param string|null $contentType An optional content type, if provided the attachment is ignored
|
||||
* @param string|null $attachment An optional attachment, defaults to the stored file name (storedAs)
|
||||
* @return \Slim\Http\Response
|
||||
*/
|
||||
public function download(
|
||||
Media $media,
|
||||
Request $request,
|
||||
Response $response,
|
||||
?string $contentType = null,
|
||||
?string $attachment = null
|
||||
): Response {
|
||||
$this->logger->debug('widgetDownloader::download: Download for mediaId ' . $media->mediaId);
|
||||
|
||||
// The file path
|
||||
$libraryPath = $this->libraryLocation . $media->storedAs;
|
||||
|
||||
$this->logger->debug('widgetDownloader::download: ' . $libraryPath . ', ' . $contentType);
|
||||
|
||||
// Set some headers
|
||||
$headers = [];
|
||||
$fileSize = filesize($libraryPath);
|
||||
$headers['Content-Length'] = $fileSize;
|
||||
|
||||
// If we have been given a content type, then serve that to the browser.
|
||||
if ($contentType !== null) {
|
||||
$headers['Content-Type'] = $contentType;
|
||||
} else {
|
||||
// This widget is expected to output a file - usually this is for file based media
|
||||
// Get the name with library
|
||||
$attachmentName = empty($attachment) ? $media->storedAs : $attachment;
|
||||
|
||||
// Issue some headers
|
||||
$response = HttpCacheProvider::withEtag($response, $media->md5);
|
||||
$response = HttpCacheProvider::withExpires($response, '+1 week');
|
||||
|
||||
$headers['Content-Type'] = 'application/octet-stream';
|
||||
$headers['Content-Transfer-Encoding'] = 'Binary';
|
||||
$headers['Content-disposition'] = 'attachment; filename="' . $attachmentName . '"';
|
||||
}
|
||||
|
||||
// Output the file
|
||||
if ($this->sendFileMode === 'Apache') {
|
||||
// Send via Apache X-Sendfile header?
|
||||
$headers['X-Sendfile'] = $libraryPath;
|
||||
} else if ($this->sendFileMode === 'Nginx') {
|
||||
// Send via Nginx X-Accel-Redirect?
|
||||
$headers['X-Accel-Redirect'] = '/download/' . $media->storedAs;
|
||||
}
|
||||
|
||||
// Should we output the file via the application stack, or directly by reading the file.
|
||||
if ($this->sendFileMode == 'Off') {
|
||||
// Return the file with PHP
|
||||
$this->logger->debug('download: Returning Stream with response body, sendfile off.');
|
||||
|
||||
$stream = new Stream(fopen($libraryPath, 'r'));
|
||||
$start = 0;
|
||||
$end = $fileSize - 1;
|
||||
|
||||
$rangeHeader = $request->getHeaderLine('Range');
|
||||
if ($rangeHeader !== '') {
|
||||
$this->logger->debug('download: Handling Range request, header: ' . $rangeHeader);
|
||||
|
||||
if (preg_match('/bytes=(\d+)-(\d*)/', $rangeHeader, $matches)) {
|
||||
$start = (int) $matches[1];
|
||||
$end = $matches[2] !== '' ? (int) $matches[2] : $end;
|
||||
if ($start > $end || $end >= $fileSize) {
|
||||
return $response
|
||||
->withStatus(416)
|
||||
->withHeader('Content-Range', 'bytes */' . $fileSize);
|
||||
}
|
||||
}
|
||||
$headers['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $fileSize;
|
||||
$headers['Content-Length'] = $end - $start + 1;
|
||||
$response = $response
|
||||
->withBody(new LimitStream($stream, $end - $start + 1, $start))
|
||||
->withStatus(206);
|
||||
} else {
|
||||
$response = $response->withBody($stream);
|
||||
}
|
||||
} else {
|
||||
$this->logger->debug('Using sendfile to return the file, only output headers.');
|
||||
}
|
||||
|
||||
// Add the headers we've collected to our response
|
||||
foreach ($headers as $header => $value) {
|
||||
$response = $response->withHeader($header, $value);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a thumbnail for the given media
|
||||
* @param \Xibo\Entity\Media $media
|
||||
* @param \Slim\Http\Response $response
|
||||
* @param string|null $errorThumb
|
||||
* @return \Slim\Http\Response
|
||||
*/
|
||||
public function thumbnail(
|
||||
Media $media,
|
||||
Response $response,
|
||||
?string $errorThumb = null
|
||||
): Response {
|
||||
// Our convention is to upload media covers in {mediaId}_{mediaType}cover.png
|
||||
// and then thumbnails in tn_{mediaId}_{mediaType}cover.png
|
||||
// unless we are an image module, which is its own image, and would then have a thumbnail in
|
||||
// tn_{mediaId}_{mediaType}cover.png
|
||||
try {
|
||||
$width = 120;
|
||||
$height = 120;
|
||||
|
||||
if ($media->mediaType === 'image') {
|
||||
$filePath = $this->libraryLocation . $media->storedAs;
|
||||
$thumbnailFilePath = $this->libraryLocation . 'tn_' . $media->storedAs;
|
||||
} else {
|
||||
$filePath = $this->libraryLocation . $media->mediaId . '_'
|
||||
. $media->mediaType . 'cover.png';
|
||||
$thumbnailFilePath = $this->libraryLocation . 'tn_' . $media->mediaId . '_'
|
||||
. $media->mediaType . 'cover.png';
|
||||
|
||||
// A video cover might not exist
|
||||
if (!file_exists($filePath)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
// Does the thumbnail exist already?
|
||||
Img::configure(['driver' => 'gd']);
|
||||
$img = null;
|
||||
$regenerate = true;
|
||||
if (file_exists($thumbnailFilePath)) {
|
||||
$img = Img::make($thumbnailFilePath);
|
||||
if ($img->width() === $width || $img->height() === $height) {
|
||||
// Correct cache
|
||||
$regenerate = false;
|
||||
}
|
||||
$response = $response->withHeader('Content-Type', $img->mime());
|
||||
}
|
||||
|
||||
if ($regenerate) {
|
||||
// Check that our source image is not too large
|
||||
$imageInfo = getimagesize($filePath);
|
||||
|
||||
// Make sure none of the sides are greater than allowed
|
||||
if ($this->resizeLimit > 0
|
||||
&& ($imageInfo[0] > $this->resizeLimit || $imageInfo[1] > $this->resizeLimit)
|
||||
) {
|
||||
throw new InvalidArgumentException(__('Image too large'));
|
||||
}
|
||||
|
||||
// Get the full image and make a thumbnail
|
||||
$img = Img::make($filePath);
|
||||
$img->resize($width, $height, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$img->save($thumbnailFilePath);
|
||||
$response = $response->withHeader('Content-Type', $img->mime());
|
||||
}
|
||||
|
||||
// Output Etag
|
||||
$response = HttpCacheProvider::withEtag($response, md5_file($thumbnailFilePath));
|
||||
|
||||
$response->write($img->encode());
|
||||
} catch (\Exception) {
|
||||
$this->logger->debug('thumbnail: exception raised.');
|
||||
|
||||
if ($errorThumb !== null) {
|
||||
$img = Img::make($errorThumb);
|
||||
$response->write($img->encode());
|
||||
|
||||
// Output the mime type
|
||||
$response = $response->withHeader('Content-Type', $img->mime());
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output an image preview
|
||||
* @param \Xibo\Support\Sanitizer\SanitizerInterface $params
|
||||
* @param string $filePath
|
||||
* @param \Slim\Http\Response $response
|
||||
* @param string|null $errorThumb
|
||||
* @return \Slim\Http\Response
|
||||
* @throws \Xibo\Support\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function imagePreview(
|
||||
SanitizerInterface $params,
|
||||
string $filePath,
|
||||
Response $response,
|
||||
?string $errorThumb = null
|
||||
): Response {
|
||||
// Image previews call for dynamically generated images as various sizes
|
||||
// for example a background image will stretch to the entire region
|
||||
// an image widget may be aspect, fit or scale
|
||||
try {
|
||||
$filePath = $this->libraryLocation . $filePath;
|
||||
|
||||
// Does it exist?
|
||||
if (!file_exists($filePath)) {
|
||||
throw new NotFoundException(__('File not found'));
|
||||
}
|
||||
|
||||
// Check that our source image is not too large
|
||||
$imageInfo = getimagesize($filePath);
|
||||
|
||||
// Make sure none of the sides are greater than allowed
|
||||
if ($this->resizeLimit > 0
|
||||
&& ($imageInfo[0] > $this->resizeLimit || $imageInfo[1] > $this->resizeLimit)
|
||||
) {
|
||||
throw new InvalidArgumentException(__('Image too large'));
|
||||
}
|
||||
|
||||
// Continue to output at the desired size
|
||||
$width = intval($params->getDouble('width'));
|
||||
$height = intval($params->getDouble('height'));
|
||||
$proportional = !$params->hasParam('proportional')
|
||||
|| $params->getCheckbox('proportional') == 1;
|
||||
|
||||
$fit = $proportional && $params->getCheckbox('fit') === 1;
|
||||
|
||||
// only use upsize constraint, if we the requested dimensions are larger than resize limit.
|
||||
$useUpsizeConstraint = max($width, $height) > $this->resizeLimit;
|
||||
|
||||
$this->logger->debug('Whole file: ' . $filePath
|
||||
. ' requested with Width and Height ' . $width . ' x ' . $height
|
||||
. ', proportional: ' . var_export($proportional, true)
|
||||
. ', fit: ' . var_export($fit, true)
|
||||
. ', upsizeConstraint ' . var_export($useUpsizeConstraint, true));
|
||||
|
||||
// Does the thumbnail exist already?
|
||||
Img::configure(['driver' => 'gd']);
|
||||
$img = Img::make($filePath);
|
||||
|
||||
// Output a specific width/height
|
||||
if ($width > 0 && $height > 0) {
|
||||
if ($fit) {
|
||||
$img->fit($width, $height);
|
||||
} else {
|
||||
$img->resize($width, $height, function ($constraint) use ($proportional, $useUpsizeConstraint) {
|
||||
if ($proportional) {
|
||||
$constraint->aspectRatio();
|
||||
}
|
||||
if ($useUpsizeConstraint) {
|
||||
$constraint->upsize();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$response->write($img->encode());
|
||||
$response = HttpCacheProvider::withExpires($response, '+1 week');
|
||||
|
||||
$response = $response->withHeader('Content-Type', $img->mime());
|
||||
} catch (\Exception $e) {
|
||||
if ($errorThumb !== null) {
|
||||
$img = Img::make($errorThumb);
|
||||
$response->write($img->encode());
|
||||
$response = $response->withHeader('Content-Type', $img->mime());
|
||||
} else {
|
||||
$this->logger->error('Cannot parse image: ' . $e->getMessage());
|
||||
throw new InvalidArgumentException(__('Cannot parse image.'), 'storedAs');
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
1057
lib/Widget/Render/WidgetHtmlRenderer.php
Normal file
1057
lib/Widget/Render/WidgetHtmlRenderer.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user