2
2
3
3
namespace App \Traits ;
4
4
5
- use Carbon \Carbon ;
6
- use App \Models \Image ;
7
- use Illuminate \Support \Str ;
8
5
use App \DTOs \Image \ImageDto ;
6
+ use App \Models \Image ;
7
+ use Carbon \Carbon ;
9
8
use Illuminate \Database \Eloquent \Relations \MorphMany ;
9
+ use Illuminate \Database \Eloquent \Relations \MorphOne ;
10
10
use Illuminate \Http \UploadedFile ;
11
11
use Illuminate \Support \Facades \Storage ;
12
- use Illuminate \Database \ Eloquent \ Relations \ MorphOne ;
12
+ use Illuminate \Support \ Str ;
13
13
14
14
trait HasImage
15
15
{
16
- protected string $ disk = 'public ' ;
16
+ protected string $ disk = 'public ' ;
17
17
protected string $ imageableType = __CLASS__ ;
18
18
19
19
public function image (): MorphOne
@@ -26,14 +26,12 @@ public function images(): MorphMany
26
26
return $ this ->morphMany (Image::class, 'imageable ' );
27
27
}
28
28
29
-
30
-
31
29
public function saveImage (UploadedFile $ image , string $ dir = null , ?string $ prefix = null , ?string $ disk = null )
32
30
{
33
- $ dir = $ dir ?? $ this ->getBaseDir ();
34
- $ prefix = $ prefix ?? $ this ->getPrefix ();
35
- $ disk = $ disk ?? $ this ->disk ;
36
- $ path = $ this ->storeOnDisk ($ image , $ dir , $ prefix , $ disk );
31
+ $ dir = $ dir ?? $ this ->getBaseDir ();
32
+ $ prefix = $ prefix ?? $ this ->getPrefix ();
33
+ $ disk = $ disk ?? $ this ->disk ;
34
+ $ path = $ this ->storeOnDisk ($ image , $ dir , $ prefix , $ disk );
37
35
$ imageDto = new ImageDto (
38
36
imageable_id: $ this ->id ,
39
37
imageable_type: $ this ->imageableType ,
@@ -45,22 +43,23 @@ public function saveImage(UploadedFile $image, string $dir = null, ?string $pref
45
43
return $ image ;
46
44
}
47
45
48
- public function updateImage (UploadedFile $ image , string $ path , string $ dir = null , ?string $ prefix = null , ?string $ disk = null )
46
+ public function updateImage (UploadedFile $ image , string | null $ path , string $ dir = null , ?string $ prefix = null , ?string $ disk = null )
49
47
{
50
- $ dir = $ dir ?? $ this ->getBaseDir ();
48
+ $ dir = $ dir ?? $ this ->getBaseDir ();
51
49
$ prefix = $ prefix ?? $ this ->getPrefix ();
52
- $ disk = $ disk ?? $ this ->disk ;
53
- $ this ->deleteImageFromDisk ($ path , $ disk );
54
- $ path = $ this ->storeOnDisk ($ image , $ dir , $ prefix , $ disk );
55
- $ image = $ this ->image ()->update ([
56
- 'path ' => $ path ,
57
- 'url ' => asset ('storage/ ' . $ path ),
58
- 'mime_type ' => $ image ->extension ()
59
- ]);
60
- return $ image ;
50
+ $ disk = $ disk ?? $ this ->disk ;
51
+ if ($ this ->image ) {
52
+ $ this ->deleteImageFromDisk ($ path , $ disk );
53
+ $ path = $ this ->storeOnDisk ($ image , $ dir , $ prefix , $ disk );
54
+ $ image = $ this ->image ()->update ([
55
+ 'path ' => $ path ,
56
+ 'url ' => asset ('storage/ ' . $ path ),
57
+ 'mime_type ' => $ image ->extension (),
58
+ ]);
59
+ }
60
+ return true ;
61
61
}
62
62
63
-
64
63
public function deleteImageFromDisk (string $ path , ?string $ disk = null )
65
64
{
66
65
$ disk = $ disk ?? $ this ->disk ;
@@ -75,7 +74,6 @@ public function deleteImage(string $path, ?string $disk = null): void
75
74
$ this ->image ()->delete ();
76
75
}
77
76
78
-
79
77
protected function getTimestamp (): string
80
78
{
81
79
return Carbon::now ()->format ('Y-m-d-H-m-s-u ' );
@@ -88,7 +86,7 @@ protected function storeOnDisk(UploadedFile $file, string $dir, string $prefix,
88
86
89
87
protected function getName (UploadedFile $ file ): string
90
88
{
91
- $ ext = ". " . $ file ->extension ();
89
+ $ ext = ". " . $ file ->extension ();
92
90
$ name = $ this ->getPrefix () . "- " . $ this ->getTimestamp () . $ ext ;
93
91
return $ name ;
94
92
}
@@ -101,7 +99,7 @@ protected function storeOnDatabase(ImageDto $dto): Image
101
99
'path ' => $ dto ->path ,
102
100
'url ' => $ dto ->url ,
103
101
'mime_type ' => $ dto ->mime_type ,
104
- ]);
102
+ ]);
105
103
}
106
104
107
105
/**
0 commit comments