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
|
|
|
|
26
|
|
|
protected function setMediaDetails(): void |
27
|
|
|
{ |
28
|
|
|
switch ($this->type) { |
29
|
|
|
case LaruploadFileType::VIDEO: |
30
|
|
|
case LaruploadFileType::AUDIO: |
31
|
|
|
$meta = $this->ffmpeg()->getMeta(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->output['width'] = $meta->width; |
|
|
|
|
34
|
|
|
$this->output['height'] = $meta->height; |
35
|
|
|
$this->output['duration'] = $meta->duration; |
36
|
|
|
|
37
|
|
|
break; |
38
|
|
|
|
39
|
|
|
case LaruploadFileType::IMAGE: |
40
|
|
|
$meta = $this->img($this->file)->getMeta(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->output['width'] = $meta['width']; |
43
|
|
|
$this->output['height'] = $meta['height']; |
44
|
|
|
$this->output['dominant_color'] = $this->dominantColor |
45
|
|
|
? $this->img($this->file)->getDominantColor($this->file) |
46
|
|
|
: null; |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set attributes before saving event |
54
|
|
|
*/ |
55
|
|
|
protected function setAttributes(Model $model): Model |
56
|
|
|
{ |
57
|
|
|
if ($this->mode === LaruploadMode::HEAVY) { |
58
|
|
|
foreach ($this->output as $key => $value) { |
59
|
|
|
$model->{"{$this->name}_file_$key"} = $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
else { |
63
|
|
|
$model->{"{$this->name}_file_name"} = $this->output['name'] ?? null; |
64
|
|
|
$model->{"{$this->name}_file_meta"} = json_encode($this->output); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $model; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Upload original file |
72
|
|
|
*/ |
73
|
|
|
protected function uploadOriginalFile(string $id, ?string $disk = null): void |
74
|
|
|
{ |
75
|
|
|
Storage::disk($disk ?: $this->disk) |
76
|
|
|
->putFileAs( |
77
|
|
|
path: $this->getBasePath($id, Larupload::ORIGINAL_FOLDER), |
|
|
|
|
78
|
|
|
file: $this->file, |
79
|
|
|
name: $this->output['name'] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Clean directory before upload |
85
|
|
|
*/ |
86
|
|
|
protected function clean($id): void |
87
|
|
|
{ |
88
|
|
|
$path = $this->getBasePath($id); |
89
|
|
|
Storage::disk($this->disk)->deleteDirectory($path); |
90
|
|
|
|
91
|
|
|
foreach ($this->output as $key => $value) { |
92
|
|
|
$this->output[$key] = null; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|