This commit is contained in:
Dr Masroor Ehsan 2025-01-10 12:15:31 +06:00
parent fb472f29c5
commit 3835897a88
2 changed files with 29 additions and 6 deletions

View File

@ -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);
}

View File

@ -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));
}
}