Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Export/Import Versions #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/Command/ExportUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\DataExporter\Command;

use OCA\DataExporter\Exporter;
use OCA\DataExporter\FSAccess\FSAccessFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -33,9 +34,13 @@ class ExportUser extends Command {
/** @var Exporter */
private $exporter;

public function __construct(Exporter $importer) {
/** @var FSAccessFactory */
private $fsAccessFactory;

public function __construct(Exporter $importer, FSAccessFactory $fsAccessFactory) {
parent::__construct();
$this->exporter = $importer;
$this->fsAccessFactory = $fsAccessFactory;
}

protected function configure() {
Expand All @@ -46,11 +51,19 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
/** @var string $userId */
$userId = $input->getArgument('userId');
/** @var string $targetDir */
$targetDir = $input->getArgument('exportDirectory');

$fsAccessExportingDir = $this->fsAccessFactory->getFSAccess($targetDir);
if (!$fsAccessExportingDir->fileExists($userId)) {
$fsAccessExportingDir->mkdir($userId);
}

$fsAccess = $this->fsAccessFactory->getFSAccess("$targetDir/$userId");
try {
$this->exporter->export(
$input->getArgument('userId'),
$input->getArgument('exportDirectory')
);
$this->exporter->export($userId, $fsAccess);
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
}
Expand Down
14 changes: 9 additions & 5 deletions lib/Command/ImportUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\DataExporter\Command;

use OCA\DataExporter\Importer;
use OCA\DataExporter\FSAccess\FSAccessFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -34,9 +35,13 @@ class ImportUser extends Command {
/** @var Importer */
private $importer;

public function __construct(Importer $importer) {
/** @var FSAccessFactory */
private $fsAccessFactory;

public function __construct(Importer $importer, FSAccessFactory $fsAccessFactory) {
parent::__construct();
$this->importer = $importer;
$this->fsAccessFactory = $fsAccessFactory;
}

protected function configure() {
Expand All @@ -47,11 +52,10 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
$targetDir = $input->getArgument('exportDirectory');
$fsAccess = $this->fsAccessFactory->getFSAccess($targetDir);
try {
$this->importer->import(
$input->getArgument('exportDirectory'),
$input->getOption('as')
);
$this->importer->import($fsAccess, $input->getOption('as'));
} catch (\Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
}
Expand Down
21 changes: 7 additions & 14 deletions lib/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use OCA\DataExporter\Exporter\FilesExporter;
use OCA\DataExporter\Exporter\MetadataExtractor;
use Symfony\Component\Filesystem\Filesystem;
use OCA\DataExporter\FSAccess\FSAccess;

class Exporter {

Expand All @@ -34,25 +34,18 @@ class Exporter {
private $metadataExtractor;
/** @var FilesExporter */
private $filesExporter;
/** @var Filesystem */
private $filesystem;

public function __construct(Serializer $serializer, MetadataExtractor $metadataExtractor, FilesExporter $filesExporter, Filesystem $filesystem) {
public function __construct(Serializer $serializer, MetadataExtractor $metadataExtractor, FilesExporter $filesExporter) {
$this->serializer = $serializer;
$this->metadataExtractor = $metadataExtractor;
$this->filesExporter = $filesExporter;
$this->filesystem = $filesystem;
}

public function export(string $uid, string $exportDirectoryPath) {
$exportPath = "$exportDirectoryPath/$uid";
$metaData = $this->metadataExtractor->extract($uid);
$this->filesystem->dumpFile(
"$exportPath/metadata.json",
$this->serializer->serialize($metaData)
);
public function export(string $uid, FSAccess $fsAccessBase) {
$metaData = $this->metadataExtractor->extract($uid, $fsAccessBase);

$filesPath = \ltrim("$exportPath/files");
$this->filesExporter->export($uid, $filesPath);
$fsAccessBase->copyContentToPath($this->serializer->serialize($metaData), '/metadata.json');

$this->filesExporter->export($uid, $fsAccessBase);
}
}
20 changes: 9 additions & 11 deletions lib/Exporter/FilesExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,42 @@
namespace OCA\DataExporter\Exporter;

use OCA\DataExporter\Utilities\Iterators\Nodes\RecursiveNodeIteratorFactory;
use OCA\DataExporter\FSAccess\FSAccess;
use OCP\Files\File;
use OCP\Files\Folder;
use Symfony\Component\Filesystem\Filesystem;

class FilesExporter {
/** @var RecursiveNodeIteratorFactory */
private $iteratorFactory;

/** @var Filesystem */
private $filesystem;

public function __construct(RecursiveNodeIteratorFactory $iteratorFactory, Filesystem $filesystem) {
public function __construct(RecursiveNodeIteratorFactory $iteratorFactory) {
$this->iteratorFactory = $iteratorFactory;
$this->filesystem = $filesystem;
}

/**
* @param string $userId
* @param string $exportPath
* @param FSAccess $fsAccess
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
*/
public function export(string $userId, string $exportPath) {
public function export(string $userId, FSAccess $fsAccess) {
list($iterator, $baseFolder) = $this->iteratorFactory->getUserFolderParentRecursiveIterator($userId);
/** @var \OCP\Files\Node $node */
foreach ($iterator as $node) {
$nodePath = $node->getPath();
$relativeFileCachePath = $baseFolder->getRelativePath($nodePath);
// $relativeFileCachePath is expected to have a leading slash always
$path = "${exportPath}${relativeFileCachePath}";
$path = "/files${relativeFileCachePath}";

if ($node instanceof File) {
$this->filesystem->dumpFile($path, $node->getContent());
$stream = $node->fopen('rb');
$fsAccess->copyStreamToPath($stream, $path);
\fclose($stream);
continue;
}

if ($node instanceof Folder) {
$this->filesystem->mkdir($path);
$fsAccess->mkdir($path);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Exporter/MetadataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
use OCA\DataExporter\Exporter\MetadataExtractor\PreferencesExtractor;
use OCA\DataExporter\Exporter\MetadataExtractor\UserExtractor;
use OCA\DataExporter\Exporter\MetadataExtractor\SharesExtractor;
use OCA\DataExporter\Model\Metadata;
use OCA\DataExporter\Model\UserMetadata;
use OCA\DataExporter\FSAccess\FSAccess;
use OCP\IURLGenerator;

/**
Expand Down Expand Up @@ -76,21 +77,21 @@ public function __construct(
* Extract all metadata required for export in to the database
*
* @param string $uid
* @return Metadata
* @return UserMetadata
* @throws \Exception
* @throws \RuntimeException if user can not be read
*/
public function extract(string $uid) : Metadata {
public function extract(string $uid, FSAccess $fsAccess) : UserMetadata {
$user = $this->userExtractor->extract($uid);
$user->setPreferences(
$this->preferencesExtractor->extract($uid)
)->setFiles(
$this->filesExtractor->extract($uid)
$this->filesExtractor->extract($uid, $fsAccess)
)->setShares(
$this->sharesExtractor->extract($uid)
);

$metadata = new Metadata();
$metadata = new UserMetadata();
$metadata->setDate(new \DateTimeImmutable())
->setUser($user)
->setOriginServer($this->urlGenerator->getAbsoluteURL('/'));
Expand Down
13 changes: 10 additions & 3 deletions lib/Exporter/MetadataExtractor/FilesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
namespace OCA\DataExporter\Exporter\MetadataExtractor;

use OCA\DataExporter\Utilities\Iterators\Nodes\RecursiveNodeIteratorFactory;
use OCA\DataExporter\Model\User\File;
use OCA\DataExporter\Model\UserMetadata\User\File;
use OCA\DataExporter\Exporter\MetadataExtractor\VersionsExtractor;
use OCA\DataExporter\FSAccess\FSAccess;
use OCP\Files\Node;

class FilesExtractor {
/** @var RecursiveNodeIteratorFactory */
private $iteratorFactory;

public function __construct(RecursiveNodeIteratorFactory $iteratorFactory) {
/** @var VersionsExtractor */
private $versionsExtractor;

public function __construct(RecursiveNodeIteratorFactory $iteratorFactory, VersionsExtractor $versionsExtractor) {
$this->iteratorFactory = $iteratorFactory;
$this->versionsExtractor = $versionsExtractor;
}

/**
Expand All @@ -41,7 +47,7 @@ public function __construct(RecursiveNodeIteratorFactory $iteratorFactory) {
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
*/
public function extract(string $userId) : array {
public function extract(string $userId, FSAccess $fsAccess) : array {
list($iterator, $baseFolder) = $this->iteratorFactory->getUserFolderParentRecursiveIterator($userId);
$files = [];

Expand All @@ -60,6 +66,7 @@ public function extract(string $userId) : array {
} else {
$file->setType(File::TYPE_FOLDER);
}
$file->setVersions($this->versionsExtractor->extract($userId, $node->getPath(), $fsAccess));

$files[] = $file;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Exporter/MetadataExtractor/PreferencesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
namespace OCA\DataExporter\Exporter\MetadataExtractor;

use OCA\DataExporter\Model\User\Preference;
use OCA\DataExporter\Model\UserMetadata\User\Preference;
use OCP\IAppConfig;
use OCP\IConfig;

Expand Down
2 changes: 1 addition & 1 deletion lib/Exporter/MetadataExtractor/SharesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
namespace OCA\DataExporter\Exporter\MetadataExtractor;

use OCA\DataExporter\Model\User\Share;
use OCA\DataExporter\Model\UserMetadata\User\Share;
use OCP\Share\IManager;
use OCP\Share as ShareConstants;
use OCP\Files\IRootFolder;
Expand Down
2 changes: 1 addition & 1 deletion lib/Exporter/MetadataExtractor/UserExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
namespace OCA\DataExporter\Exporter\MetadataExtractor;

use OCA\DataExporter\Model\User;
use OCA\DataExporter\Model\UserMetadata\User;
use OCP\IGroupManager;
use OCP\IUserManager;

Expand Down
88 changes: 88 additions & 0 deletions lib/Exporter/MetadataExtractor/VersionsExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* @author Juan Pablo Villafáñez <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license GPL-2.0
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
namespace OCA\DataExporter\Exporter\MetadataExtractor;

use OCA\DataExporter\Model\UserMetadata\User\File\Version;
use OCA\DataExporter\FSAccess\FSAccess;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IVersionedStorage;
use OCP\Files\Storage\IStorage;

class VersionsExtractor {
/** @var IRootFolder */
private $rootFolder;

public function __construct(IRootFolder $rootFolder) {
$this->rootFolder = $rootFolder;
}

public function extract(string $userId, string $path, FSAccess $fsAccess) {
$fileNode = $this->rootFolder->get($path);
if (!$fileNode instanceof File) {
// only files will have versions
return [];
}

$storage = $this->getStorage($fileNode);
$internalPath = $fileNode->getInternalPath();
$versionModels = [];

if ($storage !== null) {
$versions = $storage->getVersions($internalPath);
// traverse the version list backwards so older versions are first
for (\end($versions); \key($versions) !== null; \prev($versions)) {
$fileVersion = \current($versions);
if ($fileVersion['path'][0] !== '/') {
$versionPath = "/versions/{$fileVersion['path']}.{$fileVersion['version']}";
} else {
$versionPath = "/versions{$fileVersion['path']}.{$fileVersion['version']}";
}
$versionModel = new Version();
$versionModel->setPath($versionPath);

// copy the version over
$versionContentStream = $storage->getContentOfVersion($internalPath, $fileVersion['version']);
$fsAccess->copyStreamToPath($versionContentStream, "/files${versionPath}");
\fclose($versionContentStream);

$versionModels[] = $versionModel;
}
}
return $versionModels;
}

/**
* @param File $fileNode the target file node to fetch the storage from
* @return IStorage|IVersionedStorage|null the versioned storage or null if the storage is of
* a different type
*/
private function getStorage(File $fileNode) {
/** @var IStorage|IVersionedStorage $storage */
$storage = $fileNode->getStorage();
if (!$storage->instanceOfStorage(IVersionedStorage::class)) {
return null;
}
return $storage;
}
}
Loading