1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\UploadEntity; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Mostafaznv\Larupload\Actions\GenerateFileIdAction; |
9
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadMode; |
10
|
|
|
use Mostafaznv\Larupload\UploadEntities; |
11
|
|
|
|
12
|
|
|
trait BaseUploadEntity |
13
|
|
|
{ |
14
|
|
|
public function __construct(string $name, LaruploadMode $mode) |
15
|
|
|
{ |
16
|
|
|
$config = config('larupload'); |
17
|
|
|
|
18
|
|
|
$this->name = $name; |
|
|
|
|
19
|
|
|
$this->nameKebab = str_replace('_', '-', Str::kebab($name)); |
|
|
|
|
20
|
|
|
$this->mode = $mode; |
|
|
|
|
21
|
|
|
$this->disk = $config['disk']; |
|
|
|
|
22
|
|
|
$this->localDisk = $config['local-disk']; |
|
|
|
|
23
|
|
|
$this->secureIdsMethod = $config['secure-ids']; |
|
|
|
|
24
|
|
|
$this->withMeta = $config['with-meta']; |
|
|
|
|
25
|
|
|
$this->camelCaseResponse = $config['camel-case-response']; |
|
|
|
|
26
|
|
|
$this->namingMethod = $config['naming-method']; |
|
|
|
|
27
|
|
|
$this->lang = $config['lang']; |
|
|
|
|
28
|
|
|
$this->imageProcessingLibrary = $config['image-processing-library']; |
|
|
|
|
29
|
|
|
$this->generateCover = $config['generate-cover']; |
|
|
|
|
30
|
|
|
$this->coverStyle = $config['cover-style']; |
|
|
|
|
31
|
|
|
$this->dominantColor = $config['dominant-color']; |
|
|
|
|
32
|
|
|
$this->dominantColorQuality = $config['dominant-color-quality']; |
|
|
|
|
33
|
|
|
$this->keepOldFiles = $config['keep-old-files']; |
|
|
|
|
34
|
|
|
$this->preserveFiles = $config['preserve-files']; |
|
|
|
|
35
|
|
|
$this->optimizeImage = $config['optimize-image']['enable'] ?? false; |
|
|
|
|
36
|
|
|
$this->ffmpegQueue = $config['ffmpeg']['queue']; |
|
|
|
|
37
|
|
|
$this->ffmpegMaxQueueNum = $config['ffmpeg']['max-queue-num']; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function make(string $name, LaruploadMode $mode = LaruploadMode::HEAVY): UploadEntities |
41
|
|
|
{ |
42
|
|
|
return new static($name, $mode); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function getMode(): LaruploadMode |
47
|
|
|
{ |
48
|
|
|
return $this->mode; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setOutput(Model $model): void |
52
|
|
|
{ |
53
|
|
|
$this->id = GenerateFileIdAction::make($model, $this->secureIdsMethod, $this->mode, $this->name)->run(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
if ($this->mode === LaruploadMode::HEAVY) { |
56
|
|
|
foreach ($this->output as $key => $value) { |
57
|
|
|
$this->output[$key] = $model->{"{$this->name}_file_$key"}; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
else { |
61
|
|
|
$meta = json_decode($model->{"{$this->name}_file_meta"}, true); |
62
|
|
|
|
63
|
|
|
if (is_array($meta)) { |
64
|
|
|
foreach ($meta as $key => $value) { |
65
|
|
|
$this->output[$key] = $value; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prepare output array to response |
73
|
|
|
* |
74
|
|
|
* @return object |
75
|
|
|
*/ |
76
|
|
|
protected function outputToObject(): object |
77
|
|
|
{ |
78
|
|
|
$output = (object)$this->output; |
79
|
|
|
|
80
|
|
|
if ($this->camelCaseResponse) { |
81
|
|
|
$output->mimeType = $output->mime_type; |
82
|
|
|
$output->dominantColor = $output->dominant_color; |
83
|
|
|
|
84
|
|
|
unset($output->mime_type); |
85
|
|
|
unset($output->dominant_color); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $output; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Path Helper to generate relative path string |
93
|
|
|
* |
94
|
|
|
* @param string $id |
95
|
|
|
* @param string|null $folder |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getBasePath(string $id, string $folder = null): string |
99
|
|
|
{ |
100
|
|
|
$path = $this->mode == LaruploadMode::STANDALONE ? "$this->folder/$this->nameKebab" : "$this->folder/$id/$this->nameKebab"; |
101
|
|
|
$path = trim($path, '/'); |
102
|
|
|
|
103
|
|
|
if ($folder) { |
104
|
|
|
$folder = strtolower(str_replace('_', '-', trim($folder))); |
105
|
|
|
|
106
|
|
|
return "$path/$folder"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $path; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|