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

Add support for useFetchFormat in VideoTag #394

Merged
merged 1 commit into from Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Asset/BaseMediaAsset.php
Expand Up @@ -12,6 +12,7 @@

use Cloudinary\ArrayUtils;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Configuration\TagConfig;
use Cloudinary\Exception\ConfigurationException;
use Cloudinary\Transformation\BaseAction;
use Cloudinary\Transformation\CommonTransformation;
Expand Down Expand Up @@ -136,13 +137,14 @@ public function addAction($action)
* For non-fetched assets sets the filename extension.
* For remotely fetched assets sets the 'f_' transformation parameter.
*
* @param string $format The format to set.
* @param string $format The format to set.
* @param bool $useFetchFormat Whether to force fetch format behavior.
*
* @return static
*/
public function setFormat($format)
public function setFormat($format, $useFetchFormat = false)
{
if ($this->asset->deliveryType == DeliveryType::FETCH) {
if ($useFetchFormat || $this->asset->deliveryType == DeliveryType::FETCH) {
$this->addTransformation(Delivery::format($format));
} else {
$this->asset->extension = $format;
Expand Down Expand Up @@ -203,7 +205,9 @@ public function toUrl($withTransformation = null, $append = true)
*/
private static function setFormatParameter($params)
{
if (ArrayUtils::get($params, DeliveryType::KEY) !== DeliveryType::FETCH) {
if (ArrayUtils::get($params, DeliveryType::KEY) !== DeliveryType::FETCH
&& ! ArrayUtils::get($params, TagConfig::USE_FETCH_FORMAT, false)
) {
return $params;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Asset/Descriptor/AssetDescriptor.php
Expand Up @@ -225,7 +225,9 @@ public static function fromParams($source, $params = [])
= FileUtils::splitPathFilenameExtension($source);

// Explicit 'format' parameter overrides extension. (Fetch URLs are not affected).
if ($assetJson['delivery_type'] != DeliveryType::FETCH) {
if ($assetJson['delivery_type'] != DeliveryType::FETCH
|| ! ArrayUtils::get($params, 'use_fetch_format', false)
) {
ArrayUtils::addNonEmpty($assetJson, 'extension', ArrayUtils::get($params, 'format'));
}

Expand Down
25 changes: 25 additions & 0 deletions src/Configuration/TagConfig.php
Expand Up @@ -26,6 +26,8 @@
*/
class TagConfig extends BaseConfigSection
{
use TagConfigTrait;

const CONFIG_NAME = 'tag';

const DEFAULT_VIDEO_POSTER_FORMAT = Format::JPG;
Expand All @@ -43,6 +45,7 @@ class TagConfig extends BaseConfigSection
const CLIENT_HINTS = 'client_hints';
const UNSIGNED_UPLOAD = 'unsigned_upload';
const VIDEO_POSTER_FORMAT = 'video_poster_format';
const USE_FETCH_FORMAT = 'use_fetch_format';
const QUOTES_TYPE = 'quotes_type';
const VOID_CLOSING_SLASH = 'void_closing_slash';
const SORT_ATTRIBUTES = 'sort_attributes';
Expand Down Expand Up @@ -118,6 +121,13 @@ class TagConfig extends BaseConfigSection
*/
protected $videoPosterFormat;

/**
* Whether to use fetch format transformation ("f_") instead of file extension.
*
* @var string $useFetchFormat
*/
public $useFetchFormat;

/**
* Sets the type of the quotes to use (single or double). Default: BaseTag::DOUBLE_QUOTES.
*
Expand Down Expand Up @@ -154,4 +164,19 @@ class TagConfig extends BaseConfigSection
* @var string $contentDelimiter
*/
protected $contentDelimiter;

/**
* Sets the Tag configuration key with the specified value.
*
* @param string $configKey The configuration key.
* @param mixed $configValue THe configuration value.
*
* @return $this
*
* @internal
*/
public function setTagConfig($configKey, $configValue)
{
return $this->setConfig($configKey, $configValue);
}
}
57 changes: 57 additions & 0 deletions src/Configuration/TagConfigTrait.php
@@ -0,0 +1,57 @@
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cloudinary\Configuration;

/**
* Trait TagConfigTrait
*
* @api
*/
trait TagConfigTrait
{

/**
* Image format of the video poster.
*
* @param string $format Image format.
*
* @return $this
*/
public function videoPosterFormat($format)
{
return $this->setTagConfig(TagConfig::VIDEO_POSTER_FORMAT, $format);
}


/**
* Use fetch format transformation ("f_") instead of file extension.
*
* @param bool $useFetchFormat
*
* @return $this
*/
public function useFetchFormat($useFetchFormat = true)
{
return $this->setTagConfig(TagConfig::USE_FETCH_FORMAT, $useFetchFormat);
}

/**
* Sets the Tag configuration key with the specified value.
*
* @param string $configKey The configuration key.
* @param mixed $configValue THe configuration value.
*
* @return $this
*
* @internal
*/
abstract public function setTagConfig($configKey, $configValue);
}
20 changes: 20 additions & 0 deletions src/Tag/BaseTag.php
Expand Up @@ -13,6 +13,7 @@
use Cloudinary\ArrayUtils;
use Cloudinary\Asset\AssetQualifiers;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Configuration\TagConfigTrait;
use Cloudinary\Transformation\QualifiersAction;
use Cloudinary\Utils;
use UnexpectedValueException;
Expand All @@ -24,6 +25,8 @@
*/
abstract class BaseTag
{
use TagConfigTrait;

const SINGLE_QUOTES = 'single_quotes';
const DOUBLE_QUOTES = 'double_quotes';

Expand Down Expand Up @@ -360,6 +363,23 @@ protected static function fromParamsDefaultConfig()
return $configuration;
}

/**
* Sets the Tag configuration key with the specified value.
*
* @param string $configKey The configuration key.
* @param mixed $configValue THe configuration value.
*
* @return $this
*
* @internal
*/
public function setTagConfig($configKey, $configValue)
{
$this->config->tag->setTagConfig($configKey, $configValue);

return $this;
}

/**
* Serializes the tag to string.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/ImageTagDeliveryTypeTrait.php
Expand Up @@ -121,7 +121,7 @@ public static function youTube($videoId, $configuration = null)
*/
public static function hulu($videoId, $configuration = null)
{
return new static(Image::youTube($videoId, $configuration));
return new static(Image::hulu($videoId, $configuration));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Tag/VideoSourceTag.php
Expand Up @@ -97,7 +97,6 @@ public function video($video, $configuration = null)
public function type($type, $codecs = null)
{
$this->sourceType = ClassUtils::verifyInstance($type, VideoSourceType::class, null, $codecs);
$this->video->setFormat($this->sourceType->type);

return $this;
}
Expand All @@ -112,7 +111,9 @@ public function type($type, $codecs = null)
public function serializeAttributes($attributes = [])
{
if (! empty((string)$this->video)) {
$attributes['src'] = $this->video->toUrl($this->additionalTransformation);
$toSerialize = new Video($this->video);
$toSerialize->setFormat($this->sourceType->type, $this->config->tag->useFetchFormat);
$attributes['src'] = $toSerialize->toUrl($this->additionalTransformation);
}

if (! empty((string)$this->sourceType)) {
Expand Down
31 changes: 28 additions & 3 deletions src/Tag/VideoTag.php
Expand Up @@ -17,6 +17,7 @@
use Cloudinary\Asset\Video;
use Cloudinary\Configuration\AssetConfigTrait;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Configuration\TagConfigTrait;
use Cloudinary\Transformation\BaseAction;
use Cloudinary\Transformation\CommonTransformation;
use Cloudinary\Transformation\ImageTransformation;
Expand Down Expand Up @@ -47,6 +48,7 @@ class VideoTag extends BaseTag implements VideoTransformationInterface
use VideoTransformationTrait;
use AssetDescriptorTrait;
use AssetConfigTrait;
use TagConfigTrait;

const NAME = 'video';

Expand Down Expand Up @@ -156,7 +158,7 @@ public function sources($sourcesDefinitions)
* @param string $source The public ID of the asset.
* @param array $params The asset parameters.
*
* @return mixed
* @return static
*/

public static function fromParams($source, $params = [])
Expand Down Expand Up @@ -213,7 +215,9 @@ protected static function generateVideoPosterAttr($source, &$videoParams, $confi

if (! array_key_exists('poster', $videoParams)) {
// set default poster based on the video
return Image::fromParams($source, $videoParams);
$videoPosterParams = $videoParams;
ArrayUtils::setDefaultValue($videoPosterParams, 'use_fetch_format', $configuration->tag->useFetchFormat);
return Image::fromParams($source, $videoPosterParams);
}

// Custom poster
Expand Down Expand Up @@ -301,7 +305,7 @@ public function serializeAttributes($attributes = [])

if (! array_key_exists('poster', $this->attributes)) {
$poster = new Image($this->video);
$poster->setFormat($this->config->tag->videoPosterFormat);
$poster->setFormat($this->config->tag->videoPosterFormat, $this->config->tag->useFetchFormat);

$attributes['poster'] = $poster;
}
Expand Down Expand Up @@ -410,6 +414,27 @@ public function setUrlConfig($configKey, $configValue)
return $this->applyAssetModification('setUrlConfig', $configKey, $configValue);
}

/**
* Sets the Tag configuration key with the specified value.
*
* @param string $configKey The configuration key.
* @param mixed $configValue THe configuration value.
*
* @return $this
*
* @internal
*/
public function setTagConfig($configKey, $configValue)
{
$this->config->tag->setTagConfig($configKey, $configValue);

foreach ($this->sources as $source) {
$source->setTagConfig($configKey, $configValue);
}

return $this;
}

/**
* Applies modification to the asset and to all sources.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Tag/VideoThumbnailTag.php
Expand Up @@ -14,7 +14,6 @@
use Cloudinary\Asset\AssetType;
use Cloudinary\Asset\Video;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Configuration\TagConfig;

/**
*
Expand All @@ -36,7 +35,7 @@ public function image($source, $configuration = null)
{
parent::image(new Video($source, $configuration), $configuration);

$this->image->setFormat($configuration->tag->videoPosterFormat);
$this->image->setFormat($configuration->tag->videoPosterFormat, $configuration->tag->useFetchFormat);

return $this;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Tag/TagFromParamsTest.php
Expand Up @@ -535,6 +535,23 @@ public function testVideoTagFetch()
);
}

public function testVideoTagUseFetchFormat()
{
$videoId = self::VIDEO_NAME;
$prefixUrl = self::VIDEO_UPLOAD_PATH;

Configuration::instance()->importJson(['use_fetch_format' => true]);

self::assertStrEquals(
"<video poster='{$prefixUrl}f_jpg/$videoId'>" .
"<source src='{$prefixUrl}f_webm/$videoId' type='video/webm'>" .
"<source src='{$prefixUrl}f_mp4/$videoId' type='video/mp4'>" .
"<source src='{$prefixUrl}f_ogv/$videoId' type='video/ogg'>" .
'</video>',
VideoTag::fromParams($videoId)
);
}

public function testVideoTagWithAttributes()
{
//test video attributes
Expand Down