Skip to content

Commit

Permalink
Use OCP\Files\Folder instead of View
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Feb 1, 2022
1 parent b0748d0 commit aa71570
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 73 deletions.
27 changes: 13 additions & 14 deletions lib/ExportDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions lib/IExportDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace OCA\UserMigration;

use OC\Files\View;
use OCP\Files\Folder;

interface IExportDestination {
/**
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/IImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace OCA\UserMigration;

use OC\Files\View;
use OCP\Files\Folder;

interface IImportSource {
/**
Expand All @@ -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
Expand Down
34 changes: 4 additions & 30 deletions lib/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,48 +58,22 @@ 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 {
$stream = $this->archive->getStream($sourcePath.$path, 'r');
if ($stream === false) {
return false;
}
if ($view->file_put_contents($destinationPath.$path, $stream) === false) {
if ($destination->newFile($destinationPath.$path, $stream) === false) {
return false;
}
}
Expand Down
24 changes: 2 additions & 22 deletions lib/Service/UserMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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
Expand All @@ -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.");
}
}
Expand All @@ -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…");

Expand Down Expand Up @@ -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…");

Expand All @@ -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…");

Expand All @@ -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…");

Expand Down

0 comments on commit aa71570

Please sign in to comment.