3737use OC \Core \Command \InterruptedException ;
3838use OC \DB \Connection ;
3939use OC \DB \ConnectionAdapter ;
40+ use OCP \Files \File ;
4041use OC \ForbiddenException ;
42+ use OC \Metadata \MetadataManager ;
4143use OCP \EventDispatcher \IEventDispatcher ;
44+ use OCP \Files \IRootFolder ;
4245use OCP \Files \Mount \IMountPoint ;
4346use OCP \Files \NotFoundException ;
4447use OCP \Files \StorageNotAvailableException ;
5154use Symfony \Component \Console \Output \OutputInterface ;
5255
5356class 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 ;
5463
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+ ) {
6569 $ this ->userManager = $ userManager ;
6670 parent ::__construct ();
71+ $ this ->root = $ rootFolder ;
72+ $ this ->metadataManager = $ metadataManager ;
6773 }
6874
6975 protected function configure () {
@@ -83,6 +89,12 @@ protected function configure() {
8389 InputArgument::OPTIONAL ,
8490 '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 '
8591 )
92+ ->addOption (
93+ 'generate-metadata ' ,
94+ null ,
95+ InputOption::VALUE_NONE ,
96+ 'Generate metadata for all scanned files '
97+ )
8698 ->addOption (
8799 'all ' ,
88100 null ,
@@ -106,21 +118,26 @@ protected function configure() {
106118 );
107119 }
108120
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 {
110122 $ connection = $ this ->reconnectToDatabase ($ output );
111123 $ scanner = new \OC \Files \Utils \Scanner (
112124 $ user ,
113125 new ConnectionAdapter ($ connection ),
114- \OC ::$ server ->query (IEventDispatcher::class),
126+ \OC ::$ server ->get (IEventDispatcher::class),
115127 \OC ::$ server ->get (LoggerInterface::class)
116128 );
117129
118130 # 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 ) {
121132 $ output ->writeln ("\tFile \t<info> $ path</info> " , OutputInterface::VERBOSITY_VERBOSE );
122133 ++$ this ->filesCounter ;
123134 $ this ->abortIfInterrupted ();
135+ if ($ scanMetadata ) {
136+ $ node = $ this ->root ->get ($ path );
137+ if ($ node instanceof File) {
138+ $ this ->metadataManager ->generateMetadata ($ node , false );
139+ }
140+ }
124141 });
125142
126143 $ scanner ->listen ('\OC\Files\Utils\Scanner ' , 'scanFolder ' , function ($ path ) use ($ output ) {
@@ -197,7 +214,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
197214 ++$ user_count ;
198215 if ($ this ->userManager ->userExists ($ user )) {
199216 $ 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 ' ));
201218 $ output ->writeln ('' , OutputInterface::VERBOSITY_VERBOSE );
202219 } else {
203220 $ output ->writeln ("<error>Unknown user $ user_count $ user</error> " );
@@ -291,7 +308,7 @@ protected function showSummary($headers, $rows, OutputInterface $output) {
291308 protected function formatExecTime () {
292309 $ secs = round ($ this ->execTime );
293310 # 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 );
295312 }
296313
297314 protected function reconnectToDatabase (OutputInterface $ output ): Connection {
0 commit comments