Skip to content

Commit

Permalink
Optimized directory error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mxgross committed Sep 24, 2022
1 parent 59f6b71 commit 2bbb715
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
63 changes: 45 additions & 18 deletions Controller/EasyBackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ public function __construct(string $dataDirectory, EasyBackupConfiguration $conf

private function log($logLevel, $message)
{
$logFile = $this->getBackupDirectory().self::LOG_FILE_NAME;
$backupDir = $this->getBackupDirectory();
$logFile = $backupDir.self::LOG_FILE_NAME;

try {
if (!file_exists($logFile)) {
$this->filesystem->touch($logFile);
}

$dateTime = date('Y-m-d H:i:s');
$this->filesystem->appendToFile($logFile, "[$dateTime] $logLevel: $message".PHP_EOL);

if (!file_exists($logFile)) {
$this->filesystem->touch($logFile);
} catch (\Exception $e) {
// Todo: log in generall log
// Todo: translate error message
$this->flashError("Failed to touch $logFile");
}

$dateTime = date('Y-m-d H:i:s');
$this->filesystem->appendToFile($logFile, "[$dateTime] $logLevel: $message".PHP_EOL);
}

private function getBackupDirectory(): string
Expand All @@ -96,10 +105,14 @@ private function getBackupDirectory(): string
*/
public function indexAction(): Response
{
$existingBackups = [];
$backupDir = $this->getBackupDirectory();

if (!file_exists($backupDir)) {
$this->filesystem->mkdir($backupDir);
}

$existingBackups = [];
$status = $this->checkStatus();
$backupDir = $this->getBackupDirectory();

if ($this->filesystem->exists($backupDir)) {
$files = scandir($backupDir, SCANDIR_SORT_DESCENDING);
Expand All @@ -114,9 +127,9 @@ public function indexAction(): Response
$existingBackups[$fileOrDir] = $filesizeInMb;
}
}
}
}

$logFile = $this->getBackupDirectory().self::LOG_FILE_NAME;
$logFile = $backupDir.self::LOG_FILE_NAME;
$log = file_exists($logFile) ? file_get_contents($logFile) : 'empty';

return $this->render('@EasyBackup/index.html.twig', [
Expand Down Expand Up @@ -592,13 +605,20 @@ private function checkStatus()
'result' => '',
];

$path = $this->kimaiRootPath.'var/easy_backup';
$path = $this->kimaiRootPath.'var';
$status[] = [
'desc' => "Path '$path' writable",
'status' => is_writable($path),
'result' => '',
];

$path = $this->getBackupDirectory();
$status[] = [
'desc' => "Backup directory '$path' exists",
'status' => is_writable($path),
'result' => '',
];

$status[] = [
'desc' => "PHP extension 'zip' loaded",
'status' => extension_loaded('zip'),
Expand All @@ -611,15 +631,22 @@ private function checkStatus()
'result' => $this->getKimaiVersion(),
];

$cmd = self::CMD_GIT_HEAD;
$cmdResArr = $this->execute($cmd);
$cmdRes = !empty($cmdResArr['err']) ? $cmdResArr['err'] : $cmdResArr['out'];
// Todo: build path via config files instead of manually
$dotGitPath = $this->kimaiRootPath . 'var' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'EasyBackupBundle' . DIRECTORY_SEPARATOR .'.git';

$status[] = [
'desc' => 'git',
'status' => empty($cmdResArr['err']),
'result' => $cmdRes,
];
if (!file_exists($dotGitPath)) {
$cmd = self::CMD_GIT_HEAD;
$cmdResArr = $this->execute($cmd);
$cmdRes = !empty($cmdResArr['err']) ? $cmdResArr['err'] : $cmdResArr['out'];

$status[] = [
'desc' => 'git',
'status' => empty($cmdResArr['err']),
'result' => $cmdRes,
];
} else {
$this->log(self::LOG_INFO_PREFIX, 'No git repository recognized. Expected path: ' . $dotGitPath);
}

// Check used database

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A Kimai 2 plugin, which allows you to backup your environment with a single click.",
"homepage": "https://github.com/mxgross/EasyBackupBundle",
"type": "kimai-plugin",
"version": "0.2",
"version": "1.1",
"require": {
"kimai/kimai2-composer": "*"
},
Expand All @@ -22,7 +22,7 @@
"extra": {
"kimai": {
"require": "1.6",
"version": "0.2",
"version": "1.1",
"name": "EasyBackup"
}
},
Expand Down

0 comments on commit 2bbb715

Please sign in to comment.