From 3835897a8882846b60bdeae34fc807791a1740bb Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Fri, 10 Jan 2025 12:15:31 +0600 Subject: [PATCH] minor --- app/Models/StudyReport.php | 2 +- app/Services/ReportStorage.php | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/Models/StudyReport.php b/app/Models/StudyReport.php index b973474..1667db4 100644 --- a/app/Models/StudyReport.php +++ b/app/Models/StudyReport.php @@ -59,7 +59,7 @@ public function viewUrl(): string public function saveContent(string $content): void { - $this->file_path = ReportStorage::reportFilepath($this->study); + $this->file_path = ReportStorage::randomFilepath($this->study); Storage::disk('public')->put($this->file_path, $content); } diff --git a/app/Services/ReportStorage.php b/app/Services/ReportStorage.php index 07dc4a5..215f214 100644 --- a/app/Services/ReportStorage.php +++ b/app/Services/ReportStorage.php @@ -3,22 +3,45 @@ namespace App\Services; use App\Models\Study; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; final readonly class ReportStorage { - public static function studyFolder(Study|int $study): string + final public const REPORTS_DISK = 'public'; + final public const REPORTS_DIRECTORY = 'reports'; + + public static function folder(Study|int $study): string { $hash = md5($study instanceof Study ? $study->id : $study); - return 'reports/' . substr($hash, 0, 2) . '/' . substr($hash, 2, 2) . '/' . substr($hash, 4, 2); + return implode('/', [ + self::REPORTS_DIRECTORY, + substr($hash, 0, 2), + substr($hash, 2, 2), + substr($hash, 4, 2), + ]); } - public static function reportFilepath(Study|int $study, string $ext = '.html'): string + public static function randomFilepath(Study|int $study, string $ext = '.html'): string { - $directory = self::studyFolder($study); $filename = Str::random(16) . $ext; - return $directory . '/' . $filename; + return self::customFilepath($study, $filename); + } + + public static function customFilepath(Study|int $study, string $filename): string + { + return self::folder($study) . '/' . $filename; + } + + public static function abspath(string $path): string + { + return Storage::disk(self::REPORTS_DISK)->path($path); + } + + public static function exists(Study|int $study, string $filename): bool + { + return Storage::disk(self::REPORTS_DISK)->exists(self::customFilepath($study, $filename)); } }