implemented snapshot name parsing
This commit is contained in:
parent
a024d93e02
commit
85d63d5ae2
8 changed files with 175 additions and 36 deletions
|
@ -2,16 +2,21 @@
|
|||
|
||||
namespace OCA\GroupfolderFilesystemSnapshots\Manager;
|
||||
|
||||
use OCA\GroupfolderFilesystemSnapshots\Manager\PathManager;
|
||||
use OCP\IL10N;
|
||||
|
||||
use OCA\GroupfolderFilesystemSnapshots\Manager\PathManager;
|
||||
use OCA\GroupfolderFilesystemSnapshots\Service\SettingsService;
|
||||
use OCA\GroupfolderFilesystemSnapshots\Entity\Snapshot;
|
||||
|
||||
class SnapshotManager {
|
||||
private PathManager $pathManager;
|
||||
private string $snapshotNamingScheme = "";
|
||||
|
||||
|
||||
public function __construct(PathManager $pathManager){
|
||||
$this->pathManager = $pathManager;
|
||||
public function __construct(
|
||||
protected readonly IL10N $l10n,
|
||||
private readonly PathManager $pathManager,
|
||||
private readonly SettingsService $settingsService,
|
||||
){
|
||||
$this->snapshotNamingScheme = $this->settingsService->getAppValue("snapshot_naming_scheme");
|
||||
}
|
||||
|
||||
private function validSnapshotId(string $snapshotId) {
|
||||
|
@ -27,20 +32,94 @@ class SnapshotManager {
|
|||
}
|
||||
}
|
||||
|
||||
private function createSnapshotEntity(string $id): Snapshot {
|
||||
if ($this->snapshotNamingScheme === "zfs-auto-snapshot" && str_starts_with($id, "zfs-auto-snap")) {
|
||||
if (str_starts_with($id, "zfs-auto-snap_hourly-")) {
|
||||
$name = $this->l10n->t("Automated hourly backup");
|
||||
$datetimestring = str_replace("zfs-auto-snap_hourly-", "", $id);
|
||||
} elseif (str_starts_with($id, "zfs-auto-snap_daily-")) {
|
||||
$name = $this->l10n->t("Automated daily backup");
|
||||
$datetimestring = str_replace("zfs-auto-snap_daily-", "", $id);
|
||||
} elseif (str_starts_with($id, "zfs-auto-snap_weekly-")) {
|
||||
$name = $this->l10n->t("Automated weekly backup");
|
||||
$datetimestring = str_replace("zfs-auto-snap_weekly-", "", $id);
|
||||
} elseif (str_starts_with($id, "zfs-auto-snap_monthly-")) {
|
||||
$name = $this->l10n->t("Automated monthly backup");
|
||||
$datetimestring = str_replace("zfs-auto-snap_monthly-", "", $id);
|
||||
}
|
||||
|
||||
if(isset($datetimestring)) {
|
||||
$datetimearray = explode("-", $datetimestring);
|
||||
$timestring = array_pop($datetimearray);
|
||||
|
||||
$year = (int)$datetimearray[0];
|
||||
$month = (int)$datetimearray[1];
|
||||
$day = (int)$datetimearray[2];
|
||||
$hour = (int)substr($timestring, 0, 2);
|
||||
$minute = (int)substr($timestring, 2, 2);
|
||||
|
||||
$createdTimestamp = (new \DateTimeImmutable())
|
||||
->setDate($year, $month, $day)
|
||||
->setTime($hour, $minute);
|
||||
}
|
||||
|
||||
return new Snapshot(
|
||||
id: $id,
|
||||
name: $name ?: $id,
|
||||
createdTimestamp: $createdTimestamp,
|
||||
);
|
||||
} else {
|
||||
return new Snapshot(
|
||||
id: $id,
|
||||
name: $id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function get(string $snapshotId) {
|
||||
if(self::snapshotExists($snapshotId)) {
|
||||
return new Snapshot($snapshotId);
|
||||
return $this->createSnapshotEntity($snapshotId);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getAll() {
|
||||
function getAll(): array {
|
||||
$snapshots = [];
|
||||
|
||||
$iterator = new \FilesystemIterator($this->pathManager->getFilesystemSnapshotsPath());
|
||||
foreach ($iterator as $fileinfo) {
|
||||
if(!$fileinfo->isDir()) continue;
|
||||
yield new Snapshot($fileinfo->getFilename());
|
||||
}
|
||||
|
||||
foreach ($iterator as $fileinfo) {
|
||||
if(!$fileinfo->isDir()) continue;
|
||||
$snapshots[] = $this->createSnapshotEntity($fileinfo->getFilename());
|
||||
}
|
||||
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
function getAllGenerator(){
|
||||
$iterator = new \FilesystemIterator($this->pathManager->getFilesystemSnapshotsPath());
|
||||
|
||||
foreach ($iterator as $fileinfo) {
|
||||
if(!$fileinfo->isDir()) continue;
|
||||
yield $this->createSnapshotEntity($fileinfo->getFilename());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @var $subPathFilter Only return snapshots that have this subfolder in the specified groupfolder
|
||||
*/
|
||||
function getFilteredGenerator(int $groupfolderId, string $subDirectoryFilter) {
|
||||
$iterator = new \FilesystemIterator($this->pathManager->getFilesystemSnapshotsPath());
|
||||
|
||||
$groupfolderSubdirectoryPath = $this->pathManager->getGroupFolderDirectory($groupfolderId, $subDirectoryFilter);
|
||||
|
||||
foreach ($iterator as $fileinfo) {
|
||||
if(!$fileinfo->isDir()) continue;
|
||||
$snapshotId = $fileinfo->getFilename();
|
||||
$filterFullPath = $this->pathManager->convertToSnapshotPath($groupfolderSubdirectoryPath, $snapshotId);
|
||||
if(!(is_dir($filterFullPath))) continue;
|
||||
yield $this->createSnapshotEntity($snapshotId);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue