Completed
Pull Request — stable8.2 (#135)
by Victor
03:00
created

BackgroundScanner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.73%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 125
ccs 63
cts 71
cp 0.8873
rs 10

4 Methods

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