Completed
Push — master ( 8bf574...c0bbae )
by Lukas
28:01 queued 15:22
created

GeneratorHelper::getViewAndPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Preview;
24
25
use OC\Files\View;
26
use OCP\Files\File;
27
use OCP\Files\IRootFolder;
28
use OCP\Files\SimpleFS\ISimpleFile;
29
use OCP\IImage;
30
use OCP\Image as img;
31
use OCP\Preview\IProvider;
32
33
/**
34
 * Very small wrapper class to make the generator fully unit testable
35
 */
36
class GeneratorHelper {
37
38
	/** @var IRootFolder */
39
	private $rootFolder;
40
41
	public function __construct(IRootFolder $rootFolder) {
42
		$this->rootFolder = $rootFolder;
43
	}
44
45
	/**
46
	 * @param IProvider $provider
47
	 * @param File $file
48
	 * @param int $maxWidth
49
	 * @param int $maxHeight
50
	 * @return bool|IImage
51
	 */
52
	public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
53
		list($view, $path) = $this->getViewAndPath($file);
54
		return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
55
	}
56
57
	/**
58
	 * @param File $file
59
	 * @return array
60
	 * This is required to create the old view and path
61
	 */
62
	private function getViewAndPath(File $file) {
63
		$owner = $file->getOwner()->getUID();
64
65
		$userFolder = $this->rootFolder->getUserFolder($owner)->getParent();
66
67
		$nodes = $userFolder->getById($file->getId());
68
		$file = $nodes[0];
69
70
		$view = new View($userFolder->getPath());
71
		$path = $userFolder->getRelativePath($file->getPath());
72
73
		return [$view, $path];
74
	}
75
76
	/**
77
	 * @param ISimpleFile $maxPreview
78
	 * @return IImage
79
	 */
80
	public function getImage(ISimpleFile $maxPreview) {
81
		return new img($maxPreview->getContent());
82
	}
83
84
	/**
85
	 * @param $provider
86
	 * @return IProvider
87
	 */
88
	public function getProvider($provider) {
89
		return $provider();
90
	}
91
}
92