|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits\enso\files; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
6
|
|
|
use Illuminate\Http\File as IlluminateFile; |
|
7
|
|
|
use Illuminate\Http\UploadedFile; |
|
8
|
|
|
use Illuminate\Support\Facades\App; |
|
9
|
|
|
use Illuminate\Support\Facades\Config; |
|
10
|
|
|
use Illuminate\Support\Facades\Storage; |
|
11
|
|
|
use App\Models\User; |
|
12
|
|
|
use LaravelEnso\Files\App\Models\File; |
|
13
|
|
|
use App\Service\enso\files\Files; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
15
|
|
|
|
|
16
|
|
|
trait HasFile |
|
17
|
|
|
{ |
|
18
|
|
|
public function file(): Relation |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->morphOne(File::class, 'attachable'); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function inline(): StreamedResponse |
|
24
|
|
|
{ |
|
25
|
|
|
return (new Files($this))->inline(); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function download(): StreamedResponse |
|
29
|
|
|
{ |
|
30
|
|
|
return (new Files($this))->download(); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function temporaryLink(): string |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->file->temporaryLink(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function attach(IlluminateFile $file, string $originalName, ?User $user): void |
|
39
|
|
|
{ |
|
40
|
|
|
(new Files($this))->attach($file, $originalName, $user); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function upload(UploadedFile $file): void |
|
44
|
|
|
{ |
|
45
|
|
|
(new Files($this)) |
|
|
|
|
|
|
46
|
|
|
->mimeTypes($this->mimeTypes()) |
|
47
|
|
|
->extensions($this->extensions()) |
|
48
|
|
|
->optimize($this->optimizeImages()) |
|
49
|
|
|
->resize($this->resizeImages()) |
|
50
|
|
|
->upload($file); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function folder(): string |
|
54
|
|
|
{ |
|
55
|
|
|
if (App::environment('testing')) { |
|
56
|
|
|
$directory = Config::get('enso.files.testingFolder'); |
|
57
|
|
|
|
|
58
|
|
|
if (! Storage::has($directory)) { |
|
59
|
|
|
Storage::makeDirectory($directory); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $directory; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->folder; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function publicPath(): ?string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->file |
|
71
|
|
|
? "{$this->folder()}/{$this->file->saved_name}" |
|
72
|
|
|
: null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function mimeTypes(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return property_exists($this, 'mimeTypes') |
|
78
|
|
|
? $this->mimeTypes |
|
79
|
|
|
: []; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function extensions(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return property_exists($this, 'extensions') |
|
85
|
|
|
? $this->extensions |
|
86
|
|
|
: []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function resizeImages(): array |
|
90
|
|
|
{ |
|
91
|
|
|
return property_exists($this, 'resizeImages') |
|
92
|
|
|
? $this->resizeImages |
|
93
|
|
|
: []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function optimizeImages(): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return property_exists($this, 'optimizeImages') |
|
99
|
|
|
? $this->optimizeImages |
|
100
|
|
|
: false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected static function bootHasFile() |
|
104
|
|
|
{ |
|
105
|
|
|
self::deleting(fn ($model) => (new Files($model))->delete()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|