Completed
Push — scanner_improvements ( 4bed41 )
by Pauli
10:32
created

Scan::resolveUserFolder()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 10
Ratio 45.45 %

Importance

Changes 0
Metric Value
dl 10
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 1
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Thomas Müller <[email protected]>
10
 * @author Bart Visscher <[email protected]>
11
 * @author Leizh <[email protected]>
12
 * @copyright Thomas Müller 2013
13
 * @copyright Bart Visscher 2013
14
 * @copyright Leizh 2014
15
 */
16
17
namespace OCA\Music\Command;
18
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
26
use OCA\Music\Utility\Scanner;
27
28
class Scan extends Command {
29
	/**
30
	 * @var \OCP\IUserManager $userManager
31
	 */
32
	private $userManager;
33
	/**
34
	 * @var  Scanner
35
	 */
36
	private $scanner;
37
38
	public function __construct(\OCP\IUserManager $userManager, $scanner) {
39
		$this->userManager = $userManager;
40
		$this->scanner = $scanner;
41
		parent::__construct();
42
	}
43
44
	protected function configure() {
45
		$this
46
			->setName('music:scan')
47
			->setDescription('rescan music')
48
			->addArgument(
49
					'user_id',
50
					InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
51
					'will rescan all music files of the given user(s)'
52
			)
53
			->addOption(
54
					'all',
55
					null,
56
					InputOption::VALUE_NONE,
57
					'will rescan all music files of all known users'
58
			)
59
			->addOption(
60
					'debug',
61
					null,
62
					InputOption::VALUE_NONE,
63
					'will run the scan in debug mode (memory usage)'
64
			)
65
		;
66
	}
67
68
	protected function execute(InputInterface $input, OutputInterface $output) {
69
		if (!$input->getOption('debug')) {
70
			$this->scanner->listen('\OCA\Music\Utility\Scanner', 'update', function($path) use ($output) {
71
				$output->writeln("Scanning <info>$path</info>");
72
			});
73
		}
74
75
		if ($input->getOption('all')) {
76
			$users = $this->userManager->search('');
77
		} else {
78
			$users = $input->getArgument('user_id');
79
		}
80
81
		foreach ($users as $user) {
82
			if (is_object($user)) {
83
				$user = $user->getUID();
84
			}
85
			\OC_Util::tearDownFS();
86
			\OC_Util::setupFS($user);
87
			$output->writeln("Start scan for <info>$user</info>");
88
			$userHome = $this->scanner->resolveUserFolder($user);
89
			$unscanned = $this->scanner->getUnscannedMusicFileIds($user, $userHome);
90
			$output->writeln('Found ' . count($unscanned) . ' new music files');
91
			
92
			if (count($unscanned)) {
93
				$processedCount = $this->scanner->scanFiles(
94
						$user, $userHome, $unscanned,
95
						$input->getOption('debug') ? $output : null);
96
				$output->writeln("Added $processedCount files to database");
97
			}
98
99
			if ($this->scanner->findCovers()) {
100
				$output->writeln("Some cover image(s) were found and added");
101
			}
102
		}
103
	}
104
}
105