77 */
88namespace OCA \Files_Trashbin \Command ;
99
10+ use OC \Core \Command \Base ;
11+ use OC \Files \SetupManager ;
12+ use OC \User \LazyUser ;
1013use OCP \Files \IRootFolder ;
14+ use OCP \Files \NotFoundException ;
15+ use OCP \Files \NotPermittedException ;
1116use OCP \IDBConnection ;
17+ use OCP \IUser ;
1218use OCP \IUserBackend ;
1319use OCP \IUserManager ;
1420use OCP \Util ;
15- use Symfony \Component \Console \Command \Command ;
1621use Symfony \Component \Console \Exception \InvalidOptionException ;
1722use Symfony \Component \Console \Input \InputArgument ;
1823use Symfony \Component \Console \Input \InputInterface ;
1924use Symfony \Component \Console \Input \InputOption ;
2025use Symfony \Component \Console \Output \OutputInterface ;
2126
22- class CleanUp extends Command {
27+ class CleanUp extends Base {
2328
2429 public function __construct (
2530 protected IRootFolder $ rootFolder ,
2631 protected IUserManager $ userManager ,
2732 protected IDBConnection $ dbConnection ,
33+ protected SetupManager $ setupManager ,
2834 ) {
2935 parent ::__construct ();
3036 }
3137
3238 protected function configure () {
39+ parent ::configure ();
3340 $ this
3441 ->setName ('trashbin:cleanup ' )
3542 ->setDescription ('Remove deleted files ' )
@@ -53,9 +60,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5360 throw new InvalidOptionException ('Either specify a user_id or --all-users ' );
5461 } elseif (!empty ($ users )) {
5562 foreach ($ users as $ user ) {
56- if ($ this ->userManager ->userExists ($ user )) {
63+ $ userObject = $ this ->userManager ->get ($ user );
64+ if ($ userObject ) {
5765 $ output ->writeln ("Remove deleted files of <info> $ user</info> " );
58- $ this ->removeDeletedFiles ($ user , $ output , $ verbose );
66+ $ this ->removeDeletedFiles ($ userObject , $ output , $ verbose );
5967 } else {
6068 $ output ->writeln ("<error>Unknown user $ user</error> " );
6169 return 1 ;
@@ -75,7 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7583 $ users = $ backend ->getUsers ('' , $ limit , $ offset );
7684 foreach ($ users as $ user ) {
7785 $ output ->writeln (" <info> $ user</info> " );
78- $ this ->removeDeletedFiles ($ user , $ output , $ verbose );
86+ $ userObject = new LazyUser ($ user , $ this ->userManager , null , $ backend );
87+ $ this ->removeDeletedFiles ($ userObject , $ output , $ verbose );
7988 }
8089 $ offset += $ limit ;
8190 } while (count ($ users ) >= $ limit );
@@ -89,30 +98,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8998 /**
9099 * remove deleted files for the given user
91100 */
92- protected function removeDeletedFiles (string $ uid , OutputInterface $ output , bool $ verbose ): void {
93- \OC_Util:: tearDownFS ();
94- \OC_Util:: setupFS ( $ uid );
95- $ path = '/ ' . $ uid . '/files_trashbin ' ;
96- if ( $ this -> rootFolder -> nodeExists ( $ path )) {
101+ protected function removeDeletedFiles (IUser $ user , OutputInterface $ output , bool $ verbose ): void {
102+ $ this -> setupManager -> tearDown ();
103+ $ this -> setupManager -> setupForUser ( $ user );
104+ $ path = '/ ' . $ user -> getUID () . '/files_trashbin ' ;
105+ try {
97106 $ node = $ this ->rootFolder ->get ($ path );
98-
99- if ($ verbose ) {
100- $ output ->writeln ('Deleting <info> ' . Util::humanFileSize ($ node ->getSize ()) . "</info> in trash for <info> $ uid</info>. " );
101- }
102- $ node ->delete ();
103- if ($ this ->rootFolder ->nodeExists ($ path )) {
104- $ output ->writeln ('<error>Trash folder sill exists after attempting to delete it</error> ' );
105- return ;
106- }
107- $ query = $ this ->dbConnection ->getQueryBuilder ();
108- $ query ->delete ('files_trash ' )
109- ->where ($ query ->expr ()->eq ('user ' , $ query ->createParameter ('uid ' )))
110- ->setParameter ('uid ' , $ uid );
111- $ query ->executeStatement ();
112- } else {
107+ } catch (NotFoundException |NotPermittedException ) {
113108 if ($ verbose ) {
114- $ output ->writeln ("No trash found for <info> $ uid </info> " );
109+ $ output ->writeln ("No trash found for <info> { $ user -> getUID ()} </info> " );
115110 }
111+ return ;
112+ }
113+
114+ if ($ verbose ) {
115+ $ output ->writeln ('Deleting <info> ' . Util::humanFileSize ($ node ->getSize ()) . "</info> in trash for <info> {$ user ->getUID ()}</info>. " );
116+ }
117+ $ node ->delete ();
118+ if ($ this ->rootFolder ->nodeExists ($ path )) {
119+ $ output ->writeln ('<error>Trash folder sill exists after attempting to delete it</error> ' );
120+ return ;
116121 }
122+ $ query = $ this ->dbConnection ->getQueryBuilder ();
123+ $ query ->delete ('files_trash ' )
124+ ->where ($ query ->expr ()->eq ('user ' , $ query ->createParameter ('uid ' )))
125+ ->setParameter ('uid ' , $ user ->getUID ());
126+ $ query ->executeStatement ();
117127 }
118128}
0 commit comments