Passed
Push — master ( 3f546e...a23a23 )
by Daniel
28:53 queued 14s
created

PicoAsset::isPublicAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[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
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Model;
26
27
use OCA\CMSPico\Files\StorageFile;
28
use OCP\Files\NotPermittedException;
29
30
class PicoAsset
31
{
32
	/** @var string[] */
33
	private static $secureMimeTypes = [
34
		'text/plain' => 'text/plain',
35
		'image/bmp' => 'image/bmp',
36
		'image/gif' => 'image/gif',
37
		'image/jpeg' => 'image/jpeg',
38
		'image/png' => 'image/png',
39
		'image/tiff' => 'image/tiff',
40
		'image/webp' => 'image/webp',
41
	];
42
43
	/** @var StorageFile */
44
	private $file;
45
46
	/** @var bool */
47
	private $publicAsset;
48
49
	/** @var \DateTime|null */
50
	private $lastModified;
51
52
	/** @var string|null */
53
	private $secureMimeType;
54
55
	/**
56
	 * Asset constructor.
57
	 *
58
	 * @param StorageFile $file
59
	 * @param bool        $publicAsset
60
	 *
61
	 * @throws NotPermittedException
62
	 */
63 3
	public function __construct(StorageFile $file, bool $publicAsset)
64
	{
65 3
		$this->file = $file;
66 3
		$this->publicAsset = $publicAsset;
67
68 3
		if (!$this->isReadable()) {
69
			throw new NotPermittedException();
70
		}
71 3
	}
72
73
	/**
74
	 * @return string
75
	 */
76
	public function getPath(): string
77
	{
78
		return $this->file->getPath();
79
	}
80
81
	/**
82
	 * @return string
83
	 */
84 3
	public function getName(): string
85
	{
86 3
		return $this->file->getName();
87
	}
88
89
	/**
90
	 * @return string
91
	 */
92
	public function getExtension(): string
93
	{
94
		return $this->file->getExtension();
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100 3
	public function getETag(): string
101
	{
102 3
		return $this->file->getOCNode()->getEtag();
103
	}
104
105
	/**
106
	 * @return \DateTime
107
	 */
108 3
	public function getLastModified(): \DateTime
109
	{
110 3
		if ($this->lastModified === null) {
111 3
			$lastModifiedTime = $this->file->getOCNode()->getMTime();
112 3
			$this->lastModified = (new \DateTime())->setTimestamp($lastModifiedTime);
113
		}
114
115 3
		return $this->lastModified;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121 3
	public function getMimeType(): string
122
	{
123 3
		return $this->file->getOCNode()->getMimetype() ?: 'application/octet-stream';
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129 3
	public function getSecureMimeType(): string
130
	{
131 3
		if ($this->secureMimeType === null) {
132 3
			if (isset(self::$secureMimeTypes[$this->getMimeType()])) {
133
				// whitelisted secure mime types
134
				// files of this type are secure by design, or can't be used in a insecure manner due to HTMLPurifier
135 3
				$this->secureMimeType = self::$secureMimeTypes[$this->getMimeType()];
136
			} else {
137
				// fallback to Nextcloud's built-in list of secure mime types
138
				$mimeTypeDetector = \OC::$server->getMimeTypeDetector();
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getMimeTypeDetector() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

138
				$mimeTypeDetector = /** @scrutinizer ignore-deprecated */ \OC::$server->getMimeTypeDetector();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
139
				$this->secureMimeType = $mimeTypeDetector->getSecureMimeType($this->getMimeType());
140
			}
141
		}
142
143 3
		return $this->secureMimeType;
144
	}
145
146
	/**
147
	 * @return bool
148
	 */
149 3
	public function isReadable(): bool
150
	{
151 3
		return $this->file->isReadable();
152
	}
153
154
	/**
155
	 * @return bool
156
	 */
157
	public function isPublicAsset(): bool
158
	{
159
		return $this->publicAsset;
160
	}
161
162
	/**
163
	 * @return string
164
	 */
165 1
	public function getContent(): string
166
	{
167 1
		return $this->file->getContent();
168
	}
169
}
170