1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('enum_to_names')) { |
4
|
|
|
/** |
5
|
|
|
* Returns an array of enum names |
6
|
|
|
* |
7
|
|
|
* @param UnitEnum[] $enums |
8
|
|
|
* @return array |
9
|
|
|
*/ |
10
|
|
|
function enum_to_names(array $enums): array |
11
|
|
|
{ |
12
|
|
|
return array_column($enums, 'name'); |
13
|
|
|
} |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
if (!function_exists('disk_driver_is_local')) { |
17
|
|
|
/** |
18
|
|
|
* Check if given driver is local |
19
|
|
|
* |
20
|
|
|
* @param string $disk |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
function disk_driver_is_local(string $disk): bool |
24
|
|
|
{ |
25
|
|
|
return config("filesystems.disks.$disk.driver") == \Mostafaznv\Larupload\Larupload::LOCAL_DRIVER; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (!function_exists('larupload_temp_dir')) { |
30
|
|
|
/** |
31
|
|
|
* Get temp directory |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
function larupload_temp_dir(): string |
36
|
|
|
{ |
37
|
|
|
// @codeCoverageIgnoreStart |
38
|
|
|
if (ini_get('upload_tmp_dir')) { |
39
|
|
|
$path = ini_get('upload_tmp_dir'); |
40
|
|
|
} |
41
|
|
|
else if (getenv('temp')) { |
42
|
|
|
$path = getenv('temp'); |
43
|
|
|
} |
44
|
|
|
// @codeCoverageIgnoreEnd |
45
|
|
|
else { |
46
|
|
|
$path = sys_get_temp_dir(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return rtrim($path, '/'); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!function_exists('split_larupload_path')) { |
54
|
|
|
/** |
55
|
|
|
* Extract name from path |
56
|
|
|
* |
57
|
|
|
* @param string $dir |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
function split_larupload_path(string $dir): array |
61
|
|
|
{ |
62
|
|
|
$path = dirname($dir); |
63
|
|
|
$name = pathinfo($dir, PATHINFO_BASENAME); |
64
|
|
|
|
65
|
|
|
return [$path, $name]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!function_exists('get_larupload_save_path')) { |
70
|
|
|
/** |
71
|
|
|
* Get save to path |
72
|
|
|
* |
73
|
|
|
* @param string $disk |
74
|
|
|
* @param string $saveTo |
75
|
|
|
* @param string|null $extension |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
function get_larupload_save_path(string $disk, string $saveTo, ?string $extension = null): array |
79
|
|
|
{ |
80
|
|
|
$saveTo = larupload_style_path($saveTo, $extension); |
81
|
|
|
$permanent = \Illuminate\Support\Facades\Storage::disk($disk)->path($saveTo); |
82
|
|
|
list($path, $name) = split_larupload_path($saveTo); |
83
|
|
|
|
84
|
|
|
if (disk_driver_is_local($disk)) { |
85
|
|
|
$temp = null; |
86
|
|
|
} |
87
|
|
|
else { |
88
|
|
|
$tempDir = larupload_temp_dir(); |
89
|
|
|
$tempName = \Illuminate\Support\Carbon::now()->unix() . '-' . $name; |
|
|
|
|
90
|
|
|
$temp = "$tempDir/$tempName"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'path' => $path, |
95
|
|
|
'name' => $name, |
96
|
|
|
'temp' => $temp, |
97
|
|
|
'local' => $temp ?: $permanent, |
98
|
|
|
'permanent' => $permanent, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!function_exists('larupload_finalize_save')) { |
104
|
|
|
/** |
105
|
|
|
* Upload local files/folders to remote server, if driver was not local |
106
|
|
|
* |
107
|
|
|
* @param string $disk |
108
|
|
|
* @param array $saveTo |
109
|
|
|
* @param bool $isFolder |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
function larupload_finalize_save(string $disk, array $saveTo, bool $isFolder = false): void |
113
|
|
|
{ |
114
|
|
|
if ($saveTo['temp']) { |
115
|
|
|
if ($isFolder) { |
116
|
|
|
$path = $saveTo['temp']; |
117
|
|
|
$rii = new RecursiveIteratorIterator( |
118
|
|
|
new RecursiveDirectoryIterator($path) |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
foreach ($rii as $r) { |
123
|
|
|
if ($r->isFile()) { |
124
|
|
|
$filePath = $r->getPathname(); |
125
|
|
|
$subPath = str_replace("$path/", '', $filePath); |
126
|
|
|
$saveToPath = dirname($saveTo['path'] . '/' . $saveTo['name'] . '/' . $subPath); |
127
|
|
|
|
128
|
|
|
$fileObject = new \Symfony\Component\HttpFoundation\File\File($filePath); |
129
|
|
|
|
130
|
|
|
\Illuminate\Support\Facades\Storage::disk($disk)->putFileAs( |
131
|
|
|
$saveToPath, $fileObject, $fileObject->getFilename() |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
@unlink($filePath); |
|
|
|
|
135
|
|
|
unset($fileObject); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
@rmdir($path); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
else { |
142
|
|
|
$file = new \Symfony\Component\HttpFoundation\File\File($saveTo['temp']); |
143
|
|
|
|
144
|
|
|
\Illuminate\Support\Facades\Storage::disk($disk)->putFileAs( |
145
|
|
|
$saveTo['path'], $file, $saveTo['name'] |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
@unlink($saveTo['temp']); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!function_exists('file_has_value')) { |
155
|
|
|
/** |
156
|
|
|
* Check file is set and has value |
157
|
|
|
* |
158
|
|
|
* @param mixed $file |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
function file_has_value(mixed $file): bool |
162
|
|
|
{ |
163
|
|
|
return $file and ($file instanceof \Illuminate\Http\UploadedFile); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!function_exists('file_is_valid')) { |
168
|
|
|
/** |
169
|
|
|
* Throw exception if file is not valid |
170
|
|
|
* |
171
|
|
|
* @param mixed $file |
172
|
|
|
* @param string $name |
173
|
|
|
* @param string $type |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
function file_is_valid(mixed $file, string $name, string $type): bool |
177
|
|
|
{ |
178
|
|
|
if ($file instanceof \Illuminate\Http\UploadedFile) { |
179
|
|
|
$condition = $file->isValid() === false; |
180
|
|
|
$message = trans('larupload::messages.file-has-error', [ |
181
|
|
|
'attribute' => "$name-$type", |
182
|
|
|
'error' => $file->getErrorMessage() |
183
|
|
|
]); |
184
|
|
|
|
185
|
|
|
throw_if($condition, $message); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!function_exists('larupload_style_path')) { |
193
|
|
|
/** |
194
|
|
|
* Change path extension |
195
|
|
|
* |
196
|
|
|
* @param string $path |
197
|
|
|
* @param string|null $extension |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
function larupload_style_path(string $path, ?string $extension): string |
201
|
|
|
{ |
202
|
|
|
if ($extension) { |
203
|
|
|
$info = pathinfo($path); |
204
|
|
|
|
205
|
|
|
return $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $extension; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $path; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
|