|
1
|
|
|
<?php |
|
2
|
|
|
namespace CbCaio\ImgAttacher\Models; |
|
3
|
|
|
|
|
4
|
|
|
use CbCaio\ImgAttacher\Contracts\AttacherImageContract; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractAttacherImage extends Model implements AttacherImageContract |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $processing_style_routine; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var UploadedFile |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $uploaded_file; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $different_filename; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param UploadedFile $file |
|
27
|
|
|
* @param string $filename |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setAttributesFromFile(UploadedFile $file, $filename = NULL) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setUploadedFile($file); |
|
33
|
|
|
|
|
34
|
|
|
is_null($filename) |
|
35
|
|
|
? $this->setFileNameAttribute($file->getClientOriginalName()) |
|
36
|
|
|
: $this->setFileNameAttribute($filename); |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->setFileExtensionAttribute($file->getClientOriginalExtension()); |
|
40
|
|
|
$this->setMimeTypeAttribute($file->getClientMimeType()); |
|
41
|
|
|
$this->setFileSizeAttribute($file->getSize()); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->filenameIsDifferentFromOriginal()) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setDifferentFilename(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return mixed owner model |
|
53
|
|
|
*/ |
|
54
|
|
|
public function owner() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->morphTo(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getOwnerType() |
|
63
|
|
|
{ |
|
64
|
|
|
return array_has($this->attributes, 'owner_type') |
|
65
|
|
|
? $this->attributes['owner_type'] |
|
66
|
|
|
: NULL; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return the base_url followed by the path to the image related to the $processing_style after processing |
|
71
|
|
|
* |
|
72
|
|
|
* @param string|null $processing_style |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getUrl($processing_style) |
|
76
|
|
|
{ |
|
77
|
|
|
return app('img-attacher.FilePathManager')->parseUrl($this, $processing_style); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return the the path to the image related to the $processing_style after processing |
|
82
|
|
|
* |
|
83
|
|
|
* @param string|null $processing_style |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getPath($processing_style) |
|
87
|
|
|
{ |
|
88
|
|
|
return app('img-attacher.FilePathManager')->parsePath($this, $processing_style); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param null|string $processing_style |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getDeletePath($processing_style = NULL) |
|
96
|
|
|
{ |
|
97
|
|
|
return app('img-attacher.FilePathManager')->parseDeletePath($this, $processing_style); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getPreviousPath() |
|
104
|
|
|
{ |
|
105
|
|
|
return app('img-attacher.FilePathManager')->parsePreviousPath($this); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getDifferentFilename() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->different_filename; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setDifferentFilename() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->different_filename = $this->original['file_name']; |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function hasDifferentFileName() |
|
130
|
|
|
{ |
|
131
|
|
|
return empty($this->different_filename) ? FALSE : TRUE; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool | null |
|
136
|
|
|
*/ |
|
137
|
|
|
public function filenameIsDifferentFromOriginal() |
|
138
|
|
|
{ |
|
139
|
|
|
if (empty($this->original)) |
|
140
|
|
|
{ |
|
141
|
|
|
return NULL; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return ($this->attributes['file_name'] != $this->original['file_name']) ? TRUE : FALSE; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getProcessingStyleRoutine() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->processing_style_routine; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param $name |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setProcessingStyleRoutine($name = NULL) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->processing_style_routine = isset($name) ? $name : 'default_routine'; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return UploadedFile |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getUploadedFile() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->uploaded_file; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return UploadedFile |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setUploadedFile(UploadedFile $file) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->uploaded_file = $file; |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getFileNameAttribute() |
|
188
|
|
|
{ |
|
189
|
|
|
return array_has($this->attributes, 'file_name') |
|
190
|
|
|
? $this->attributes['file_name'] |
|
191
|
|
|
: NULL; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $name |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function setFileNameAttribute($name) |
|
199
|
|
|
{ |
|
200
|
|
|
$file_name = str_slug(pathinfo($name, PATHINFO_FILENAME)) . '.' . pathinfo($name, PATHINFO_EXTENSION); |
|
201
|
|
|
|
|
202
|
|
|
$this->attributes['file_name'] = $file_name; |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getFileExtensionAttribute() |
|
211
|
|
|
{ |
|
212
|
|
|
return array_has($this->attributes, 'file_extension') |
|
213
|
|
|
? $this->attributes['file_extension'] |
|
214
|
|
|
: NULL; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param string $extension |
|
219
|
|
|
* @return $this |
|
220
|
|
|
*/ |
|
221
|
|
|
public function setFileExtensionAttribute($extension) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->attributes['file_extension'] = $extension; |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getFileSizeAttribute() |
|
232
|
|
|
{ |
|
233
|
|
|
return array_has($this->attributes, 'file_size') |
|
234
|
|
|
? $this->attributes['file_size'] |
|
235
|
|
|
: NULL; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param string $size |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setFileSizeAttribute($size) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->attributes['file_size'] = $size; |
|
245
|
|
|
|
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getMimeTypeAttribute() |
|
253
|
|
|
{ |
|
254
|
|
|
return array_has($this->attributes, 'mime_type') |
|
255
|
|
|
? $this->attributes['mime_type'] |
|
256
|
|
|
: NULL; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param string $type |
|
261
|
|
|
* @return $this |
|
262
|
|
|
*/ |
|
263
|
|
|
public function setMimeTypeAttribute($type) |
|
264
|
|
|
{ |
|
265
|
|
|
$this->attributes['mime_type'] = $type; |
|
266
|
|
|
|
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
} |