|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadMode; |
|
8
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadSecureIdsMethod; |
|
9
|
|
|
|
|
10
|
|
|
class GenerateFileIdAction |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct( |
|
13
|
|
|
private readonly ?Model $model, |
|
14
|
|
|
private readonly LaruploadSecureIdsMethod $method, |
|
15
|
|
|
private readonly LaruploadMode $attachmentMode, |
|
16
|
|
|
private readonly ?string $attachmentName |
|
17
|
|
|
) {} |
|
18
|
|
|
|
|
19
|
|
|
public static function make(?Model $model, LaruploadSecureIdsMethod $method, LaruploadMode $attachmentMode, string $attachmentName): self |
|
20
|
|
|
{ |
|
21
|
|
|
return new self($model, $method, $attachmentMode, $attachmentName); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function run(): string |
|
26
|
|
|
{ |
|
27
|
|
|
if ($secureId = $this->retrieveCurrentSecureId()) { |
|
28
|
|
|
return $secureId; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return match ($this->method) { |
|
32
|
|
|
LaruploadSecureIdsMethod::ULID => Str::ulid()->toBase32(), |
|
33
|
|
|
LaruploadSecureIdsMethod::UUID => Str::uuid()->toString(), |
|
34
|
|
|
LaruploadSecureIdsMethod::HASHID => $this->hashid(), |
|
35
|
|
|
default => $this->model->id, |
|
36
|
|
|
}; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function retrieveCurrentSecureId(): string|null |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->attachmentMode === LaruploadMode::HEAVY) { |
|
42
|
|
|
return $this->model->{"{$this->attachmentName}_file_id"} ?? null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return json_decode($this->model->{"{$this->attachmentName}_file_meta"})->id ?? null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function hashid(): string |
|
49
|
|
|
{ |
|
50
|
|
|
$salt = config('app.key'); |
|
51
|
|
|
$hashids = new \Hashids\Hashids($salt, 20); |
|
52
|
|
|
|
|
53
|
|
|
return $hashids->encode($this->model->id); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|