37
37
use OC \Core \Command \InterruptedException ;
38
38
use OC \DB \Connection ;
39
39
use OC \DB \ConnectionAdapter ;
40
+ use OCP \Files \File ;
40
41
use OC \ForbiddenException ;
42
+ use OC \Metadata \MetadataManager ;
41
43
use OCP \EventDispatcher \IEventDispatcher ;
44
+ use OCP \Files \IRootFolder ;
42
45
use OCP \Files \Mount \IMountPoint ;
43
46
use OCP \Files \NotFoundException ;
44
47
use OCP \Files \StorageNotAvailableException ;
51
54
use Symfony \Component \Console \Output \OutputInterface ;
52
55
53
56
class Scan extends Base {
57
+ private IUserManager $ userManager ;
58
+ protected float $ execTime = 0 ;
59
+ protected int $ foldersCounter = 0 ;
60
+ protected int $ filesCounter = 0 ;
61
+ private IRootFolder $ root ;
62
+ private MetadataManager $ metadataManager ;
54
63
55
- /** @var IUserManager $userManager */
56
- private $ userManager ;
57
- /** @var float */
58
- protected $ execTime = 0 ;
59
- /** @var int */
60
- protected $ foldersCounter = 0 ;
61
- /** @var int */
62
- protected $ filesCounter = 0 ;
63
-
64
- public function __construct (IUserManager $ userManager ) {
64
+ public function __construct (
65
+ IUserManager $ userManager ,
66
+ IRootFolder $ rootFolder ,
67
+ MetadataManager $ metadataManager
68
+ ) {
65
69
$ this ->userManager = $ userManager ;
66
70
parent ::__construct ();
71
+ $ this ->root = $ rootFolder ;
72
+ $ this ->metadataManager = $ metadataManager ;
67
73
}
68
74
69
75
protected function configure () {
@@ -83,6 +89,12 @@ protected function configure() {
83
89
InputArgument::OPTIONAL ,
84
90
'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored '
85
91
)
92
+ ->addOption (
93
+ 'generate-metadata ' ,
94
+ null ,
95
+ InputOption::VALUE_NONE ,
96
+ 'Generate metadata for all scanned files '
97
+ )
86
98
->addOption (
87
99
'all ' ,
88
100
null ,
@@ -106,21 +118,26 @@ protected function configure() {
106
118
);
107
119
}
108
120
109
- protected function scanFiles ($ user , $ path , OutputInterface $ output , $ backgroundScan = false , $ recursive = true , $ homeOnly = false ) {
121
+ protected function scanFiles (string $ user , string $ path , bool $ scanMetadata , OutputInterface $ output , bool $ backgroundScan = false , bool $ recursive = true , bool $ homeOnly = false ): void {
110
122
$ connection = $ this ->reconnectToDatabase ($ output );
111
123
$ scanner = new \OC \Files \Utils \Scanner (
112
124
$ user ,
113
125
new ConnectionAdapter ($ connection ),
114
- \OC ::$ server ->query (IEventDispatcher::class),
126
+ \OC ::$ server ->get (IEventDispatcher::class),
115
127
\OC ::$ server ->get (LoggerInterface::class)
116
128
);
117
129
118
130
# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
119
-
120
- $ scanner ->listen ('\OC\Files\Utils\Scanner ' , 'scanFile ' , function ($ path ) use ($ output ) {
131
+ $ scanner ->listen ('\OC\Files\Utils\Scanner ' , 'scanFile ' , function (string $ path ) use ($ output , $ scanMetadata ) {
121
132
$ output ->writeln ("\tFile \t<info> $ path</info> " , OutputInterface::VERBOSITY_VERBOSE );
122
133
++$ this ->filesCounter ;
123
134
$ this ->abortIfInterrupted ();
135
+ if ($ scanMetadata ) {
136
+ $ node = $ this ->root ->get ($ path );
137
+ if ($ node instanceof File) {
138
+ $ this ->metadataManager ->generateMetadata ($ node , false );
139
+ }
140
+ }
124
141
});
125
142
126
143
$ scanner ->listen ('\OC\Files\Utils\Scanner ' , 'scanFolder ' , function ($ path ) use ($ output ) {
@@ -197,7 +214,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
197
214
++$ user_count ;
198
215
if ($ this ->userManager ->userExists ($ user )) {
199
216
$ output ->writeln ("Starting scan for user $ user_count out of $ users_total ( $ user) " );
200
- $ this ->scanFiles ($ user , $ path , $ output , $ input ->getOption ('unscanned ' ), !$ input ->getOption ('shallow ' ), $ input ->getOption ('home-only ' ));
217
+ $ this ->scanFiles ($ user , $ path , $ input -> getOption ( ' generate-metadata ' ), $ output , $ input ->getOption ('unscanned ' ), !$ input ->getOption ('shallow ' ), $ input ->getOption ('home-only ' ));
201
218
$ output ->writeln ('' , OutputInterface::VERBOSITY_VERBOSE );
202
219
} else {
203
220
$ output ->writeln ("<error>Unknown user $ user_count $ user</error> " );
@@ -291,7 +308,7 @@ protected function showSummary($headers, $rows, OutputInterface $output) {
291
308
protected function formatExecTime () {
292
309
$ secs = round ($ this ->execTime );
293
310
# convert seconds into HH:MM:SS form
294
- return sprintf ('%02d:%02d:%02d ' , (int )($ secs / 3600 ), ( (int )($ secs / 60 ) % 60 ), $ secs % 60 );
311
+ return sprintf ('%02d:%02d:%02d ' , (int )($ secs / 3600 ), ((int )($ secs / 60 ) % 60 ), $ secs % 60 );
295
312
}
296
313
297
314
protected function reconnectToDatabase (OutputInterface $ output ): Connection {
0 commit comments