@@ -12,10 +12,22 @@ public function attachments()
12
12
return $ this ->morphMany (Attachment::class, 'attachmentable ' );
13
13
}
14
14
15
+ /**
16
+ * @return bool
17
+ */
18
+ public function checkIfExists ($ attachments ): bool
19
+ {
20
+ return $ attachments ->exists ();
21
+ }
22
+
23
+ /**
24
+ * @param $files,$dirName
25
+ * @return bool
26
+ */
15
27
public function newAttach ($ file , $ dirName = 'public ' )
16
28
{
17
29
if (isset ($ file )) {
18
- $ fileName = md5 (time ()).'. ' .$ file ->extension ();
30
+ $ fileName = md5 (time (). rand ( 0 , 1000000 ) ).'. ' .$ file ->extension ();
19
31
Storage::disk ('public ' )->putFileAs (
20
32
$ dirName ,
21
33
$ file ,
@@ -29,10 +41,41 @@ public function newAttach($file, $dirName = 'public')
29
41
}
30
42
}
31
43
44
+ /**
45
+ * @param $files
46
+ * @return bool
47
+ */
32
48
public function newAttachs ($ files , $ dirName = 'public ' )
33
49
{
34
50
foreach ($ files as $ file ) {
35
51
$ this ->newAttach ($ file , $ dirName );
36
52
}
37
53
}
54
+
55
+ /**
56
+ * @return array
57
+ */
58
+ public function getAttachs ()
59
+ {
60
+ if ($ this ->checkIfExists ($ this ->attachments ())) {
61
+ return $ this ->attachments ()->pluck ('name ' );
62
+ };
63
+ }
64
+
65
+ /**
66
+ * @param $dirName string
67
+ * @return bool
68
+ */
69
+ public function deleteAttach ($ dirName = 'public ' )
70
+ {
71
+ $ fileExists = Storage::disk ('public ' )->exists (ltrim ($ dirName ."/ " .$ this ->attachments ()->first ()->name ));
72
+ if (!$ fileExists ) {
73
+ return 'this attachment does not exists in this dir ' .$ dirName ;
74
+ }
75
+ if ($ this ->checkIfExists ($ this ->attachments ())) {
76
+ Storage::disk ('public ' )->delete (ltrim ($ dirName ."/ " .$ this ->attachments ()->first ()->name ));
77
+ $ this ->attachments ()->first ()->delete ();
78
+ return 'attachment has deleted successfuly ' ;
79
+ }
80
+ }
38
81
}
0 commit comments