Completed
Pull Request — stable8.2 (#145)
by Victor
03:04
created

BackgroundScanner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 131
ccs 64
cts 72
cp 0.8889
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initFS() 0 12 2
B run() 0 64 7
A check() 0 3 1
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 OCP\IUserManager;
12
use OCP\IL10N;
13
14
class BackgroundScanner {
15
16
	const BATCH_SIZE = 10;
17
18
	/** @var ScannerFactory */
19
	private $scannerFactory;
20
	
21
	/** @var IUserManager */
22
	private $userManager;
23
24
	/** @var IL10N */
25
	private $l10n;
26
27
	/** @var  AppConfig  */
28
	private $appConfig;
29
30
	/** @var string */
31
	protected $currentFilesystemUser;
32
33
	/** @var \OCP\IUserSession */
34
	protected $userSession;
35
36
	/**
37
	 * A constructor
38
	 *
39
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
40
	 * @param AppConfig $appConfig
41
	 * @param IUserManager $userManager
42
	 * @param IL10N $l10n
43
	 */
44 1
	public function __construct(ScannerFactory $scannerFactory, AppConfig $appConfig, IUserManager $userManager, IL10N $l10n){
45 1
		$this->scannerFactory = $scannerFactory;
46 1
		$this->userManager = $userManager;
47 1
		$this->l10n = $l10n;
48 1
		$this->appConfig = $appConfig;
49 1
	}
50
	
51
	/**
52
	 * Background scanner main job
53
	 * @return null
54
	 */
55 1
	public function run(){
56 1
		if (!$this->initFS()) {
57
			return;
58
		}
59
		// locate files that are not checked yet
60 1
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
61
		try {
62 1
			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
63 1
			$qb->select(['fc.fileid'])
64 1
				->from('filecache', 'fc')
65 1
				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
66 1
				->innerJoin(
67 1
					'fc',
68 1
					'storages',
69 1
					'ss',
70 1
					$qb->expr()->andX(
71 1
						$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
72 1
						$qb->expr()->orX(
73 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
74 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
75 1
						)
76 1
					)
77 1
				)
78 1
				->where(
79 1
					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
80 1
				)
81 1
				->andWhere(
82 1
					$qb->expr()->orX(
83 1
						$qb->expr()->isNull('fa.fileid'),
84 1
						$qb->expr()->gt('fc.mtime', 'fa.check_time')
85 1
					)
86 1
				)
87 1
				->andWhere(
88 1
					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
89 1
				)
90 1
				->andWhere(
91 1
					$qb->expr()->neq('fc.size', $qb->expr()->literal('0'))
92 1
				)
93
			;
94 1
			$result = $qb->execute();
95 1
		} catch(\Exception $e) {
96
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
97
			return;
98
		}
99
100 1
		$view = new \OC\Files\View('');
101 1
		$cnt = 0;
102 1
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
103
			try {
104 1
				$path = $view->getPath($row['fileid']);
105 1
				if (!is_null($path)) {
106 1
					$item = new Item($this->l10n, $view, $path, $row['fileid']);
107 1
					$scanner = $this->scannerFactory->getScanner();
108 1
					$status = $scanner->scan($item);
109 1
					$status->dispatch($item, true);
110
 					// increased only for successfully scanned files
111 1
					$cnt = $cnt + 1;
112 1
				}
113 1
			} catch (\Exception $e){
114
				\OC::$server->getLogger()->debug( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
115
			}
116 1
		}
117 1
		\OC_Util::tearDownFS();
118 1
	}
119
120
	/**
121
	 * A hack to access files and views. Better than before.
122
	 *
123
	 * @return bool
124
	 */
125 1
	protected function initFS(){
126
		//Need any valid user to mount FS
127 1
		$results = $this->userManager->search('', 2, 0);
128 1
		$anyUser = array_pop($results);
129 1
		if (is_null($anyUser)) {
130
			\OC::$server->getLogger()->error("Failed to setup file system", ['app' => 'files_antivirus']);
131
			return false;
132
		}
133 1
		\OC_Util::tearDownFS();
134 1
		\OC_Util::setupFS($anyUser->getUID());
135 1
		return true;
136
	}
137
138
	/**
139
	 * @deprecated 
140
	 */
141
	public static function check(){
142
		return \OCA\Files_Antivirus\Cron\Task::run();
143
	}
144
}
145