1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
<?php
namespace BEdita\Core\Filesystem;
use BEdita\Core\Filesystem\Exception\InvalidStreamException;
use BEdita\Core\Filesystem\Exception\InvalidThumbnailOptionsException;
use BEdita\Core\Filesystem\Thumbnail\AsyncGenerator;
use BEdita\Core\Filesystem\Thumbnail\GlideGenerator;
use BEdita\Core\Model\Entity\Stream;
use Cake\Core\Configure;
use Cake\Core\StaticConfigTrait;
use Cake\Utility\Hash;
class Thumbnail
{
use StaticConfigTrait;
protected static $_registry;
protected static $_dsnClassMap = [
'glide' => GlideGenerator::class,
'async' => AsyncGenerator::class,
];
public static function setRegistry(?ThumbnailRegistry $registry = null)
{
static::$_registry = $registry;
}
public static function getRegistry()
{
if (!isset(static::$_registry)) {
static::$_registry = new ThumbnailRegistry();
}
return static::$_registry;
}
public static function getGenerator($name)
{
$registry = static::getRegistry();
if ($registry->has($name)) {
return $registry->get($name);
}
return $registry->load($name, static::getConfig($name));
}
public static function get(Stream $stream, $options = 'default')
{
if ($stream->get('private_url')) {
return [
'url' => null,
'ready' => false,
'acceptable' => false,
];
}
$options = self::getOptions($options);
$generator = Hash::get($options, 'generator', 'default');
unset($options['generator']);
$generator = static::getGenerator($generator);
$url = $generator->getUrl($stream, $options);
$ready = $generator->exists($stream, $options);
if (!$ready) {
try {
$ready = $generator->generate($stream, $options);
} catch (InvalidStreamException $e) {
$acceptable = false;
$message = $e->getMessage();
}
}
$res = compact('url', 'ready');
if (isset($acceptable, $message)) {
$res += compact('acceptable', 'message');
}
return $res;
}
protected static function getOptions($options)
{
if (is_string($options)) {
$key = sprintf('Thumbnails.presets.%s', $options);
if (!Configure::check($key)) {
throw new InvalidThumbnailOptionsException(__d('bedita', 'Preset "{0}" not found', $options));
}
$options = Configure::read($key);
} elseif (!Configure::read('Thumbnails.allowAny')) {
throw new InvalidThumbnailOptionsException(__d('bedita', 'Thumbnails can only be generated for one of the configured presets'));
}
return $options;
}
public static function delete(Stream $stream)
{
$generators = static::configured();
foreach ($generators as $generator) {
static::getGenerator($generator)->delete($stream);
}
}
}