Completed
Pull Request — master (#56)
by Roeland
01:32
created

BackgroundScanner::getUserFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Copyright (c) 2012 Bart Visscher <[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 OCA\Files_Antivirus\Scanner\ScannerFactory;
12
use OCP\Files\File;
13
use OCP\Files\IMimeTypeLoader;
14
use OCP\IDBConnection;
15
use OCP\IL10N;
16
use OCP\Files\IRootFolder;
17
use OCP\ILogger;
18
use OCP\IUser;
19
use OCP\IUserManager;
20
21
class BackgroundScanner {
22
23
	const BATCH_SIZE = 10;
24
25
	/** @var IRootFolder */
26
	protected $rootFolder;
27
28
	/** @var ScannerFactory */
29
	private $scannerFactory;
30
31
	/** @var IL10N */
32
	private $l10n;
33
34
	/** @var  AppConfig  */
35
	private $appConfig;
36
37
	/** @var ILogger */
38
	protected $logger;
39
40
	/** @var IUserManager */
41
	protected $userManager;
42
43
	/** @var IDBConnection */
44
	protected $db;
45
46
	/** @var IMimeTypeLoader */
47
	protected $mimeTypeLoader;
48
49
	/**
50
	 * A constructor
51
	 *
52
	 * @param ScannerFactory $scannerFactory
53
	 * @param IL10N $l10n
54
	 * @param AppConfig $appConfig
55
	 * @param IRootFolder $rootFolder
56
	 * @param ILogger $logger
57
	 * @param IUserManager $userManager
58
	 * @param IDBConnection $db
59
	 * @param IMimeTypeLoader $mimeTypeLoader
60
	 */
61
	public function __construct(ScannerFactory $scannerFactory,
62
								IL10N $l10n,
63
								AppConfig $appConfig,
64
								IRootFolder $rootFolder,
65
								ILogger $logger,
66
								IUserManager $userManager,
67
								IDBConnection $db,
68
								IMimeTypeLoader $mimeTypeLoader
69
	){
70
		$this->rootFolder = $rootFolder;
71
		$this->scannerFactory = $scannerFactory;
72
		$this->l10n = $l10n;
73
		$this->appConfig = $appConfig;
74
		$this->logger = $logger;
75
		$this->userManager = $userManager;
76
		$this->db = $db;
77
		$this->mimeTypeLoader = $mimeTypeLoader;
78
	}
79
	
80
	/**
81
	 * Background scanner main job
82
	 */
83
	public function run(){
84
		// locate files that are not checked yet
85
		try {
86
			$result = $this->getFilesForScan();
87
		} catch(\Exception $e) {
88
			$this->logger->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
89
			return;
90
		}
91
92
		$cnt = 0;
93
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
94
			try {
95
				$fileId = $row['fileid'];
96
				$userId = $row['user_id'];
97
				/** @var IUser $owner */
98
				$owner = $this->userManager->get($userId);
99
				if (!$owner instanceof IUser){
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
					continue;
101
				}
102
				$this->scanOneFile($owner, $fileId);
103
				// increased only for successfully scanned files
104
				$cnt = $cnt + 1;
105
			} catch (\Exception $e) {
106
				$this->logger->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
107
			}
108
		}
109
	}
110
111
	protected function getFilesForScan(){
112
		$dirMimeTypeId = $this->mimeTypeLoader->getId('httpd/unix-directory');
113
114
		$qb = $this->db->getQueryBuilder();
115
116
		$sizeLimit = (int)$this->appConfig->getAvMaxFileSize();
117
		if ( $sizeLimit === -1 ){
118
			$sizeLimitExpr = $qb->expr()->neq('fc.size', $qb->expr()->literal('0'));
119
		} else {
120
			$sizeLimitExpr = $qb->expr()->andX(
121
				$qb->expr()->neq('fc.size', $qb->expr()->literal('0')),
122
				$qb->expr()->lt('fc.size', $qb->createNamedParameter($sizeLimit))
123
			);
124
		}
125
126
		$qb->select(['fc.fileid', 'mnt.user_id'])
127
			->from('filecache', 'fc')
128
			->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
129
			->innerJoin(
130
				'fc',
131
				'mounts',
132
				'mnt',
133
				$qb->expr()->andX(
134
					$qb->expr()->eq('fc.storage', 'mnt.storage_id')
135
				)
136
			)
137
			->where(
138
				$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
139
			)
140
			->andWhere(
141
				$qb->expr()->orX(
142
					$qb->expr()->isNull('fa.fileid'),
143
					$qb->expr()->gt('fc.mtime', 'fa.check_time')
144
				)
145
			)
146
			->andWhere(
147
				$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
148
			)
149
			->andWhere( $sizeLimitExpr )
150
		;
151
		return $qb->execute();
152
	}
153
154
	/**
155
	 * @param IUser $owner
156
	 * @param int $fileId
157
	 */
158
	protected function scanOneFile(IUser $owner, $fileId){
159
		$userFolder = $this->rootFolder->getUserFolder($owner->getUID());
160
		$files = $userFolder->getById($fileId);
161
162
		if (count($files) === 0) {
163
			return;
164
		}
165
166
		/** @var File $file */
167
		$file = array_pop($files);
168
169
		if (!($file instanceof File)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
170
			return;
171
		}
172
173
		$item = new Item($this->l10n, $file);
174
		$scanner = $this->scannerFactory->getScanner();
175
		$status = $scanner->scan($item);
176
		$status->dispatch($item, true);
177
	}
178
}
179