|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
use Mostafaznv\Larupload\Actions\SetFileNameAction; |
|
8
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadFileType; |
|
9
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadMode; |
|
10
|
|
|
use Mostafaznv\Larupload\Larupload; |
|
11
|
|
|
|
|
12
|
|
|
trait BaseAttachment |
|
13
|
|
|
{ |
|
14
|
|
|
protected function setBasicDetails(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$fileName = SetFileNameAction::make($this->file, $this->namingMethod, $this->lang)->generate(); |
|
17
|
|
|
|
|
18
|
|
|
$this->output['name'] = $fileName; |
|
|
|
|
|
|
19
|
|
|
$this->output['id'] = $this->id; |
|
20
|
|
|
$this->output['format'] = $this->file->getClientOriginalExtension(); |
|
21
|
|
|
$this->output['size'] = $this->file->getSize(); |
|
22
|
|
|
$this->output['type'] = $this->type->name; |
|
23
|
|
|
$this->output['mime_type'] = $this->file->getMimeType(); |
|
24
|
|
|
|
|
25
|
|
|
if ($this->storeOriginalFileName) { |
|
26
|
|
|
$this->output['original_name'] = $this->file->getClientOriginalName(); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function setMediaDetails(): void |
|
31
|
|
|
{ |
|
32
|
|
|
switch ($this->type) { |
|
33
|
|
|
case LaruploadFileType::VIDEO: |
|
34
|
|
|
case LaruploadFileType::AUDIO: |
|
35
|
|
|
$meta = $this->ffmpeg()->getMeta(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$this->output['width'] = $meta->width; |
|
|
|
|
|
|
38
|
|
|
$this->output['height'] = $meta->height; |
|
39
|
|
|
$this->output['duration'] = $meta->duration; |
|
40
|
|
|
|
|
41
|
|
|
break; |
|
42
|
|
|
|
|
43
|
|
|
case LaruploadFileType::IMAGE: |
|
44
|
|
|
$meta = $this->img($this->file)->getMeta(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$this->output['width'] = $meta['width']; |
|
47
|
|
|
$this->output['height'] = $meta['height']; |
|
48
|
|
|
$this->output['dominant_color'] = $this->dominantColor |
|
49
|
|
|
? $this->img($this->file)->getDominantColor($this->file) |
|
50
|
|
|
: null; |
|
51
|
|
|
|
|
52
|
|
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set attributes before saving event |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function setAttributes(Model $model): Model |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->mode === LaruploadMode::HEAVY) { |
|
62
|
|
|
foreach ($this->output as $key => $value) { |
|
63
|
|
|
if ($key == 'original_name' and !$this->storeOriginalFileName) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$model->{"{$this->name}_file_$key"} = $value; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
else { |
|
71
|
|
|
$model->{"{$this->name}_file_name"} = $this->output['name'] ?? null; |
|
72
|
|
|
$model->{"{$this->name}_file_meta"} = json_encode($this->output); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $model; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Upload original file |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function uploadOriginalFile(string $id, ?string $disk = null): void |
|
82
|
|
|
{ |
|
83
|
|
|
Storage::disk($disk ?: $this->disk) |
|
84
|
|
|
->putFileAs( |
|
85
|
|
|
path: $this->getBasePath($id, Larupload::ORIGINAL_FOLDER), |
|
|
|
|
|
|
86
|
|
|
file: $this->file, |
|
87
|
|
|
name: $this->output['name'] |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Clean directory before upload |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function clean($id): void |
|
95
|
|
|
{ |
|
96
|
|
|
$path = $this->getBasePath($id); |
|
97
|
|
|
Storage::disk($this->disk)->deleteDirectory($path); |
|
98
|
|
|
|
|
99
|
|
|
foreach ($this->output as $key => $value) { |
|
100
|
|
|
$this->output[$key] = null; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|