Completed
Push — master ( 3363e2...c5c11c )
by
unknown
05:35
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
 * Copyright (c) 2017 Viktar Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus;
10
11
use \OCP\IRequest;
12
13
class RequestHelper {
14
15
	/** @var  IRequest */
16
	private $request;
17
18 2
	public function __construct(IRequest $request) {
19 2
		$this->request = $request;
20 2
	}
21
22
	/**
23
	 * Get current upload size
24
	 * returns null for chunks and when there is no upload
25
	 *
26
	 * @param string $path
27
	 * @return int|null
28
	 */
29 3
	public function getUploadSize($path) {
30 3
		$uploadSize = null;
31
32 3
		$requestMethod = $this->request->getMethod();
33 3
		$isRemoteScript = $this->isScriptName('remote.php');
34
		// Are we uploading anything?
35 3
		if (in_array($requestMethod, ['MOVE', 'PUT']) && $isRemoteScript) {
36
			// v1 && v2 Chunks are not scanned
37
			if (
38
				\OC_FileChunking::isWebdavChunk()
39
				|| ($requestMethod === 'PUT' &&  strpos($path, 'uploads/') === 0)
40
			) {
41
				return null;
42
			}
43
44
			if ($requestMethod === 'PUT') {
45
				$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
46
			} else {
47
				$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
48
			}
49
		}
50
51 3
		return $uploadSize;
52
	}
53
54
	/**
55
	 *
56
	 * @param string $string
57
	 * @return bool
58
	 */
59 3
	public function isScriptName($string) {
60 3
		$pattern = sprintf('|/%s|', preg_quote($string));
61 3
		return preg_match($pattern, $this->request->getScriptName()) === 1;
62
	}
63
}
64