Completed
Push — master ( 4ab573...bed94c )
by
unknown
31:03 queued 04:45
created
apps/files/lib/Command/SanitizeFilenames.php 2 patches
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -26,126 +26,126 @@
 block discarded – undo
26 26
 
27 27
 class SanitizeFilenames extends Base {
28 28
 
29
-	private OutputInterface $output;
30
-	private ?string $charReplacement;
31
-	private bool $dryRun;
32
-
33
-	public function __construct(
34
-		private IUserManager $userManager,
35
-		private IRootFolder $rootFolder,
36
-		private IUserSession $session,
37
-		private IFactory $l10nFactory,
38
-		private FilenameValidator $filenameValidator,
39
-	) {
40
-		parent::__construct();
41
-	}
42
-
43
-	protected function configure(): void {
44
-		parent::configure();
45
-
46
-		$this
47
-			->setName('files:sanitize-filenames')
48
-			->setDescription('Renames files to match naming constraints')
49
-			->addArgument(
50
-				'user_id',
51
-				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
52
-				'will only rename files the given user(s) have access to'
53
-			)
54
-			->addOption(
55
-				'dry-run',
56
-				mode: InputOption::VALUE_NONE,
57
-				description: 'Do not actually rename any files but just check filenames.',
58
-			)
59
-			->addOption(
60
-				'char-replacement',
61
-				'c',
62
-				mode: InputOption::VALUE_REQUIRED,
63
-				description: 'Replacement for invalid character (by default space, underscore or dash is used)',
64
-			);
29
+    private OutputInterface $output;
30
+    private ?string $charReplacement;
31
+    private bool $dryRun;
32
+
33
+    public function __construct(
34
+        private IUserManager $userManager,
35
+        private IRootFolder $rootFolder,
36
+        private IUserSession $session,
37
+        private IFactory $l10nFactory,
38
+        private FilenameValidator $filenameValidator,
39
+    ) {
40
+        parent::__construct();
41
+    }
42
+
43
+    protected function configure(): void {
44
+        parent::configure();
45
+
46
+        $this
47
+            ->setName('files:sanitize-filenames')
48
+            ->setDescription('Renames files to match naming constraints')
49
+            ->addArgument(
50
+                'user_id',
51
+                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
52
+                'will only rename files the given user(s) have access to'
53
+            )
54
+            ->addOption(
55
+                'dry-run',
56
+                mode: InputOption::VALUE_NONE,
57
+                description: 'Do not actually rename any files but just check filenames.',
58
+            )
59
+            ->addOption(
60
+                'char-replacement',
61
+                'c',
62
+                mode: InputOption::VALUE_REQUIRED,
63
+                description: 'Replacement for invalid character (by default space, underscore or dash is used)',
64
+            );
65 65
 			
66
-	}
67
-
68
-	protected function execute(InputInterface $input, OutputInterface $output): int {
69
-		$this->charReplacement = $input->getOption('char-replacement');
70
-		// check if replacement is needed
71
-		$c = $this->filenameValidator->getForbiddenCharacters();
72
-		if (count($c) > 0) {
73
-			try {
74
-				$this->filenameValidator->sanitizeFilename($c[0], $this->charReplacement);
75
-			} catch (\InvalidArgumentException) {
76
-				if ($this->charReplacement === null) {
77
-					$output->writeln('<error>Character replacement required</error>');
78
-				} else {
79
-					$output->writeln('<error>Invalid character replacement given</error>');
80
-				}
81
-				return 1;
82
-			}
83
-		}
84
-
85
-		$this->dryRun = $input->getOption('dry-run');
86
-		if ($this->dryRun) {
87
-			$output->writeln('<info>Dry run is enabled, no actual renaming will be applied.</>');
88
-		}
89
-
90
-		$this->output = $output;
91
-		$users = $input->getArgument('user_id');
92
-		if (!empty($users)) {
93
-			foreach ($users as $userId) {
94
-				$user = $this->userManager->get($userId);
95
-				if ($user === null) {
96
-					$output->writeln("<error>User '$userId' does not exist - skipping</>");
97
-					continue;
98
-				}
99
-				$this->sanitizeUserFiles($user);
100
-			}
101
-		} else {
102
-			$this->userManager->callForSeenUsers($this->sanitizeUserFiles(...));
103
-		}
104
-		return self::SUCCESS;
105
-	}
106
-
107
-	private function sanitizeUserFiles(IUser $user): void {
108
-		// Set an active user so that event listeners can correctly work (e.g. files versions)
109
-		$this->session->setVolatileActiveUser($user);
110
-
111
-		$this->output->writeln('<info>Analyzing files of ' . $user->getUID() . '</>');
112
-
113
-		$folder = $this->rootFolder->getUserFolder($user->getUID());
114
-		$this->sanitizeFiles($folder);
115
-	}
116
-
117
-	private function sanitizeFiles(Folder $folder): void {
118
-		foreach ($folder->getDirectoryListing() as $node) {
119
-			$this->output->writeln('scanning: ' . $node->getPath(), OutputInterface::VERBOSITY_VERBOSE);
120
-
121
-			try {
122
-				$oldName = $node->getName();
123
-				$newName = $this->filenameValidator->sanitizeFilename($oldName, $this->charReplacement);
124
-				if ($oldName !== $newName) {
125
-					$newName = $folder->getNonExistingName($newName);
126
-					$path = rtrim(dirname($node->getPath()), '/');
127
-
128
-					if (!$this->dryRun) {
129
-						$node->move("$path/$newName");
130
-					} elseif (!$folder->isCreatable()) {
131
-						// simulate error for dry run
132
-						throw new NotPermittedException();
133
-					}
134
-					$this->output->writeln('renamed: "' . $oldName . '" to "' . $newName . '"');
135
-				}
136
-			} catch (LockedException) {
137
-				$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (file is locked)</>');
138
-			} catch (NotPermittedException) {
139
-				$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (no permissions)</>');
140
-			} catch (Exception $error) {
141
-				$this->output->writeln('<error>failed: ' . $node->getPath() . '</>');
142
-				$this->output->writeln('<error>' . $error->getMessage() . '</>', OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_VERBOSE);
143
-			}
144
-
145
-			if ($node instanceof Folder) {
146
-				$this->sanitizeFiles($node);
147
-			}
148
-		}
149
-	}
66
+    }
67
+
68
+    protected function execute(InputInterface $input, OutputInterface $output): int {
69
+        $this->charReplacement = $input->getOption('char-replacement');
70
+        // check if replacement is needed
71
+        $c = $this->filenameValidator->getForbiddenCharacters();
72
+        if (count($c) > 0) {
73
+            try {
74
+                $this->filenameValidator->sanitizeFilename($c[0], $this->charReplacement);
75
+            } catch (\InvalidArgumentException) {
76
+                if ($this->charReplacement === null) {
77
+                    $output->writeln('<error>Character replacement required</error>');
78
+                } else {
79
+                    $output->writeln('<error>Invalid character replacement given</error>');
80
+                }
81
+                return 1;
82
+            }
83
+        }
84
+
85
+        $this->dryRun = $input->getOption('dry-run');
86
+        if ($this->dryRun) {
87
+            $output->writeln('<info>Dry run is enabled, no actual renaming will be applied.</>');
88
+        }
89
+
90
+        $this->output = $output;
91
+        $users = $input->getArgument('user_id');
92
+        if (!empty($users)) {
93
+            foreach ($users as $userId) {
94
+                $user = $this->userManager->get($userId);
95
+                if ($user === null) {
96
+                    $output->writeln("<error>User '$userId' does not exist - skipping</>");
97
+                    continue;
98
+                }
99
+                $this->sanitizeUserFiles($user);
100
+            }
101
+        } else {
102
+            $this->userManager->callForSeenUsers($this->sanitizeUserFiles(...));
103
+        }
104
+        return self::SUCCESS;
105
+    }
106
+
107
+    private function sanitizeUserFiles(IUser $user): void {
108
+        // Set an active user so that event listeners can correctly work (e.g. files versions)
109
+        $this->session->setVolatileActiveUser($user);
110
+
111
+        $this->output->writeln('<info>Analyzing files of ' . $user->getUID() . '</>');
112
+
113
+        $folder = $this->rootFolder->getUserFolder($user->getUID());
114
+        $this->sanitizeFiles($folder);
115
+    }
116
+
117
+    private function sanitizeFiles(Folder $folder): void {
118
+        foreach ($folder->getDirectoryListing() as $node) {
119
+            $this->output->writeln('scanning: ' . $node->getPath(), OutputInterface::VERBOSITY_VERBOSE);
120
+
121
+            try {
122
+                $oldName = $node->getName();
123
+                $newName = $this->filenameValidator->sanitizeFilename($oldName, $this->charReplacement);
124
+                if ($oldName !== $newName) {
125
+                    $newName = $folder->getNonExistingName($newName);
126
+                    $path = rtrim(dirname($node->getPath()), '/');
127
+
128
+                    if (!$this->dryRun) {
129
+                        $node->move("$path/$newName");
130
+                    } elseif (!$folder->isCreatable()) {
131
+                        // simulate error for dry run
132
+                        throw new NotPermittedException();
133
+                    }
134
+                    $this->output->writeln('renamed: "' . $oldName . '" to "' . $newName . '"');
135
+                }
136
+            } catch (LockedException) {
137
+                $this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (file is locked)</>');
138
+            } catch (NotPermittedException) {
139
+                $this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (no permissions)</>');
140
+            } catch (Exception $error) {
141
+                $this->output->writeln('<error>failed: ' . $node->getPath() . '</>');
142
+                $this->output->writeln('<error>' . $error->getMessage() . '</>', OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_VERBOSE);
143
+            }
144
+
145
+            if ($node instanceof Folder) {
146
+                $this->sanitizeFiles($node);
147
+            }
148
+        }
149
+    }
150 150
 
151 151
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
 		// Set an active user so that event listeners can correctly work (e.g. files versions)
109 109
 		$this->session->setVolatileActiveUser($user);
110 110
 
111
-		$this->output->writeln('<info>Analyzing files of ' . $user->getUID() . '</>');
111
+		$this->output->writeln('<info>Analyzing files of '.$user->getUID().'</>');
112 112
 
113 113
 		$folder = $this->rootFolder->getUserFolder($user->getUID());
114 114
 		$this->sanitizeFiles($folder);
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 
117 117
 	private function sanitizeFiles(Folder $folder): void {
118 118
 		foreach ($folder->getDirectoryListing() as $node) {
119
-			$this->output->writeln('scanning: ' . $node->getPath(), OutputInterface::VERBOSITY_VERBOSE);
119
+			$this->output->writeln('scanning: '.$node->getPath(), OutputInterface::VERBOSITY_VERBOSE);
120 120
 
121 121
 			try {
122 122
 				$oldName = $node->getName();
@@ -131,15 +131,15 @@  discard block
 block discarded – undo
131 131
 						// simulate error for dry run
132 132
 						throw new NotPermittedException();
133 133
 					}
134
-					$this->output->writeln('renamed: "' . $oldName . '" to "' . $newName . '"');
134
+					$this->output->writeln('renamed: "'.$oldName.'" to "'.$newName.'"');
135 135
 				}
136 136
 			} catch (LockedException) {
137
-				$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (file is locked)</>');
137
+				$this->output->writeln('<comment>skipping: '.$node->getPath().' (file is locked)</>');
138 138
 			} catch (NotPermittedException) {
139
-				$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (no permissions)</>');
139
+				$this->output->writeln('<comment>skipping: '.$node->getPath().' (no permissions)</>');
140 140
 			} catch (Exception $error) {
141
-				$this->output->writeln('<error>failed: ' . $node->getPath() . '</>');
142
-				$this->output->writeln('<error>' . $error->getMessage() . '</>', OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_VERBOSE);
141
+				$this->output->writeln('<error>failed: '.$node->getPath().'</>');
142
+				$this->output->writeln('<error>'.$error->getMessage().'</>', OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_VERBOSE);
143 143
 			}
144 144
 
145 145
 			if ($node instanceof Folder) {
Please login to merge, or discard this patch.