1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ReliqArts\GuidedImage\Traits; |
4
|
|
|
|
5
|
|
|
use File; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
use ReliqArts\GuidedImage\Exceptions\BadImplementation; |
8
|
|
|
use ReliqArts\GuidedImage\Helpers\Config; |
9
|
|
|
use ReliqArts\GuidedImage\ViewModels\Result; |
10
|
|
|
use URL; |
11
|
|
|
use Validator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get guided by acquiring these traits. |
15
|
|
|
* |
16
|
|
|
* @author Patrick Reid (@IAmReliQ) |
17
|
|
|
* |
18
|
|
|
* @since 2016 |
19
|
|
|
* |
20
|
|
|
* @uses \ReliqArts\GuidedImage\ViewModels\Result; |
21
|
|
|
*/ |
22
|
|
|
trait Guided |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The rules that govern a guided image. |
26
|
|
|
*/ |
27
|
|
|
public static $rules = ['file' => 'required|mimes:png,gif,jpeg|max:2048']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $className; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Mandatory ancestor eloguent model. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $eloquentAncestor = 'Illuminate\Database\Eloquent\Model'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Ensure things are ready. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $attributes = []) |
47
|
|
|
{ |
48
|
|
|
$this->className = get_class($this); |
49
|
|
|
// Instance must be of class which extends Eloquent Model. |
50
|
|
|
if (!is_subclass_of($this, $this->eloquentAncestor)) { |
|
|
|
|
51
|
|
|
throw new BadImplementation("Guided model ({$this->className}) must extend {$this->eloquentAncestor}."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
parent::__construct($attributes); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve the creator (uploader) of the image. |
59
|
|
|
*/ |
60
|
|
|
public function creator(): BelongsTo |
61
|
|
|
{ |
62
|
|
|
return $this->belongsTo('App\User', 'creator_id'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Whether image is safe for deleting. |
67
|
|
|
* Since a single image may be re-used this method is used to determine |
68
|
|
|
* when an image can be safely deleted from disk. |
69
|
|
|
* |
70
|
|
|
* @param int $safeAmount a photo is safe to delete if it is used by $safe_num amount of records |
71
|
|
|
* |
72
|
|
|
* @return bool whether image is safe for delete |
73
|
|
|
*/ |
74
|
|
|
public function isSafeForDelete(int $safeAmount = 1): bool |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Removes image from database, and filesystem, if not in use. |
81
|
|
|
* |
82
|
|
|
* @param bool $force override safety constraints |
83
|
|
|
* |
84
|
|
|
* @return Result |
85
|
|
|
*/ |
86
|
|
|
public function remove(bool $force = false): Result |
87
|
|
|
{ |
88
|
|
|
$result = new Result(); |
89
|
|
|
$img_name = $this->getName(); |
|
|
|
|
90
|
|
|
$safe = $this->isSafeForDelete(); |
91
|
|
|
|
92
|
|
|
if ($safe || $force) { |
93
|
|
|
if (File::delete(urldecode($this->getFullPath()))) { |
94
|
|
|
$this->delete(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
$result->success = true; |
97
|
|
|
} else { |
98
|
|
|
$result->message = 'Not safe to delete, hence file not removed.'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get routed link to photo. |
106
|
|
|
* |
107
|
|
|
* @param array $params parameters to pass to route |
|
|
|
|
108
|
|
|
* @param string $type Operation to be performed on instance. (resize, thumb) |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function routeResized(array $params = null, string $type = 'resize'): string |
113
|
|
|
{ |
114
|
|
|
$guidedModel = strtolower(Config::getRouteModel(true)); |
115
|
|
|
|
116
|
|
|
if (!(in_array($type, ['resize', 'thumb'], true) && is_array($params))) { |
117
|
|
|
return $this->url(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
array_unshift($params, $this->id); |
|
|
|
|
120
|
|
|
|
121
|
|
|
return route("{$guidedModel}.{$type}", $params); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get class. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getClassName(): string |
130
|
|
|
{ |
131
|
|
|
return $this->className; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get ready URL to image. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getUrl(): string |
140
|
|
|
{ |
141
|
|
|
return urldecode($this->getFullPath()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get ready image title. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getTitle(): string |
150
|
|
|
{ |
151
|
|
|
return title_case(preg_replace('/[\\-_]/', ' ', $this->getName())); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get full path. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getFullPath(): string |
160
|
|
|
{ |
161
|
|
|
return $this->full_path; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get name. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getName(): string |
170
|
|
|
{ |
171
|
|
|
return $this->name; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get upload directory. |
176
|
|
|
* |
177
|
|
|
* @return string upload directory |
178
|
|
|
*/ |
179
|
|
|
public static function getUploadDir(): string |
180
|
|
|
{ |
181
|
|
|
return Config::get('guidedimage.upload_dir'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Upload and save image. |
186
|
|
|
* |
187
|
|
|
* @param \Illuminate\Http\UploadedFile|Symfony\Component\HttpFoundation\File\UploadedFile $imageFile File |
188
|
|
|
* from request.e.g. $request->file('image'); |
|
|
|
|
189
|
|
|
* |
190
|
|
|
* @return Result |
191
|
|
|
*/ |
192
|
|
|
public static function upload($imageFile): Result |
193
|
|
|
{ |
194
|
|
|
$result = new Result(); |
195
|
|
|
$validator = Validator::make(['file' => $imageFile], self::$rules); |
196
|
|
|
$extWhitelist = Config::get('guidedimage.allowed_extensions', ['gif', 'jpg', 'jpeg', 'png']); |
197
|
|
|
$result->message = 'Invalid file size or type.'; |
198
|
|
|
$result->error = 'Invalid image.'; |
199
|
|
|
|
200
|
|
|
if ($validator->passes()) { |
201
|
|
|
$size = $imageFile->getSize(); |
202
|
|
|
$mimeType = $imageFile->getMimeType(); |
203
|
|
|
$extension = $imageFile->getClientOriginalExtension(); |
204
|
|
|
$fullName = $imageFile->getClientOriginalName(); |
205
|
|
|
$filePathInfo = pathinfo($fullName); |
206
|
|
|
$filename = str_slug($filePathInfo['filename']); |
207
|
|
|
$existing = self::where('name', $filename)->where('size', $size); |
208
|
|
|
|
209
|
|
|
// explicitly check extension against whitelist |
210
|
|
|
if (in_array(strtolower($extension), $extWhitelist, true)) { |
211
|
|
|
if (!$existing->count()) { |
212
|
|
|
$im['size'] = $size; |
|
|
|
|
213
|
|
|
$im['name'] = $filename; |
214
|
|
|
$im['mime_type'] = $mimeType; |
215
|
|
|
$im['extension'] = $extension; |
216
|
|
|
$im['location'] = self::getUploadDir(); |
217
|
|
|
$im['creator_id'] = auth()->user()->id; |
|
|
|
|
218
|
|
|
$im['full_path'] = urlencode($im['location'] . '/' . $filename . '.' . $im['extension']); |
219
|
|
|
list($im['width'], $im['height']) = getimagesize($imageFile); |
220
|
|
|
|
221
|
|
|
try { |
222
|
|
|
$file = $imageFile->move($im['location'], $im['name'] . '.' . $im['extension']); |
|
|
|
|
223
|
|
|
$newImage = new self(); |
224
|
|
|
|
225
|
|
|
// file moved, save |
226
|
|
|
$newImage->fill($im); |
|
|
|
|
227
|
|
|
if ($newImage->save()) { |
|
|
|
|
228
|
|
|
$result->extra = $newImage; |
229
|
|
|
$result->success = true; |
230
|
|
|
$result->error = null; |
231
|
|
|
} |
232
|
|
|
} catch (Exception $e) { |
|
|
|
|
233
|
|
|
$result->error = $e->getMessage(); |
234
|
|
|
$result->message = null; |
235
|
|
|
} |
236
|
|
|
} else { |
237
|
|
|
$result->extra = $existing->first(); |
238
|
|
|
$result->message = 'Image reused.'; |
239
|
|
|
$result->success = true; |
240
|
|
|
$result->error = null; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $result; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|