| Total Complexity | 47 |
| Total Lines | 384 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PreviewManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PreviewManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class PreviewManager implements IPreview { |
||
| 45 | /** @var IConfig */ |
||
| 46 | protected $config; |
||
| 47 | |||
| 48 | /** @var IRootFolder */ |
||
| 49 | protected $rootFolder; |
||
| 50 | |||
| 51 | /** @var IAppData */ |
||
| 52 | protected $appData; |
||
| 53 | |||
| 54 | /** @var EventDispatcherInterface */ |
||
| 55 | protected $eventDispatcher; |
||
| 56 | |||
| 57 | /** @var Generator */ |
||
| 58 | private $generator; |
||
| 59 | |||
| 60 | /** @var GeneratorHelper */ |
||
| 61 | private $helper; |
||
| 62 | |||
| 63 | /** @var bool */ |
||
| 64 | protected $providerListDirty = false; |
||
| 65 | |||
| 66 | /** @var bool */ |
||
| 67 | protected $registeredCoreProviders = false; |
||
| 68 | |||
| 69 | /** @var array */ |
||
| 70 | protected $providers = []; |
||
| 71 | |||
| 72 | /** @var array mime type => support status */ |
||
| 73 | protected $mimeTypeSupportMap = []; |
||
| 74 | |||
| 75 | /** @var array */ |
||
| 76 | protected $defaultProviders; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | protected $userId; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * PreviewManager constructor. |
||
| 83 | * |
||
| 84 | * @param IConfig $config |
||
| 85 | * @param IRootFolder $rootFolder |
||
| 86 | * @param IAppData $appData |
||
| 87 | * @param EventDispatcherInterface $eventDispatcher |
||
| 88 | * @param string $userId |
||
| 89 | */ |
||
| 90 | public function __construct(IConfig $config, |
||
| 91 | IRootFolder $rootFolder, |
||
| 92 | IAppData $appData, |
||
| 93 | EventDispatcherInterface $eventDispatcher, |
||
| 94 | GeneratorHelper $helper, |
||
| 95 | $userId) { |
||
| 96 | $this->config = $config; |
||
| 97 | $this->rootFolder = $rootFolder; |
||
| 98 | $this->appData = $appData; |
||
| 99 | $this->eventDispatcher = $eventDispatcher; |
||
| 100 | $this->helper = $helper; |
||
| 101 | $this->userId = $userId; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * In order to improve lazy loading a closure can be registered which will be |
||
| 106 | * called in case preview providers are actually requested |
||
| 107 | * |
||
| 108 | * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 |
||
| 109 | * |
||
| 110 | * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
||
| 111 | * @param \Closure $callable |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | public function registerProvider($mimeTypeRegex, \Closure $callable) { |
||
| 115 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!isset($this->providers[$mimeTypeRegex])) { |
||
| 120 | $this->providers[$mimeTypeRegex] = []; |
||
| 121 | } |
||
| 122 | $this->providers[$mimeTypeRegex][] = $callable; |
||
| 123 | $this->providerListDirty = true; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get all providers |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function getProviders() { |
||
| 131 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 132 | return []; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->registerCoreProviders(); |
||
| 136 | if ($this->providerListDirty) { |
||
| 137 | $keys = array_map('strlen', array_keys($this->providers)); |
||
| 138 | array_multisort($keys, SORT_DESC, $this->providers); |
||
| 139 | $this->providerListDirty = false; |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this->providers; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Does the manager have any providers |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function hasProviders() { |
||
| 150 | $this->registerCoreProviders(); |
||
| 151 | return !empty($this->providers); |
||
| 152 | } |
||
| 153 | |||
| 154 | private function getGenerator(): Generator { |
||
| 155 | if ($this->generator === null) { |
||
| 156 | $this->generator = new Generator( |
||
| 157 | $this->config, |
||
| 158 | $this, |
||
| 159 | $this->appData, |
||
| 160 | new GeneratorHelper( |
||
| 161 | $this->rootFolder, |
||
| 162 | $this->config |
||
| 163 | ), |
||
| 164 | $this->eventDispatcher |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | return $this->generator; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns a preview of a file |
||
| 172 | * |
||
| 173 | * The cache is searched first and if nothing usable was found then a preview is |
||
| 174 | * generated by one of the providers |
||
| 175 | * |
||
| 176 | * @param File $file |
||
| 177 | * @param int $width |
||
| 178 | * @param int $height |
||
| 179 | * @param bool $crop |
||
| 180 | * @param string $mode |
||
| 181 | * @param string $mimeType |
||
| 182 | * @return ISimpleFile |
||
| 183 | * @throws NotFoundException |
||
| 184 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 185 | * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
||
| 186 | */ |
||
| 187 | public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
||
| 188 | return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Generates previews of a file |
||
| 193 | * |
||
| 194 | * @param File $file |
||
| 195 | * @param array $specifications |
||
| 196 | * @param string $mimeType |
||
| 197 | * @return ISimpleFile the last preview that was generated |
||
| 198 | * @throws NotFoundException |
||
| 199 | * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
||
| 200 | * @since 19.0.0 |
||
| 201 | */ |
||
| 202 | public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
||
| 203 | return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * returns true if the passed mime type is supported |
||
| 208 | * |
||
| 209 | * @param string $mimeType |
||
| 210 | * @return boolean |
||
| 211 | */ |
||
| 212 | public function isMimeSupported($mimeType = '*') { |
||
| 213 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (isset($this->mimeTypeSupportMap[$mimeType])) { |
||
| 218 | return $this->mimeTypeSupportMap[$mimeType]; |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->registerCoreProviders(); |
||
| 222 | $providerMimeTypes = array_keys($this->providers); |
||
| 223 | foreach ($providerMimeTypes as $supportedMimeType) { |
||
| 224 | if (preg_match($supportedMimeType, $mimeType)) { |
||
| 225 | $this->mimeTypeSupportMap[$mimeType] = true; |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $this->mimeTypeSupportMap[$mimeType] = false; |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check if a preview can be generated for a file |
||
| 235 | * |
||
| 236 | * @param \OCP\Files\FileInfo $file |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | public function isAvailable(\OCP\Files\FileInfo $file) { |
||
| 240 | if (!$this->config->getSystemValue('enable_previews', true)) { |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->registerCoreProviders(); |
||
| 245 | if (!$this->isMimeSupported($file->getMimetype())) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | |||
| 249 | $mount = $file->getMountPoint(); |
||
| 250 | if ($mount and !$mount->getOption('previews', true)){ |
||
| 251 | return false; |
||
| 252 | } |
||
| 253 | |||
| 254 | foreach ($this->providers as $supportedMimeType => $providers) { |
||
| 255 | if (preg_match($supportedMimeType, $file->getMimetype())) { |
||
| 256 | foreach ($providers as $providerClosure) { |
||
| 257 | $provider = $this->helper->getProvider($providerClosure); |
||
| 258 | if (!($provider instanceof IProviderV2)) { |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** @var $provider IProvider */ |
||
| 263 | if ($provider->isAvailable($file)) { |
||
| 264 | return true; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | return false; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * List of enabled default providers |
||
| 274 | * |
||
| 275 | * The following providers are enabled by default: |
||
| 276 | * - OC\Preview\PNG |
||
| 277 | * - OC\Preview\JPEG |
||
| 278 | * - OC\Preview\GIF |
||
| 279 | * - OC\Preview\BMP |
||
| 280 | * - OC\Preview\HEIC |
||
| 281 | * - OC\Preview\XBitmap |
||
| 282 | * - OC\Preview\MarkDown |
||
| 283 | * - OC\Preview\MP3 |
||
| 284 | * - OC\Preview\TXT |
||
| 285 | * |
||
| 286 | * The following providers are disabled by default due to performance or privacy concerns: |
||
| 287 | * - OC\Preview\Font |
||
| 288 | * - OC\Preview\Illustrator |
||
| 289 | * - OC\Preview\Movie |
||
| 290 | * - OC\Preview\MSOfficeDoc |
||
| 291 | * - OC\Preview\MSOffice2003 |
||
| 292 | * - OC\Preview\MSOffice2007 |
||
| 293 | * - OC\Preview\OpenDocument |
||
| 294 | * - OC\Preview\PDF |
||
| 295 | * - OC\Preview\Photoshop |
||
| 296 | * - OC\Preview\Postscript |
||
| 297 | * - OC\Preview\StarOffice |
||
| 298 | * - OC\Preview\SVG |
||
| 299 | * - OC\Preview\TIFF |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | protected function getEnabledDefaultProvider() { |
||
| 304 | if ($this->defaultProviders !== null) { |
||
| 305 | return $this->defaultProviders; |
||
| 306 | } |
||
| 307 | |||
| 308 | $imageProviders = [ |
||
| 309 | Preview\PNG::class, |
||
| 310 | Preview\JPEG::class, |
||
| 311 | Preview\GIF::class, |
||
| 312 | Preview\BMP::class, |
||
| 313 | Preview\HEIC::class, |
||
| 314 | Preview\XBitmap::class, |
||
| 315 | Preview\Krita::class, |
||
| 316 | ]; |
||
| 317 | |||
| 318 | $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
||
| 319 | Preview\MarkDown::class, |
||
| 320 | Preview\MP3::class, |
||
| 321 | Preview\TXT::class, |
||
| 322 | Preview\OpenDocument::class, |
||
| 323 | ], $imageProviders)); |
||
| 324 | |||
| 325 | if (in_array(Preview\Image::class, $this->defaultProviders)) { |
||
| 326 | $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
||
| 327 | } |
||
| 328 | $this->defaultProviders = array_unique($this->defaultProviders); |
||
| 329 | return $this->defaultProviders; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Register the default providers (if enabled) |
||
| 334 | * |
||
| 335 | * @param string $class |
||
| 336 | * @param string $mimeType |
||
| 337 | */ |
||
| 338 | protected function registerCoreProvider($class, $mimeType, $options = []) { |
||
| 342 | }); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Register the default providers (if enabled) |
||
| 348 | */ |
||
| 349 | protected function registerCoreProviders() { |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 |