1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HustleWorks\ChuteLaravel\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* App\Models\ImageTransformation |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property int $image_original_id |
13
|
|
|
* @property string $filename |
14
|
|
|
* @property string $name |
15
|
|
|
* @property string $disk |
16
|
|
|
* @property string $path |
17
|
|
|
* @property string|null $width |
18
|
|
|
* @property string|null $height |
19
|
|
|
* @property string|null $quality |
20
|
|
|
* @property int|null $size_in_kilobytes |
21
|
|
|
* @property string|null $mime_type |
22
|
|
|
* @property \Carbon\Carbon|null $created_at |
23
|
|
|
* @property \Carbon\Carbon|null $updated_at |
24
|
|
|
* @property-read \HustleWorks\ChuteLaravel\Models\Image $imageOriginal |
25
|
|
|
* @method static Builder|ImageTransformation whereCreatedAt($value) |
26
|
|
|
* @method static Builder|ImageTransformation whereDisk($value) |
27
|
|
|
* @method static Builder|ImageTransformation whereHeight($value) |
28
|
|
|
* @method static Builder|ImageTransformation whereId($value) |
29
|
|
|
* @method static Builder|ImageTransformation whereImageOriginalId($value) |
30
|
|
|
* @method static Builder|ImageTransformation whereMimeType($value) |
31
|
|
|
* @method static Builder|ImageTransformation whereName($value) |
32
|
|
|
* @method static Builder|ImageTransformation wherePath($value) |
33
|
|
|
* @method static Builder|ImageTransformation whereQuality($value) |
34
|
|
|
* @method static Builder|ImageTransformation whereSizeInKilobytes($value) |
35
|
|
|
* @method static Builder|ImageTransformation whereUpdatedAt($value) |
36
|
|
|
* @method static Builder|ImageTransformation whereWidth($value) |
37
|
|
|
* @mixin \Eloquent |
38
|
|
|
*/ |
39
|
|
|
class ImageTransformation extends Model |
40
|
|
|
{ |
41
|
|
|
protected $fillable = [ |
42
|
|
|
'image_original_id', |
43
|
|
|
'disk', |
44
|
|
|
'filename', |
45
|
|
|
'name', |
46
|
|
|
'path', |
47
|
|
|
'width', |
48
|
|
|
'height', |
49
|
|
|
'quality', |
50
|
|
|
'size', |
51
|
|
|
'mime_type', |
52
|
|
|
'size_in_kilobytes', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Image Original |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
59
|
|
|
*/ |
60
|
|
|
protected function imageOriginal() |
61
|
|
|
{ |
62
|
|
|
return $this->belongsTo(Image::class, 'image_id'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function imageUrl() |
66
|
|
|
{ |
67
|
|
|
return \Storage::disk($this->imageOriginal->disk) |
68
|
|
|
->url("{$this->imageOriginal->path}/{$this->imageOriginal->uuid}/$this->filename"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The "booting" method of the model. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
protected static function boot() |
77
|
|
|
{ |
78
|
|
|
parent::boot(); |
79
|
|
|
|
80
|
|
View Code Duplication |
static::deleting(function (ImageTransformation $image_transformation) { |
|
|
|
|
81
|
|
|
$image_original = $image_transformation->imageOriginal; |
82
|
|
|
$path = "$image_original->directory/$image_original->uuid/$image_transformation->path"; |
83
|
|
|
if (\Storage::disk($image_transformation->disk)->exists($path)) { |
84
|
|
|
\Storage::disk($image_transformation->disk)->delete($path); |
85
|
|
|
} |
86
|
|
|
if (!\Storage::disk($image_transformation->disk)->files("$image_original->directory/$image_original->uuid")) { |
87
|
|
|
\Storage::disk($image_transformation->disk)->deleteDirectory("$image_original->directory/$image_original->uuid"); |
88
|
|
|
} |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.