77 */
88namespace OCA \Files_Sharing \Command ;
99
10+ use OCA \Files_Sharing \External \ExternalShareMapper ;
1011use OCP \DB \QueryBuilder \IQueryBuilder ;
1112use OCP \Federation \ICloudIdManager ;
1213use OCP \IDBConnection ;
2223class CleanupRemoteStorages extends Command {
2324
2425 public function __construct (
25- protected IDBConnection $ connection ,
26- private ICloudIdManager $ cloudIdManager ,
26+ protected readonly IDBConnection $ connection ,
27+ private readonly ICloudIdManager $ cloudIdManager ,
28+ private readonly ExternalShareMapper $ externalShareMapper ,
2729 ) {
2830 parent ::__construct ();
2931 }
3032
31- protected function configure () {
33+ protected function configure (): void {
3234 $ this
3335 ->setName ('sharing:cleanup-remote-storages ' )
3436 ->setDescription ('Cleanup shared storage entries that have no matching entry in the shares_external table ' )
@@ -37,6 +39,12 @@ protected function configure() {
3739 null ,
3840 InputOption::VALUE_NONE ,
3941 'only show which storages would be deleted '
42+ )
43+ ->addOption (
44+ 'all ' ,
45+ null ,
46+ InputOption::VALUE_NONE ,
47+ 'Delete every external shares and their storage ' ,
4048 );
4149 }
4250
@@ -46,11 +54,12 @@ public function execute(InputInterface $input, OutputInterface $output): int {
4654 $ output ->writeln (count ($ remoteStorages ) . ' remote storage(s) need(s) to be checked ' );
4755
4856 $ remoteShareIds = $ this ->getRemoteShareIds ();
57+ $ all = $ input ->getOption ('all ' );
4958
5059 $ output ->writeln (count ($ remoteShareIds ) . ' remote share(s) exist ' );
5160
5261 foreach ($ remoteShareIds as $ id => $ remoteShareId ) {
53- if (isset ($ remoteStorages [$ remoteShareId ])) {
62+ if ($ all || isset ($ remoteStorages [$ remoteShareId ])) {
5463 if ($ input ->getOption ('dry-run ' ) || $ output ->isVerbose ()) {
5564 $ output ->writeln ("<info> $ remoteShareId belongs to remote share $ id</info> " );
5665 }
@@ -74,10 +83,14 @@ public function execute(InputInterface $input, OutputInterface $output): int {
7483 }
7584 }
7685 }
77- return 0 ;
86+
87+ if ($ all ) {
88+ $ this ->externalShareMapper ->deleteAll ();
89+ }
90+ return Command::SUCCESS ;
7891 }
7992
80- public function countFiles ($ numericId , OutputInterface $ output ) {
93+ public function countFiles ($ numericId , OutputInterface $ output ): void {
8194 $ queryBuilder = $ this ->connection ->getQueryBuilder ();
8295 $ queryBuilder ->select ($ queryBuilder ->func ()->count ('fileid ' ))
8396 ->from ('filecache ' )
@@ -92,7 +105,7 @@ public function countFiles($numericId, OutputInterface $output) {
92105 $ output ->writeln ("$ count files can be deleted for storage $ numericId " );
93106 }
94107
95- public function deleteStorage ($ id , $ numericId , OutputInterface $ output ) {
108+ public function deleteStorage ($ id , $ numericId , OutputInterface $ output ): void {
96109 $ queryBuilder = $ this ->connection ->getQueryBuilder ();
97110 $ queryBuilder ->delete ('storages ' )
98111 ->where ($ queryBuilder ->expr ()->eq (
@@ -106,7 +119,7 @@ public function deleteStorage($id, $numericId, OutputInterface $output) {
106119 $ this ->deleteFiles ($ numericId , $ output );
107120 }
108121
109- public function deleteFiles ($ numericId , OutputInterface $ output ) {
122+ public function deleteFiles ($ numericId , OutputInterface $ output ): void {
110123 $ queryBuilder = $ this ->connection ->getQueryBuilder ();
111124 $ queryBuilder ->delete ('filecache ' )
112125 ->where ($ queryBuilder ->expr ()->eq (
@@ -119,7 +132,7 @@ public function deleteFiles($numericId, OutputInterface $output) {
119132 $ output ->writeln ("deleted $ count files " );
120133 }
121134
122- public function getRemoteStorages () {
135+ private function getRemoteStorages (): array {
123136 $ queryBuilder = $ this ->connection ->getQueryBuilder ();
124137 $ queryBuilder ->select (['id ' , 'numeric_id ' ])
125138 ->from ('storages ' )
@@ -148,7 +161,10 @@ public function getRemoteStorages() {
148161 return $ remoteStorages ;
149162 }
150163
151- public function getRemoteShareIds () {
164+ /**
165+ * @return array<string, string>
166+ */
167+ private function getRemoteShareIds (): array {
152168 $ queryBuilder = $ this ->connection ->getQueryBuilder ();
153169 $ queryBuilder ->select (['id ' , 'share_token ' , 'owner ' , 'remote ' ])
154170 ->from ('share_external ' );
@@ -159,7 +175,6 @@ public function getRemoteShareIds() {
159175 while ($ row = $ result ->fetchAssociative ()) {
160176 $ cloudId = $ this ->cloudIdManager ->getCloudId ($ row ['owner ' ], $ row ['remote ' ]);
161177 $ remote = $ cloudId ->getRemote ();
162-
163178 $ remoteShareIds [$ row ['id ' ]] = 'shared:: ' . md5 ($ row ['share_token ' ] . '@ ' . $ remote );
164179 }
165180 $ result ->closeCursor ();
0 commit comments