1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Uploadable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
|
7
|
|
|
class UploadOptions |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* upload Create Dir Mode file Mask (default '0755') |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
public $uploadCreateDirModeMask = '0755'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* If set to true, a $model->uploadFileNameSuffixSeparator.$model->id suffx are added to upload file name. |
17
|
|
|
* Ex.: |
18
|
|
|
* original uploaded file name: pippo.jpg |
19
|
|
|
* final name: 'pippo'.$model->uploadFileNameSuffixSeparator.$model->id.'jpg' |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
public $appendModelIdSuffixInUploadedFileName = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Suffix separator to generate new file name |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $uploadFileNameSuffixSeparator = '_'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array of upload attributes |
32
|
|
|
* Ex.: |
33
|
|
|
* public $uploads = ['image', 'image_mobile']; |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public $uploads = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Array of Mime type string. |
40
|
|
|
* Ex. (default): |
41
|
|
|
* public $uploadsMimeType = [ |
42
|
|
|
* 'image/gif', |
43
|
|
|
* 'image/jpeg', |
44
|
|
|
* 'image/png', |
45
|
|
|
* ]; |
46
|
|
|
* A full listing of MIME types and their corresponding extensions may be found |
47
|
|
|
* at the following location: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
public $uploadsMimeType = [ |
51
|
|
|
'image/gif', |
52
|
|
|
'image/jpeg', |
53
|
|
|
'image/png', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* upload base path relative to $storageDiskName root folder |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
public $uploadBasePath; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The default storage disk name |
64
|
|
|
* used for default upload base path |
65
|
|
|
* default: public |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
public $storageDiskName = 'public'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* An associative array of 'attribute_name' => 'uploadBasePath' |
72
|
|
|
* set an attribute name here to override its default upload base path |
73
|
|
|
* The path is relative to $storageDiskName root folder. |
74
|
|
|
* Ex.: |
75
|
|
|
* public $uploadPaths = ['image' => 'product', 'image_thumb' => 'product/thumb']; |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
public $uploadPaths = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The laravel storage disk to store uploaded files |
82
|
|
|
* @var \Illuminate\Filesystem\FilesystemAdapter |
83
|
|
|
*/ |
84
|
|
|
public $storage; |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return UploadOptions |
89
|
|
|
*/ |
90
|
|
|
public static function create(): UploadOptions |
91
|
|
|
{ |
92
|
|
|
return new static(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* upload Create Dir Mode file Mask (Ex.: '0755') |
97
|
|
|
* @param string $mask |
98
|
|
|
* @return UploadOptions |
99
|
|
|
*/ |
100
|
|
|
public function setCreateDirModeMask(string $mask): UploadOptions |
101
|
|
|
{ |
102
|
|
|
$this->uploadCreateDirModeMask = $mask; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* $model->uploadFileNameSuffixSeparator.$model->id suffx are added to upload file name. |
109
|
|
|
* Ex.: |
110
|
|
|
* original uploaded file name: pippo.jpg |
111
|
|
|
* final name: 'pippo'.$model->uploadFileNameSuffixSeparator.$model->id.'jpg' |
112
|
|
|
*/ |
113
|
|
|
public function appendModelIdSuffixInFileName(): UploadOptions |
114
|
|
|
{ |
115
|
|
|
$this->appendModelIdSuffixInUploadedFileName = true; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
*/ |
122
|
|
|
public function dontAppendModelIdSuffixInFileName(): UploadOptions |
123
|
|
|
{ |
124
|
|
|
$this->appendModelIdSuffixInUploadedFileName = false; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Suffix separator to generate new file name |
131
|
|
|
* @param string $suffix |
132
|
|
|
* @return UploadOptions |
133
|
|
|
*/ |
134
|
|
|
public function setFileNameSuffixSeparator(string $suffix): UploadOptions |
135
|
|
|
{ |
136
|
|
|
$this->uploadFileNameSuffixSeparator = $suffix; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Array of upload attributes |
143
|
|
|
* Ex.: ['image', 'image_mobile']; |
144
|
|
|
* @param array $attributes |
145
|
|
|
* @return UploadOptions |
146
|
|
|
*/ |
147
|
|
|
public function setUploadsAttributes(array $attributes): UploadOptions |
148
|
|
|
{ |
149
|
|
|
$this->uploads = $attributes; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Array of Mime type string. |
156
|
|
|
* Ex.: |
157
|
|
|
* [ |
158
|
|
|
* 'image/gif', |
159
|
|
|
* 'image/jpeg', |
160
|
|
|
* 'image/png', |
161
|
|
|
* ]; |
162
|
|
|
* A full listing of MIME types and their corresponding extensions may be found |
163
|
|
|
* at the following location: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types |
164
|
|
|
* @param array $mime |
165
|
|
|
* @return UploadOptions |
166
|
|
|
*/ |
167
|
|
|
public function setMimeType(array $mime): UploadOptions |
168
|
|
|
{ |
169
|
|
|
$this->uploadsMimeType = $mime; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* upload base path. path relative to $storageDiskName root folder |
176
|
|
|
* Ex.: public/upload/news |
177
|
|
|
* @param string $path |
178
|
|
|
* @return UploadOptions |
179
|
|
|
*/ |
180
|
|
|
public function setUploadBasePath(string $path): UploadOptions |
181
|
|
|
{ |
182
|
|
|
$this->uploadBasePath = canonicalize($path); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* An associative array of 'attribute_name' => 'uploadBasePath' |
189
|
|
|
* set an attribute name here to override its default upload base path |
190
|
|
|
* The path is relative to $storageDiskName root folder. |
191
|
|
|
* Ex.: |
192
|
|
|
* public $uploadPaths = ['image' => 'product', 'image_thumb' => 'product/thumb']; |
193
|
|
|
* @param array $attributesPaths |
194
|
|
|
* @return UploadOptions |
195
|
|
|
*/ |
196
|
|
|
public function setUploadPaths(array $attributesPaths): UploadOptions |
197
|
|
|
{ |
198
|
|
|
array_map(function ($v) { |
199
|
|
|
return $v == '' ? $v : canonicalize($v); |
200
|
|
|
}, $attributesPaths); |
201
|
|
|
|
202
|
|
|
$this->uploadPaths = $attributesPaths; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set a Storage Disk name |
209
|
|
|
* @param string $diskName |
210
|
|
|
* @return UploadOptions |
211
|
|
|
*/ |
212
|
|
|
public function setStorageDisk(string $diskName) |
213
|
|
|
{ |
214
|
|
|
$this->storage = Storage::disk($diskName); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get a Storage Disk |
221
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter |
222
|
|
|
*/ |
223
|
|
|
public function getStorage() : \Illuminate\Filesystem\FilesystemAdapter |
224
|
|
|
{ |
225
|
|
|
return $this->storage; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get the options for generating the upload. |
230
|
|
|
*/ |
231
|
|
|
public function getUploadOptionsDefault(): UploadOptions |
232
|
|
|
{ |
233
|
|
|
return UploadOptions::create() |
234
|
|
|
->setCreateDirModeMask('0755') |
235
|
|
|
->appendModelIdSuffixInFileName() |
236
|
|
|
->setFileNameSuffixSeparator('_') |
237
|
|
|
->setUploadsAttributes(['image', 'image_mobile']) |
238
|
|
|
->setMimeType([ |
239
|
|
|
'image/gif', |
240
|
|
|
'image/jpeg', |
241
|
|
|
'image/png', |
242
|
|
|
]) |
243
|
|
|
->setUploadBasePath( 'upload/') |
244
|
|
|
->setUploadPaths([]) |
245
|
|
|
->setStorageDisk('public'); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|