summaryrefslogtreecommitdiffstats
path: root/lib/private/Preview
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2016-10-17 21:53:23 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2016-11-03 14:00:33 +0100
commit87855aa97b0d90f8036ec40174ac1a3dcbd463e8 (patch)
tree95aed134338652a4773af4c98549442141b67fb0 /lib/private/Preview
parent3d6ee7ffc97af5c426ae7553bcb40f3c28064f93 (diff)
downloadnextcloud-server-87855aa97b0d90f8036ec40174ac1a3dcbd463e8.tar.gz
nextcloud-server-87855aa97b0d90f8036ec40174ac1a3dcbd463e8.zip
Added genertor helper & tests
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib/private/Preview')
-rw-r--r--lib/private/Preview/Generator.php44
-rw-r--r--lib/private/Preview/GeneratorHelper.php91
2 files changed, 100 insertions, 35 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php
index d4c38b1cb6a..3d4e9bf3677 100644
--- a/lib/private/Preview/Generator.php
+++ b/lib/private/Preview/Generator.php
@@ -23,46 +23,43 @@
namespace OC\Preview;
-use OC\Files\View;
use OCP\Files\File;
use OCP\Files\IAppData;
-use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IImage;
-use OCP\Image as img;
use OCP\IPreview;
use OCP\Preview\IProvider;
class Generator {
- /** @var IRootFolder*/
- private $rootFolder;
/** @var IPreview */
private $previewManager;
/** @var IConfig */
private $config;
/** @var IAppData */
private $appData;
+ /** @var GeneratorHelper */
+ private $helper;
/**
- * @param IRootFolder $rootFolder
* @param IConfig $config
* @param IPreview $previewManager
* @param IAppData $appData
+ * @param GeneratorHelper $helper
*/
public function __construct(
- IRootFolder $rootFolder,
IConfig $config,
IPreview $previewManager,
- IAppData $appData
+ IAppData $appData,
+ GeneratorHelper $helper
) {
- $this->rootFolder = $rootFolder;
$this->config = $config;
$this->previewManager = $previewManager;
$this->appData = $appData;
+ $this->helper = $helper;
}
/**
@@ -88,10 +85,6 @@ class Generator {
throw new NotFoundException();
}
- /*
- * Get the preview folder
- * TODO: Separate preview creation from storing previews
- */
$previewFolder = $this->getPreviewFolder($file);
// Get the max preview and infer the max preview sizes from that
@@ -134,17 +127,15 @@ class Generator {
}
foreach ($providers as $provider) {
- $provider = $provider();
+ $provider = $this->helper->getProvider($provider);
if (!($provider instanceof IProvider)) {
continue;
}
- list($view, $path) = $this->getViewAndPath($file);
-
$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);
- $preview = $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
+ $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
if (!($preview instanceof IImage)) {
continue;
@@ -185,24 +176,7 @@ class Generator {
return $path;
}
- /**
- * @param File $file
- * @return array
- * This is required to create the old view and path
- */
- private function getViewAndPath(File $file) {
- $owner = $file->getOwner()->getUID();
-
- $userFolder = $this->rootFolder->getUserFolder($owner)->getParent();
- $nodes = $userFolder->getById($file->getId());
-
- $file = $nodes[0];
- $view = new View($userFolder->getPath());
- $path = $userFolder->getRelativePath($file->getPath());
-
- return [$view, $path];
- }
/**
* @param int $width
@@ -300,7 +274,7 @@ class Generator {
* @throws NotFoundException
*/
private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
- $preview = new img($maxPreview->getContent());
+ $preview = $this->helper->getImage($maxPreview);
if ($crop) {
if ($height !== $preview->height() && $width !== $preview->width()) {
diff --git a/lib/private/Preview/GeneratorHelper.php b/lib/private/Preview/GeneratorHelper.php
new file mode 100644
index 00000000000..282c46a2a5d
--- /dev/null
+++ b/lib/private/Preview/GeneratorHelper.php
@@ -0,0 +1,91 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program 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 (at your option) any later version.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OC\Preview;
+
+use OC\Files\View;
+use OCP\Files\File;
+use OCP\Files\IRootFolder;
+use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\IImage;
+use OCP\Image as img;
+use OCP\Preview\IProvider;
+
+/**
+ * Very small wrapper class to make the generator fully unit testable
+ */
+class GeneratorHelper {
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ public function __construct(IRootFolder $rootFolder) {
+ $this->rootFolder = $rootFolder;
+ }
+
+ /**
+ * @param IProvider $provider
+ * @param File $file
+ * @param int $maxWidth
+ * @param int $maxHeight
+ * @return bool|IImage
+ */
+ public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
+ list($view, $path) = $this->getViewAndPath($file);
+ return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
+ }
+
+ /**
+ * @param File $file
+ * @return array
+ * This is required to create the old view and path
+ */
+ private function getViewAndPath(File $file) {
+ $owner = $file->getOwner()->getUID();
+
+ $userFolder = $this->rootFolder->getUserFolder($owner)->getParent();
+
+ $nodes = $userFolder->getById($file->getId());
+ $file = $nodes[0];
+
+ $view = new View($userFolder->getPath());
+ $path = $userFolder->getRelativePath($file->getPath());
+
+ return [$view, $path];
+ }
+
+ /**
+ * @param ISimpleFile $maxPreview
+ * @return IImage
+ */
+ public function getImage(ISimpleFile $maxPreview) {
+ return new img($maxPreview->getContent());
+ }
+
+ /**
+ * @param $provider
+ * @return IProvider
+ */
+ public function getProvider($provider) {
+ return $provider();
+ }
+}