Completed
Push — skip-by-size ( b521aa )
by Victor
07:48
created

RequestHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
	public function __construct(IRequest $request) {
19
		$this->request = $request;
20
	}
21
22
	/**
23
	 * @return int|null
24
	 */
25
	public function getUploadSize() {
26
		// Chunks are not scanned anyway. Skip chunks dav v1 due to performance
27
		if (\OC_FileChunking::isWebdavChunk()) {
28
			return null;
29
		}
30
31
		try {
32
			$pathInfo = $this->request->getPathInfo();
33
		} catch (\Exception $e) {
34
			// CLI?
35
			return null;
36
		}
37
38
		$requestMethod = $this->request->getMethod();
39
40
		$isRemoteScript = $this->isScriptName('remote.php');
41
		$isDavPathV1 = $pathInfo === '/webdav' || strpos($pathInfo, '/webdav/') === 0;
42
		$isDavPathV2 = $pathInfo === '/dav/files'
43
			|| strpos($pathInfo, '/dav/files/') === 0
44
			|| strpos($pathInfo, '/dav/uploads/') === 0
45
		;
46
		$isDavRequest = $isRemoteScript	&& ($isDavPathV1 || $isDavPathV2);
47
48
		// No upload in progress
49
		if (!$isDavRequest || !in_array($requestMethod, ['MOVE', 'PUT'])){
50
			return null;
51
		}
52
53
		$uploadSize = (int) $this->request->getHeader('CONTENT_LENGTH');
54
55
		return $uploadSize;
56
	}
57
58
	/**
59
	 *
60
	 * @param $string
61
	 * @return bool
62
	 */
63
	public function isScriptName($string) {
64
		$pattern = sprintf('|/%s|', preg_quote($string));
65
		return preg_match($pattern, $this->request->getScriptName()) === 1;
66
	}
67
}