Debugging of fingerprint deletion and QR code repair script added

This commit is contained in:
Mal 2020-08-20 16:53:54 +02:00
parent f2957ab4ad
commit 0334c7022d
4 changed files with 70 additions and 6 deletions

View File

@ -1,3 +1,6 @@
repair:
php backend/scripts/repair.php
setup: setup:
php backend/scripts/setup.php php backend/scripts/setup.php

View File

@ -24,8 +24,15 @@ final class FingerprintDeleteController extends AbstractController
} }
try { try {
$fingerprint = new Fingerprint($this->fingerprintId); $db = new MySqlDatabase();
$db->startTransaction();
$fingerprint = new Fingerprint($this->fingerprintId, $db);
$qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint());
$fingerprint->Delete(); $fingerprint->Delete();
$qrCode->delete();
$db->commit();
$this->response = new ApiJsonResponse(); $this->response = new ApiJsonResponse();
} catch (Throwable $e) { } catch (Throwable $e) {

View File

@ -24,11 +24,7 @@ final class QrCode
$returnCode = 0; $returnCode = 0;
$path = substr(Setting::PATH_QR_CODES, -1) === '/' $filename = $this->getFilePath();
? Setting::PATH_QR_CODES
: Setting::PATH_QR_CODES . '/';
$filename = $path . $this->fingerprintId . '.svg';
passthru( passthru(
sprintf('mv %s %s', $this->temporaryFilename, $filename), sprintf('mv %s %s', $this->temporaryFilename, $filename),
@ -42,6 +38,19 @@ final class QrCode
} }
} }
public function delete(): void
{
$filepath = $this->getFilePath();
if (!is_file($filepath)) {
throw new QrCodeException(sprintf('Qr code file %s not found!', $filepath));
}
if (!unlink($filepath)) {
throw new QrCodeException('Couldn\'t delete %s!', $filepath);
}
}
public function generate(): bool public function generate(): bool
{ {
$returnCode = 0; $returnCode = 0;
@ -58,10 +67,24 @@ final class QrCode
return !(bool)$returnCode; return !(bool)$returnCode;
} }
public function hasFile(): bool
{
return is_file($this->getFilePath());
}
private function generateTemporaryFilename(): string private function generateTemporaryFilename(): string
{ {
$hash = hash('md5', (new DateTime())->format('U') . $this->fingerprint); $hash = hash('md5', (new DateTime())->format('U') . $this->fingerprint);
return sprintf('%s.svg', $hash); return sprintf('%s.svg', $hash);
} }
private function getFilePath(): string
{
$path = substr(Setting::PATH_QR_CODES, -1) === '/'
? Setting::PATH_QR_CODES
: Setting::PATH_QR_CODES . '/';
return $path . $this->fingerprintId . '.svg';
}
} }

View File

@ -0,0 +1,31 @@
<?php
require 'backend/classes/core/Autoloader.php';
$autoloader = new Autoloader();
echo 'Checking for missing qr codes...' . PHP_EOL;
$db = new MySqlDatabase();
$countForMissing = 0;
foreach ($db->Select(Fingerprint::class, [Fingerprint::FIELD_ID]) as $record) {
$fingerprint = new Fingerprint((int)$record[Fingerprint::FIELD_ID], $db);
$qrCode = new QrCode($fingerprint->getFingerprintId(), $fingerprint->getFingerprint());
if (!$qrCode->hasFile()) {
$countForMissing++;
$qrCode->generate();
$qrCode->save();
echo sprintf("\t=> Missing QR code for fingerprint %d generated.\n", $fingerprint->getFingerprintId());
}
}
echo $countForMissing === 0
? 'No missing QR codes found.' . PHP_EOL
: sprintf('%d missing QR code(s) fixed.', $countForMissing) . PHP_EOL;