From aa71570ea41894e3a26bf1fedb5a057b4d56f617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Feb 2022 14:41:59 +0100 Subject: [PATCH] Use OCP\Files\Folder instead of View MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/ExportDestination.php | 27 +++++++++++----------- lib/IExportDestination.php | 10 +++++--- lib/IImportSource.php | 8 +++---- lib/ImportSource.php | 34 ++++------------------------ lib/Service/UserMigrationService.php | 24 ++------------------ 5 files changed, 30 insertions(+), 73 deletions(-) diff --git a/lib/ExportDestination.php b/lib/ExportDestination.php index 28c91ff0..5b19fe38 100644 --- a/lib/ExportDestination.php +++ b/lib/ExportDestination.php @@ -26,7 +26,8 @@ namespace OCA\UserMigration; -use OC\Files\View; +use OCP\Files\File; +use OCP\Files\Folder; use OCP\ITempManager; use ZipStreamer\COMPR; use ZipStreamer\ZipStreamer; @@ -63,20 +64,18 @@ public function addFile(string $path, string $content): bool { /** * {@inheritDoc} */ - public function copyFromView(View $view, string $sourcePath, string $destinationPath): bool { + public function copyFolder(Folder $folder, string $destinationPath): bool { $this->streamer->addEmptyDir($destinationPath); - $files = $view->getDirectoryContent($sourcePath); - foreach ($files as $f) { - switch ($f->getType()) { - case \OCP\Files\FileInfo::TYPE_FILE: - $read = $view->fopen($f->getPath(), 'rb'); - $this->streamer->addFileFromStream($read, $destinationPath.'/'.$f->getName()); - break; - case \OCP\Files\FileInfo::TYPE_FOLDER: - if ($this->copyFromView($view, $sourcePath.'/'.$f->getName(), $destinationPath.'/'.$f->getName()) === false) { - return false; - } - break; + $nodes = $folder->getDirectoryListing(); + foreach ($nodes as $node) { + if ($node instanceof File) { + $read = $node->fopen('rb'); + $this->streamer->addFileFromStream($read, $destinationPath.'/'.$node->getName()); + } else { + $success = $this->copyFolder($node, $destinationPath.'/'.$node->getName()); + if ($success === false) { + return false; + } } } return true; diff --git a/lib/IExportDestination.php b/lib/IExportDestination.php index 655a9548..74912ec4 100644 --- a/lib/IExportDestination.php +++ b/lib/IExportDestination.php @@ -26,7 +26,7 @@ namespace OCA\UserMigration; -use OC\Files\View; +use OCP\Files\Folder; interface IExportDestination { /** @@ -39,9 +39,13 @@ interface IExportDestination { public function addFile(string $path, string $content): bool; /** - * Copy files from View + * Copy a folder to the export + * + * @param Folder $folder folder to copy to the export archive. + * @param string $destinationPath Full path to the folder in the export archive. Parent directories will be created if needed. + * @return bool whether the folder was successfully added. */ - public function copyFromView(View $view, string $sourcePath, string $destinationPath): bool; + public function copyFolder(Folder $folder, string $destinationPath): bool; /** * Called after export is complete diff --git a/lib/IImportSource.php b/lib/IImportSource.php index 3eaf8a1b..df43144c 100644 --- a/lib/IImportSource.php +++ b/lib/IImportSource.php @@ -26,7 +26,7 @@ namespace OCA\UserMigration; -use OC\Files\View; +use OCP\Files\Folder; interface IImportSource { /** @@ -46,12 +46,12 @@ public function getFileContents(string $path): string; public function getFileAsStream(string $path); /** - * Copy files from the export to View + * Copy files from the export to a Folder * + * Folder $destination folder to copy into * string $sourcePath path in the export archive - * string $destinationPath path to copy to in the view */ - public function copyToView(View $view, string $sourcePath, string $destinationPath): bool; + public function copyToFolder(Folder $destination, string $sourcePath): bool; /** * Called after import is complete diff --git a/lib/ImportSource.php b/lib/ImportSource.php index 3e531f32..7c16c1ec 100644 --- a/lib/ImportSource.php +++ b/lib/ImportSource.php @@ -28,7 +28,7 @@ use OC\Archive\Archive; use OC\Archive\ZIP; -use OC\Files\View; +use OCP\Files\Folder; class ImportSource implements IImportSource { private Archive $archive; @@ -58,40 +58,14 @@ public function getFileAsStream(string $path) { /** * {@inheritDoc} */ - public function copyToView(View $view, string $sourcePath, string $destinationPath): bool { + public function copyToFolder(Folder $destination, string $sourcePath): bool { // TODO at the very least log errors $sourcePath = rtrim($sourcePath, '/').'/'; - $destinationPath = rtrim($destinationPath, '/'); $files = $this->archive->getFolder($sourcePath); - $folderPath = $destinationPath; - $toCreate = []; - while (!empty($folderPath) && !$view->file_exists($folderPath)) { - $toCreate[] = $folderPath; - $lastSlash = strrpos($folderPath, '/'); - if ($lastSlash !== false) { - $folderPath = substr($folderPath, 0, $lastSlash); - } else { - $folderPath = ''; - } - } - - if (!empty($folderPath) && $view->is_file($folderPath)) { - return false; - } - - $toCreate = array_reverse($toCreate); - foreach ($toCreate as $currentPath) { - if ($view->mkdir($currentPath) === false) { - return false; - } - } - - $destinationPath .= '/'; - foreach ($files as $path) { if (str_ends_with($path, '/')) { - if ($this->copyToView($view, $sourcePath.$path, $destinationPath.$path) === false) { + if ($this->copyToFolder($destination->newFolder($path), $sourcePath.$path) === false) { return false; } } else { @@ -99,7 +73,7 @@ public function copyToView(View $view, string $sourcePath, string $destinationPa if ($stream === false) { return false; } - if ($view->file_put_contents($destinationPath.$path, $stream) === false) { + if ($destination->newFile($destinationPath.$path, $stream) === false) { return false; } } diff --git a/lib/Service/UserMigrationService.php b/lib/Service/UserMigrationService.php index 3bea017c..19a8fd84 100644 --- a/lib/Service/UserMigrationService.php +++ b/lib/Service/UserMigrationService.php @@ -33,7 +33,6 @@ use OCA\UserMigration\ImportSource; use OC\Files\AppData; use OC\Files\Filesystem; -use OC\Files\View; use OCP\Accounts\IAccountManager; use OCP\Files\IRootFolder; use OCP\IConfig; @@ -87,43 +86,36 @@ public function export(IUser $user, ?OutputInterface $output = null): string { \OC::$server->getUserFolder($uid); Filesystem::initMountPoints($uid); - $view = new View(); - $exportDestination = new ExportDestination($this->tempManager, $uid); // copy the files $this->exportFiles( $uid, $exportDestination, - $view, $output ); $this->exportUserInformation( $user, $exportDestination, - $view, $output ); $this->exportAccountInformation( $user, $exportDestination, - $view, $output ); $this->exportAppsSettings( $uid, $exportDestination, - $view, $output ); $this->exportVersions( $uid, $exportDestination, - $view, $output ); @@ -157,11 +149,10 @@ public function import(string $path, ?OutputInterface $output = null): void { */ protected function exportFiles(string $uid, IExportDestination $exportDestination, - View $view, OutputInterface $output): void { $output->writeln("Copying files…"); - if ($exportDestination->copyFromView($view, "$uid/files", "files") === false) { + if ($exportDestination->copyFolder($this->root->getUserFolder($uid), "files") === false) { throw new UserMigrationException("Could not copy files."); } // TODO files metadata should be exported as well if relevant. Maybe move this to an export operation @@ -177,14 +168,7 @@ protected function importFiles(IUser $user, $uid = $user->getUID(); - // setup filesystem - // Requesting the user folder will set it up if the user hasn't logged in before - \OC::$server->getUserFolder($uid); - Filesystem::initMountPoints($uid); - - $view = new View(); - - if ($importSource->copyToView($view, "files", "$uid/files") === false) { + if ($importSource->copyToFolder($this->root->getUserFolder($uid), "files") === false) { throw new UserMigrationException("Could not import files."); } } @@ -194,7 +178,6 @@ protected function importFiles(IUser $user, */ protected function exportUserInformation(IUser $user, IExportDestination $exportDestination, - View $view, OutputInterface $output): void { $output->writeln("Exporting user information in user.json…"); @@ -238,7 +221,6 @@ protected function importUser(IImportSource $importSource, */ protected function exportAccountInformation(IUser $user, IExportDestination $exportDestination, - View $view, OutputInterface $output): void { $output->writeln("Exporting account information in account.json…"); @@ -263,7 +245,6 @@ protected function importAccountInformation(IUser $user, */ protected function exportVersions(string $uid, IExportDestination $exportDestination, - View $view, OutputInterface $output): void { $output->writeln("Exporting versions in versions.json…"); @@ -282,7 +263,6 @@ protected function exportVersions(string $uid, */ protected function exportAppsSettings(string $uid, IExportDestination $exportDestination, - View $view, OutputInterface $output): void { $output->writeln("Exporting settings in settings.json…");