Move Ipreview to more of DI thingy

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-10-16 16:48:11 +02:00
parent 8468212386
commit 5466fbf761
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
5 changed files with 116 additions and 52 deletions

View File

@ -23,15 +23,14 @@
namespace OC\Core\Controller; namespace OC\Core\Controller;
use OC\PreviewManager; use OC\DatabaseException;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\Files\File; use OCP\Files\File;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IConfig; use OCP\IPreview;
use OCP\IRequest; use OCP\IRequest;
class PreviewController extends Controller { class PreviewController extends Controller {
@ -42,29 +41,28 @@ class PreviewController extends Controller {
/** @var IRootFolder */ /** @var IRootFolder */
private $root; private $root;
/** @var IConfig */ /** @var IPreview */
private $config; private $preview;
/** @var PreviewManager */
private $previewManager;
/** @var IAppData */
private $appData;
/**
* PreviewController constructor.
*
* @param string $appName
* @param IRequest $request
* @param IPreview $preview
* @param IRootFolder $root
* @param string $userId
*/
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
IPreview $preview,
IRootFolder $root, IRootFolder $root,
IConfig $config,
PreviewManager $previewManager,
IAppData $appData,
$userId $userId
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->previewManager = $previewManager; $this->preview = $preview;
$this->root = $root; $this->root = $root;
$this->config = $config;
$this->appData = $appData;
$this->userId = $userId; $this->userId = $userId;
} }
@ -103,21 +101,17 @@ class PreviewController extends Controller {
return new DataResponse([], Http::STATUS_NOT_FOUND); return new DataResponse([], Http::STATUS_NOT_FOUND);
} }
if (!($file instanceof File) || (!$forceIcon && !$this->previewManager->isAvailable($file))) { if (!($file instanceof File) || (!$forceIcon && !$this->preview->isAvailable($file))) {
return new DataResponse([], Http::STATUS_NOT_FOUND); return new DataResponse([], Http::STATUS_NOT_FOUND);
} else if (!$file->isReadable()) { } else if (!$file->isReadable()) {
return new DataResponse([], Http::STATUS_FORBIDDEN); return new DataResponse([], Http::STATUS_FORBIDDEN);
} }
$preview = new \OC\Preview\Generator( try {
$this->root, $f = $this->preview->getPreview($file, $x, $y, !$a, $mode);
$this->config, } catch (NotFoundException $e) {
$this->previewManager, return new DataResponse([], Http::STATUS_NOT_FOUND);
$file, }
$this->appData
);
$f = $preview->getPreview($x, $y, !$a, $mode);
return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
} }
} }

View File

@ -37,16 +37,9 @@ use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
class Generator { class Generator {
//the thumbnail folder
const THUMBNAILS_FOLDER = 'thumbnails';
const MODE_FILL = 'fill';
const MODE_COVER = 'cover';
/** @var IRootFolder*/ /** @var IRootFolder*/
private $rootFolder; private $rootFolder;
/** @var File */
private $file;
/** @var IPreview */ /** @var IPreview */
private $previewManager; private $previewManager;
/** @var IConfig */ /** @var IConfig */
@ -58,19 +51,16 @@ class Generator {
* @param IRootFolder $rootFolder * @param IRootFolder $rootFolder
* @param IConfig $config * @param IConfig $config
* @param IPreview $previewManager * @param IPreview $previewManager
* @param File $file
* @param IAppData $appData * @param IAppData $appData
*/ */
public function __construct( public function __construct(
IRootFolder $rootFolder, IRootFolder $rootFolder,
IConfig $config, IConfig $config,
IPreview $previewManager, IPreview $previewManager,
File $file,
IAppData $appData IAppData $appData
) { ) {
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->config = $config; $this->config = $config;
$this->file = $file;
$this->previewManager = $previewManager; $this->previewManager = $previewManager;
$this->appData = $appData; $this->appData = $appData;
} }
@ -81,6 +71,7 @@ class Generator {
* The cache is searched first and if nothing usable was found then a preview is * The cache is searched first and if nothing usable was found then a preview is
* generated by one of the providers * generated by one of the providers
* *
* @param File $file
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
@ -88,8 +79,8 @@ class Generator {
* @return ISimpleFile * @return ISimpleFile
* @throws NotFoundException * @throws NotFoundException
*/ */
public function getPreview($width = -1, $height = -1, $crop = false, $mode = Generator::MODE_FILL) { public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL) {
if (!$this->previewManager->isMimeSupported($this->file->getMimeType())) { if (!$this->previewManager->isMimeSupported($file->getMimeType())) {
throw new NotFoundException(); throw new NotFoundException();
} }
@ -97,10 +88,10 @@ class Generator {
* Get the preview folder * Get the preview folder
* TODO: Separate preview creation from storing previews * TODO: Separate preview creation from storing previews
*/ */
$previewFolder = $this->getPreviewFolder(); $previewFolder = $this->getPreviewFolder($file);
// Get the max preview and infer the max preview sizes from that // Get the max preview and infer the max preview sizes from that
$maxPreview = $this->getMaxPreview($previewFolder); $maxPreview = $this->getMaxPreview($previewFolder, $file);
list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
// Calculate the preview size // Calculate the preview size
@ -118,10 +109,11 @@ class Generator {
/** /**
* @param ISimpleFolder $previewFolder * @param ISimpleFolder $previewFolder
* @param File $file
* @return ISimpleFile * @return ISimpleFile
* @throws NotFoundException * @throws NotFoundException
*/ */
private function getMaxPreview(ISimpleFolder $previewFolder) { private function getMaxPreview(ISimpleFolder $previewFolder, File $file) {
$nodes = $previewFolder->getDirectoryListing(); $nodes = $previewFolder->getDirectoryListing();
foreach ($nodes as $node) { foreach ($nodes as $node) {
@ -132,7 +124,7 @@ class Generator {
$previewProviders = $this->previewManager->getProviders(); $previewProviders = $this->previewManager->getProviders();
foreach ($previewProviders as $supportedMimeType => $providers) { foreach ($previewProviders as $supportedMimeType => $providers) {
if (!preg_match($supportedMimeType, $this->file->getMimeType())) { if (!preg_match($supportedMimeType, $file->getMimeType())) {
continue; continue;
} }
@ -142,7 +134,7 @@ class Generator {
continue; continue;
} }
list($view, $path) = $this->getViewAndPath($this->file); list($view, $path) = $this->getViewAndPath($file);
$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048); $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048); $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);
@ -239,13 +231,13 @@ class Generator {
* Fill means that the $height and $width are the max * Fill means that the $height and $width are the max
* Cover means min. * Cover means min.
*/ */
if ($mode === self::MODE_FILL) { if ($mode === IPreview::MODE_FILL) {
if ($ratioH > $ratioW) { if ($ratioH > $ratioW) {
$height = $width * $ratio; $height = $width * $ratio;
} else { } else {
$width = $height / $ratio; $width = $height / $ratio;
} }
} else if ($mode === self::MODE_COVER) { } else if ($mode === IPreview::MODE_COVER) {
if ($ratioH > $ratioW) { if ($ratioH > $ratioW) {
$width = $height / $ratio; $width = $height / $ratio;
} else { } else {
@ -352,13 +344,14 @@ class Generator {
/** /**
* Get the specific preview folder for this file * Get the specific preview folder for this file
* *
* @param File $file
* @return ISimpleFolder * @return ISimpleFolder
*/ */
private function getPreviewFolder() { private function getPreviewFolder(File $file) {
try { try {
$folder = $this->appData->getFolder($this->file->getId()); $folder = $this->appData->getFolder($file->getId());
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
$folder = $this->appData->newFolder($this->file->getId()); $folder = $this->appData->newFolder($file->getId());
} }
return $folder; return $folder;

View File

@ -25,13 +25,29 @@
*/ */
namespace OC; namespace OC;
use OC\Preview\Generator;
use OCP\Files\File;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
class PreviewManager implements IPreview { class PreviewManager implements IPreview {
/** @var \OCP\IConfig */ /** @var IConfig */
protected $config; protected $config;
/** @var IRootFolder */
protected $rootFolder;
/** @var IAppData */
protected $appData;
/** @var Generator */
private $generator;
/** @var bool */ /** @var bool */
protected $providerListDirty = false; protected $providerListDirty = false;
@ -52,8 +68,12 @@ class PreviewManager implements IPreview {
* *
* @param \OCP\IConfig $config * @param \OCP\IConfig $config
*/ */
public function __construct(\OCP\IConfig $config) { public function __construct(IConfig $config,
IRootFolder $rootFolder,
IAppData $appData) {
$this->config = $config; $this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
} }
/** /**
@ -120,6 +140,34 @@ class PreviewManager implements IPreview {
return $preview->getPreview(); return $preview->getPreview();
} }
/**
* Returns a preview of a file
*
* The cache is searched first and if nothing usable was found then a preview is
* generated by one of the providers
*
* @param File $file
* @param int $width
* @param int $height
* @param bool $crop
* @param string $mode
* @return ISimpleFile
* @throws NotFoundException
* @since 9.2.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL) {
if ($this->generator === null) {
$this->generator = new Generator(
$this->rootFolder,
$this->config,
$this,
$this->appData
);
}
return $this->generator->getPreview($file, $width, $height, $crop, $mode);
}
/** /**
* returns true if the passed mime type is supported * returns true if the passed mime type is supported
* *

View File

@ -117,7 +117,11 @@ class Server extends ServerContainer implements IServerContainer {
}); });
$this->registerService('PreviewManager', function (Server $c) { $this->registerService('PreviewManager', function (Server $c) {
return new PreviewManager($c->getConfig()); return new PreviewManager(
$c->getConfig(),
$c->getRootFolder(),
$c->getAppDataDir('preview')
);
}); });
$this->registerService(\OC\Preview\Watcher::class, function (Server $c) { $this->registerService(\OC\Preview\Watcher::class, function (Server $c) {

View File

@ -33,11 +33,19 @@
// This means that they should be used by apps instead of the internal ownCloud classes // This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP; namespace OCP;
use OCP\Files\File;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\NotFoundException;
/** /**
* This class provides functions to render and show thumbnails and previews of files * This class provides functions to render and show thumbnails and previews of files
* @since 6.0.0 * @since 6.0.0
*/ */
interface IPreview { interface IPreview {
const MODE_FILL = 'fill';
const MODE_COVER = 'cover';
/** /**
* In order to improve lazy loading a closure can be registered which will be * In order to improve lazy loading a closure can be registered which will be
* called in case preview providers are actually requested * called in case preview providers are actually requested
@ -73,9 +81,26 @@ interface IPreview {
* @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
* @return \OCP\IImage * @return \OCP\IImage
* @since 6.0.0 * @since 6.0.0
* @deprecated 9.2.0 Use getPreview
*/ */
public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false); public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false);
/**
* Returns a preview of a file
*
* The cache is searched first and if nothing usable was found then a preview is
* generated by one of the providers
*
* @param File $file
* @param int $width
* @param int $height
* @param bool $crop
* @param string $mode
* @return ISimpleFile
* @throws NotFoundException
* @since 9.2.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL);
/** /**
* Returns true if the passed mime type is supported * Returns true if the passed mime type is supported