nextcloud_groupfolder_files.../lib/Manager/PathManager.php

78 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace OCA\GroupfolderFilesystemSnapshots\Manager;
use OCP\IConfig;
use OCP\Files\IRootFolder;
use OCA\GroupFolders\Folder\FolderManager;
class PathManager {
private IConfig $config;
private IRootFolder $rootFolder;
private FolderManager $groupfolderFolderManager;
const FILESYSTEM_ROOT_PATH = "/srv/nextcloud/files/";
const FILESYSTEM_SNAPSHOT_PATH = "/srv/nextcloud/files/.zfs/snapshot/";
public function __construct(IConfig $config, IRootFolder $rootFolder, FolderManager $manager){
$this->config = $config;
$this->groupfolderFolderManager = $manager;
$this->rootFolder = $rootFolder;
}
public function getFilesystemSnapshotPath() {
return self::FILESYSTEM_SNAPSHOT_PATH;
}
// Nextcloud general
public function getDataDirectory() {
return $this->config->getSystemValue("datadirectory");
}
private function getRootFolderStorageId(): ?int {
return $this->rootFolder->getMountPoint()->getNumericStorageId();
}
// Snapshots
public function getSnapshotPath(string $snapshotId) {
return realpath(self::FILESYSTEM_SNAPSHOT_PATH . DIRECTORY_SEPARATOR . $snapshotId . DIRECTORY_SEPARATOR);
}
public function convertToSnapshotPath(string $filesystemPath, string $snapshotId) {
$filesystemPath = realpath($filesystemPath);
if(!str_starts_with($filesystemPath, self::FILESYSTEM_ROOT_PATH)) {
return false;
}
return str_replace(self::FILESYSTEM_ROOT_PATH, $this->getSnapshotPath($snapshotId) . DIRECTORY_SEPARATOR, $filesystemPath);
}
// Groupfolders
private function checkIfGroupfolderExists(int $groupfolderId): bool {
$storageId = $this->getRootFolderStorageId();
if ($storageId === null) {
return "storage Id null";
}
$folder = $this->groupfolderFolderManager->getFolder($groupfolderId, $storageId);
if ($folder === false) {
return "Folder does not exist";
}
return true;
}
public function getGroupFolderDirectory(int $groupfolderId) {
$folderExistsCheck = $this->checkIfGroupfolderExists($groupfolderId);
if(!$folderExistsCheck) {
return $folderExistsCheck;
}
return realpath($this->getDataDirectory() . DIRECTORY_SEPARATOR . "__groupfolders" . DIRECTORY_SEPARATOR . $groupfolderId . DIRECTORY_SEPARATOR);
}
public function getGroupFolderSnapshotDirectory(int $groupfolderId, string $snapshotId) {
return $this->convertToSnapshotPath($this->getGroupFolderDirectory($groupfolderId), $snapshotId);
}
}