added relative_path functionality to DiffTaskService

This commit is contained in:
Jonathan Treffler 2024-01-23 22:12:02 +01:00 committed by root
parent c13e8dfcb2
commit 9280da6f0e

View file

@ -7,6 +7,7 @@ use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\GroupfolderFilesystemSnapshots\Exceptions\NotFoundException; use OCA\GroupfolderFilesystemSnapshots\Exceptions\NotFoundException;
use OCA\GroupfolderFilesystemSnapshots\Exceptions\InvalidRelativePathException;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTask; use OCA\GroupfolderFilesystemSnapshots\Db\DiffTask;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskMapper; use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskMapper;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResult; use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResult;
@ -14,112 +15,120 @@ use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResultMapper;
use OCA\GroupfolderFilesystemSnapshots\Manager\PathManager; use OCA\GroupfolderFilesystemSnapshots\Manager\PathManager;
use OCA\GroupfolderFilesystemSnapshots\RecursiveDiff; use OCA\GroupfolderFilesystemSnapshots\RecursiveDiff;
class DiffTaskService { class DiffTaskService {
private DiffTaskMapper $mapper; private DiffTaskMapper $mapper;
private DiffTaskResultMapper $diffTaskResultMapper; private DiffTaskResultMapper $diffTaskResultMapper;
private PathManager $pathManager; private PathManager $pathManager;
public function __construct(DiffTaskMapper $mapper, DiffTaskResultMapper $diffTaskResultMapper, PathManager $pathManager){ public function __construct(
$this->mapper = $mapper; DiffTaskMapper $mapper,
$this->diffTaskResultMapper = $diffTaskResultMapper; DiffTaskResultMapper $diffTaskResultMapper,
$this->pathManager = $pathManager; PathManager $pathManager,
} ){
$this->mapper = $mapper;
$this->diffTaskResultMapper = $diffTaskResultMapper;
$this->pathManager = $pathManager;
}
/** /**
* @return DiffTask[] * @return DiffTask[]
*/ */
public function findAll(string $userId): array { public function findAll(string $userId): array {
return $this->mapper->findAll($userId); return $this->mapper->findAll($userId);
} }
/** /**
* @return never * @return never
*/ */
private function handleException ($e, $criteria) { private function handleException ($e, $criteria) {
if ($e instanceof DoesNotExistException || if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) { $e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage(), $criteria); throw new NotFoundException($e->getMessage(), $criteria);
} else { } else {
throw $e; throw $e;
} }
} }
public function find(int $id, string $userId): DiffTask { public function find(int $id, string $userId): DiffTask {
try { try {
return $this->mapper->find($id, $userId); return $this->mapper->find($id, $userId);
} catch(Exception $e) { } catch(Exception $e) {
$this->handleException($e, ["userId" => $userId, "id" => $id]); $this->handleException($e, ["userId" => $userId, "id" => $id]);
} }
} }
function create(int $groupfolderId, string $snapshotId, string $userId, Callable $progressCallback = null): DiffTask { function create(string $relativePathInGroupfolder, int $groupfolderId, string $snapshotId, string $userId, Callable $progressCallback = null): ?DiffTask {
$snapshotPath = $this->pathManager->getGroupFolderSnapshotDirectory($groupfolderId, $snapshotId); $snapshotPath = $this->pathManager->getGroupFolderSnapshotDirectory($groupfolderId, $relativePathInGroupfolder, $snapshotId);
$groupfolderPath = $this->pathManager->getGroupFolderDirectory($groupfolderId); $groupfolderPath = $this->pathManager->getGroupFolderDirectory($groupfolderId, $relativePathInGroupfolder);
$newTask = new DiffTask(); if(!($snapshotPath && $groupfolderPath)) {
$newTask->setGroupfolderId($groupfolderId); throw new InvalidRelativePathException;
$newTask->setSnapshotId($snapshotId); }
$newTask->setTimestamp(time());
$newTask->setUserId($userId);
$task = $this->mapper->insert($newTask);
$numFiles = 0; $newTask = new DiffTask();
$newTask->setGroupfolderId($groupfolderId);
$newTask->setRelativePath($relativePathInGroupfolder);
$newTask->setSnapshotId($snapshotId);
$newTask->setTimestamp(time());
$newTask->setUserId($userId);
$task = $this->mapper->insert($newTask);
$diffTask = new RecursiveDiff( $numFiles = 0;
$snapshotPath,
$groupfolderPath,
"",
function(string $type, bool $beforeFileExists, ?string $beforePath, ?int $beforeSize, bool $currentFileExists, ?string $currentPath, ?int $currentSize) use ($task) {
$newResult = new DiffTaskResult();
$newResult->setTaskId($task->getId());
$newResult->setType($type);
$newResult->setBeforeFileExists($beforeFileExists);
$newResult->setBeforePath($beforePath);
$newResult->setBeforeSize($beforeSize);
$newResult->setCurrentFileExists($currentFileExists);
$newResult->setCurrentPath($currentPath);
$newResult->setCurrentSize($currentSize);
$newResult = $this->diffTaskResultMapper->insert($newResult);
},
function($numDoneFiles) use ($progressCallback, &$numFiles) {
if(isset($progressCallback) && ($numFiles != 0) && ($numFiles != $numDoneFiles)) {
($progressCallback)([
"overallFiles" => $numFiles,
"doneFiles" => $numDoneFiles,
"progress" => number_format(($numDoneFiles / $numFiles),2),
"progressPercent" => (number_format(($numDoneFiles / $numFiles),2) * 100) . "%",
]);
}
},
);
$numFiles = $diffTask->scan(); $diffTask = new RecursiveDiff(
$snapshotPath,
$groupfolderPath,
"",
function(string $type, bool $beforeFileExists, ?string $beforePath, ?int $beforeSize, bool $currentFileExists, ?string $currentPath, ?int $currentSize) use ($task) {
$newResult = new DiffTaskResult();
$newResult->setTaskId($task->getId());
$newResult->setType($type);
$newResult->setBeforeFileExists($beforeFileExists);
$newResult->setBeforePath($beforePath);
$newResult->setBeforeSize($beforeSize);
$newResult->setCurrentFileExists($currentFileExists);
$newResult->setCurrentPath($currentPath);
$newResult->setCurrentSize($currentSize);
$newResult = $this->diffTaskResultMapper->insert($newResult);
},
function($numDoneFiles) use ($progressCallback, &$numFiles) {
if(isset($progressCallback) && ($numFiles != 0) && ($numFiles != $numDoneFiles)) {
($progressCallback)([
"overallFiles" => $numFiles,
"doneFiles" => $numDoneFiles,
"progress" => number_format(($numDoneFiles / $numFiles),2),
"progressPercent" => (number_format(($numDoneFiles / $numFiles),2) * 100) . "%",
]);
}
},
);
$diffTask->diff(); $numFiles = $diffTask->scan();
if(isset($progressCallback)) { $diffTask->diff();
($progressCallback)([
"overallFiles" => $numFiles,
"doneFiles" => $numFiles,
"progress" => 1.0,
"progressPercent" => "100.00%",
"result" => $task,
]);
}
return $task; if(isset($progressCallback)) {
} ($progressCallback)([
"overallFiles" => $numFiles,
"doneFiles" => $numFiles,
"progress" => 1.0,
"progressPercent" => "100.00%",
"result" => $task,
]);
}
public function delete(int $id, string $userId): DiffTask { return $task;
try { }
$task = $this->mapper->find($id, $userId);
$this->mapper->delete($task); public function delete(int $id, string $userId): DiffTask {
return $task; try {
} catch(Exception $e) { $task = $this->mapper->find($id, $userId);
$this->handleException($e); $this->mapper->delete($task);
} return $task;
} } catch(Exception $e) {
$this->handleException($e);
}
}
} }