Passed
Push — master ( 335af0...d4a44d )
by Roeland
16:33 queued 10s
created

ProviderV2   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 2 1
A __construct() 0 2 1
A getLocalFile() 0 16 4
A cleanTmpFiles() 0 6 2
1
<?php declare(strict_types=1);
2
/**
3
 * @copyright Copyright (c) 2019 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Preview;
23
24
use OCP\Files\File;
25
use OCP\Files\FileInfo;
26
use OCP\IImage;
27
use OCP\Preview\IProviderV2;
28
29
abstract class ProviderV2 implements IProviderV2 {
30
	private $options;
31
32
	private $tmpFiles = [];
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param array $options
38
	 */
39
	public function __construct(array $options = []) {
40
		$this->options = $options;
41
	}
42
43
	/**
44
	 * @return string Regex with the mimetypes that are supported by this provider
45
	 */
46
	abstract public function getMimeType(): string ;
47
48
	/**
49
	 * Check if a preview can be generated for $path
50
	 *
51
	 * @param FileInfo $file
52
	 * @return bool
53
	 */
54
	public function isAvailable(FileInfo $file): bool {
55
		return true;
56
	}
57
58
	/**
59
	 * get thumbnail for file at path $path
60
	 *
61
	 * @param File $file
62
	 * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
63
	 * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
64
	 * @return null|\OCP\IImage false if no preview was generated
65
	 * @since 17.0.0
66
	 */
67
	abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
68
69
	/**
70
	 * Get a path to either the local file or temporary file
71
	 *
72
	 * @param File $file
73
	 * @param int $maxSize maximum size for temporary files
74
	 * @return string
75
	 */
76
	protected function getLocalFile(File $file, int $maxSize = null): string {
77
		$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
78
		if ($useTempFile) {
79
			$absPath = \OC::$server->getTempManager()->getTemporaryFile();
80
81
			$content = $file->fopen('r');
82
83
			if ($maxSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxSize of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
84
				$content = stream_get_contents($content, $maxSize);
85
			}
86
87
			file_put_contents($absPath, $content);
88
			$this->tmpFiles[] = $absPath;
89
			return $absPath;
90
		} else {
91
			return $file->getStorage()->getLocalFile($file->getInternalPath());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file->getStorage...ile->getInternalPath()) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
92
		}
93
	}
94
95
	/**
96
	 * Clean any generated temporary files
97
	 */
98
	protected function cleanTmpFiles() {
99
		foreach ($this->tmpFiles as $tmpFile) {
100
			unlink($tmpFile);
101
		}
102
103
		$this->tmpFiles = [];
104
	}
105
}
106