Completed
Pull Request — master (#226)
by Victor
17:08 queued 15:30
created

RequestHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 72.41%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 84
ccs 21
cts 29
cp 0.7241
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setSizeForPath() 0 3 1
A isScriptName() 0 4 1
A __construct() 0 4 1
D getUploadSize() 0 33 9
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 OC\Cache\CappedMemoryCache;
17
use \OCP\IRequest;
18
19
/**
20
 * Used to detect the size of the uploaded file
21
 *
22
 * @package OCA\Files_Antivirus
23
 */
24
class RequestHelper {
25
	/**
26
	 * @var  IRequest
27
	 */
28
	private $request;
29
30
	/**
31
	 * @var CappedMemoryCache
32
	 */
33
	private $fileSizeCache;
34
35
	/**
36
	 * RequestHelper constructor.
37
	 *
38
	 * @param IRequest $request
39
	 */
40 4
	public function __construct(IRequest $request) {
41 4
		$this->request = $request;
42 4
		$this->fileSizeCache = new CappedMemoryCache();
43 4
	}
44
45
	/**
46
	 * @param string $path
47
	 * @param string $size
48
	 *
49
	 * @return void
50
	 */
51
	public function setSizeForPath($path, $size) {
52
		$this->fileSizeCache->set($path, $size);
53
	}
54
55
	/**
56
	 * Get current upload size
57
	 * returns null for chunks and when there is no upload
58
	 *
59
	 * @param string $path
60
	 *
61
	 * @return int|null
62
	 */
63 3
	public function getUploadSize($path) {
64 3
		$cachedSize = $this->fileSizeCache->get($path);
65 3
		if ($cachedSize > 0) {
66
			return $cachedSize;
67
		}
68
69 3
		$requestMethod = $this->request->getMethod();
70
		// Are we uploading anything?
71 3
		if ($requestMethod !== 'PUT') {
72 1
			return null;
73
		}
74 2
		$isRemoteScript = $this->isScriptName('remote.php');
75 2
		$isPublicScript = $this->isScriptName('public.php');
76 2
		if (!$isRemoteScript && !$isPublicScript) {
77
			return null;
78
		}
79
80 2
		if ($isRemoteScript) {
81
			// v1 && v2 Chunks are not scanned
82 1
			if (\strpos($path, 'uploads/') === 0) {
83 1
				return null;
84
			}
85
86
			if (\OC_FileChunking::isWebdavChunk()
87
				&& \strpos($path, 'cache/') === 0
88
			) {
89
				return null;
90
			}
91
		}
92 1
		$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
93
94 1
		return $uploadSize;
95
	}
96
97
	/**
98
	 *
99
	 * @param string $string
100
	 *
101
	 * @return bool
102
	 */
103 2
	public function isScriptName($string) {
104 2
		$pattern = \sprintf('|/%s|', \preg_quote($string));
105 2
		return \preg_match($pattern, $this->request->getScriptName()) === 1;
106
	}
107
}
108