Completed
Push — allow_disabling_background_sca... ( 6fed93...05460f )
by Patrick
06:06
created

RequestHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isScriptName() 0 4 1
D getUploadSize() 0 28 10
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
	 * 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
	public function getUploadSize($path) {
30
		$uploadSize = null;
31
32
		$requestMethod = $this->request->getMethod();
33
		$isRemoteScript = $this->isScriptName('remote.php');
34
		$isPublicScript = $this->isScriptName('public.php');
35
		// Are we uploading anything?
36
		if (in_array($requestMethod, ['MOVE', 'PUT']) && $isRemoteScript) {
37
			// v1 && v2 Chunks are not scanned
38
			if ($requestMethod === 'PUT' &&  strpos($path, 'uploads/') === 0) {
39
				return null;
40
			}
41
42
			if (\OC_FileChunking::isWebdavChunk() && strpos($path, 'cache/') === 0) {
43
				return null;
44
			}
45
46
			if ($requestMethod === 'PUT') {
47
				$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
48
			} else {
49
				$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
50
			}
51
		} else if ($requestMethod === 'PUT' && $isPublicScript) {
52
			$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
53
		}
54
55
		return $uploadSize;
56
	}
57
58
	/**
59
	 *
60
	 * @param string $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
}
68