Completed
Pull Request — master (#230)
by Victor
98:07 queued 91:44
created

RequestHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * ownCloud - files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2017-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus;
15
16
use \OCP\IRequest;
17
18
class RequestHelper {
19
20
	/**
21
	 * @var  IRequest
22
	 */
23
	private $request;
24
25 4
	public function __construct(IRequest $request) {
26 4
		$this->request = $request;
27 4
	}
28
29
	/**
30
	 * Get current upload size
31
	 * returns null for chunks and when there is no upload
32
	 *
33
	 * @param string $path
34
	 *
35
	 * @return int|null
36
	 */
37 3
	public function getUploadSize($path) {
38 3
		$uploadSize = null;
39
40 3
		$requestMethod = $this->request->getMethod();
41 3
		$isRemoteScript = $this->isScriptName('remote.php');
42 3
		$isPublicScript = $this->isScriptName('public.php');
43
		// Are we uploading anything?
44 3
		if (\in_array($requestMethod, ['MOVE', 'PUT']) && $isRemoteScript) {
45
			// v1 && v2 Chunks are not scanned
46 1
			if ($requestMethod === 'PUT' &&  \strpos($path, 'uploads/') === 0) {
47 1
				return null;
48
			}
49
50
			if (\OC_FileChunking::isWebdavChunk() && \strpos($path, 'cache/') === 0) {
51
				return null;
52
			}
53
54
			if ($requestMethod === 'PUT') {
55
				$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
56
			} else {
57
				$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
58
			}
59 2
		} else if ($requestMethod === 'PUT' && $isPublicScript) {
60 1
			$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
61 1
		}
62
63 2
		return $uploadSize;
64
	}
65
66
	/**
67
	 *
68
	 * @param string $string
69
	 *
70
	 * @return bool
71
	 */
72 3
	public function isScriptName($string) {
73 3
		$pattern = \sprintf('|/%s|', \preg_quote($string));
74 3
		return \preg_match($pattern, $this->request->getScriptName()) === 1;
75
	}
76
}
77