48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Report;
|
|
|
|
use App\Models\Study;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
final readonly class ReportStorage
|
|
{
|
|
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 implode('/', [
|
|
self::REPORTS_DIRECTORY,
|
|
substr($hash, 0, 2),
|
|
substr($hash, 2, 2),
|
|
substr($hash, 4, 2),
|
|
]);
|
|
}
|
|
|
|
public static function randomFilepath(Study|int $study, string $ext = '.html'): string
|
|
{
|
|
$filename = Str::random(16) . $ext;
|
|
|
|
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));
|
|
}
|
|
}
|