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 OCA\GroupfolderFilesystemSnapshots\Exceptions\NotFoundException;
use OCA\GroupfolderFilesystemSnapshots\Exceptions\InvalidRelativePathException;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTask;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskMapper;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResult;
@ -14,112 +15,120 @@ use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResultMapper;
use OCA\GroupfolderFilesystemSnapshots\Manager\PathManager;
use OCA\GroupfolderFilesystemSnapshots\RecursiveDiff;
class DiffTaskService {
private DiffTaskMapper $mapper;
private DiffTaskResultMapper $diffTaskResultMapper;
private PathManager $pathManager;
private DiffTaskMapper $mapper;
private DiffTaskResultMapper $diffTaskResultMapper;
private PathManager $pathManager;
public function __construct(DiffTaskMapper $mapper, DiffTaskResultMapper $diffTaskResultMapper, PathManager $pathManager){
$this->mapper = $mapper;
$this->diffTaskResultMapper = $diffTaskResultMapper;
$this->pathManager = $pathManager;
}
public function __construct(
DiffTaskMapper $mapper,
DiffTaskResultMapper $diffTaskResultMapper,
PathManager $pathManager,
){
$this->mapper = $mapper;
$this->diffTaskResultMapper = $diffTaskResultMapper;
$this->pathManager = $pathManager;
}
/**
* @return DiffTask[]
*/
public function findAll(string $userId): array {
return $this->mapper->findAll($userId);
}
/**
* @return DiffTask[]
*/
public function findAll(string $userId): array {
return $this->mapper->findAll($userId);
}
/**
* @return never
*/
private function handleException ($e, $criteria) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage(), $criteria);
} else {
throw $e;
}
}
/**
* @return never
*/
private function handleException ($e, $criteria) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage(), $criteria);
} else {
throw $e;
}
}
public function find(int $id, string $userId): DiffTask {
try {
return $this->mapper->find($id, $userId);
} catch(Exception $e) {
$this->handleException($e, ["userId" => $userId, "id" => $id]);
}
}
public function find(int $id, string $userId): DiffTask {
try {
return $this->mapper->find($id, $userId);
} catch(Exception $e) {
$this->handleException($e, ["userId" => $userId, "id" => $id]);
}
}
function create(int $groupfolderId, string $snapshotId, string $userId, Callable $progressCallback = null): DiffTask {
$snapshotPath = $this->pathManager->getGroupFolderSnapshotDirectory($groupfolderId, $snapshotId);
$groupfolderPath = $this->pathManager->getGroupFolderDirectory($groupfolderId);
function create(string $relativePathInGroupfolder, int $groupfolderId, string $snapshotId, string $userId, Callable $progressCallback = null): ?DiffTask {
$snapshotPath = $this->pathManager->getGroupFolderSnapshotDirectory($groupfolderId, $relativePathInGroupfolder, $snapshotId);
$groupfolderPath = $this->pathManager->getGroupFolderDirectory($groupfolderId, $relativePathInGroupfolder);
$newTask = new DiffTask();
$newTask->setGroupfolderId($groupfolderId);
$newTask->setSnapshotId($snapshotId);
$newTask->setTimestamp(time());
$newTask->setUserId($userId);
$task = $this->mapper->insert($newTask);
if(!($snapshotPath && $groupfolderPath)) {
throw new InvalidRelativePathException;
}
$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(
$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 = 0;
$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)) {
($progressCallback)([
"overallFiles" => $numFiles,
"doneFiles" => $numFiles,
"progress" => 1.0,
"progressPercent" => "100.00%",
"result" => $task,
]);
}
$diffTask->diff();
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 {
try {
$task = $this->mapper->find($id, $userId);
$this->mapper->delete($task);
return $task;
} catch(Exception $e) {
$this->handleException($e);
}
}
return $task;
}
public function delete(int $id, string $userId): DiffTask {
try {
$task = $this->mapper->find($id, $userId);
$this->mapper->delete($task);
return $task;
} catch(Exception $e) {
$this->handleException($e);
}
}
}