Completed
Push — scalability_improvements ( 1f54dd...dcfdac )
by Pauli
19:38 queued 05:25
created

Scan::execute()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 36
nop 2
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
	 * @var \OCP\Files\Folder $rootFolder
39
	 */
40
	private $rootFolder;
41
42
	public function __construct(\OCP\IUserManager $userManager, $scanner, \OCP\Files\Folder $rootFolder) {
43
		$this->userManager = $userManager;
44
		$this->scanner = $scanner;
45
		$this->rootFolder = $rootFolder;
46
		parent::__construct();
47
	}
48
49
	protected function configure() {
50
		$this
51
			->setName('music:scan')
52
			->setDescription('rescan music')
53
			->addArgument(
54
					'user_id',
55
					InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
56
					'will rescan all music files of the given user(s)'
57
			)
58
			->addOption(
59
					'all',
60
					null,
61
					InputOption::VALUE_NONE,
62
					'will rescan all music files of all known users'
63
			)
64
			->addOption(
65
					'debug',
66
					null,
67
					InputOption::VALUE_NONE,
68
					'will run the scan in debug mode (memory usage)'
69
			)
70
		;
71
	}
72
73
	protected function execute(InputInterface $input, OutputInterface $output) {
74
		if (!$input->getOption('debug')) {
75
			$this->scanner->listen('\OCA\Music\Utility\Scanner', 'update', function($path) use ($output) {
76
				$output->writeln("Scanning <info>$path</info>");
77
			});
78
		}
79
80
		if ($input->getOption('all')) {
81
			$users = $this->userManager->search('');
82
		} else {
83
			$users = $input->getArgument('user_id');
84
		}
85
86
		foreach ($users as $user) {
87
			if (is_object($user)) {
88
				$user = $user->getUID();
89
			}
90
			\OC_Util::tearDownFS();
91
			\OC_Util::setupFS($user);
92
			$output->writeln("Start scan for <info>$user</info>");
93
			$userHome = $this->resolveUserFolder($user);
94
			$unscanned = $this->scanner->getUnscannedMusicFileIds($user, $userHome);
95
			$output->writeln('Found ' . count($unscanned) . ' new music files');
96
			
97
			if (count($unscanned)) {
98
				$processedCount = $this->scanner->scanFiles(
99
						$user, $userHome, $unscanned,
100
						$input->getOption('debug') ? $output : null);
101
				$output->writeln("Added $processedCount files to database");
102
			}
103
104
			if ($this->scanner->findCovers()) {
105
				$output->writeln("Some cover image(s) were found and added");
106
			}
107
		}
108
	}
109
110
	private function resolveUserFolder($userId) {
111
		$dir = '/' . $userId;
112
		$root = $this->rootFolder;
113
114
		// copy of getUserServer of server container
115
		$folder = null;
116
117 View Code Duplication
		if (!$root->nodeExists($dir)) {
118
			$folder = $root->newFolder($dir);
119
		} else {
120
			$folder = $root->get($dir);
121
		}
122
123
		$dir = '/files';
124 View Code Duplication
		if (!$folder->nodeExists($dir)) {
125
			$folder = $folder->newFolder($dir);
126
		} else {
127
			$folder = $folder->get($dir);
128
		}
129
130
		return $folder;
131
	}
132
}
133