44namespace Imahmood \FileStorage ;
55
66use Illuminate \Http \UploadedFile ;
7- use Illuminate \Support \Facades \Bus ;
87use Illuminate \Support \Facades \DB ;
9- use Illuminate \Support \Facades \Storage ;
10- use Imahmood \FileStorage \Config \Configuration ;
118use Imahmood \FileStorage \Contracts \MediaAwareInterface ;
129use Imahmood \FileStorage \Contracts \MediaTypeInterface ;
10+ use Imahmood \FileStorage \Contracts \NameGeneratorInterface ;
1311use Imahmood \FileStorage \Events \AfterMediaSaved ;
1412use Imahmood \FileStorage \Events \AfterMediaUploaded ;
15- use Imahmood \FileStorage \Exceptions \DeleteDirectoryException ;
16- use Imahmood \FileStorage \Exceptions \DeleteFileException ;
1713use Imahmood \FileStorage \Exceptions \PersistenceFailedException ;
1814use Imahmood \FileStorage \Exceptions \UploadException ;
19- use Imahmood \FileStorage \Jobs \GeneratePreview ;
20- use Imahmood \FileStorage \Jobs \OptimizeImage ;
15+ use Imahmood \FileStorage \Jobs \RunModifiersJob ;
2116use Imahmood \FileStorage \Models \Media ;
17+ use Imahmood \FileStorage \Utility \Filesystem ;
2218
2319class FileStorage
2420{
25- protected ?string $ disk = null ;
21+ protected string $ diskName ;
22+
23+ protected string $ queueName ;
24+
25+ protected bool $ queueModifiers ;
2626
2727 public function __construct (
28- protected readonly Configuration $ config ,
28+ protected readonly Filesystem $ filesystem ,
29+ protected readonly Manipulator $ manipulator ,
30+ protected readonly NameGeneratorInterface $ nameGenerator ,
2931 ) {
32+ $ this ->diskName = config ('file-storage.disk ' );
33+ $ this ->queueName = config ('file-storage.queue ' );
34+ $ this ->queueModifiers = config ('file-storage.queue_modifiers ' );
3035 }
3136
3237 /**
3338 * Specify the storage disk.
3439 */
3540 public function onDisk (string $ disk ): static
3641 {
37- $ this ->disk = $ disk ;
42+ $ this ->diskName = $ disk ;
3843
3944 return $ this ;
4045 }
@@ -48,7 +53,7 @@ public function create(
4853 UploadedFile $ uploadedFile ,
4954 ): Media {
5055 $ media = new Media ([
51- 'disk ' => $ this ->disk ?? $ this -> config -> diskName ,
56+ 'disk ' => $ this ->diskName ,
5257 'model_type ' => $ relatedTo ? $ relatedTo ::class : null ,
5358 'model_id ' => $ relatedTo ?->getPrimaryKey(),
5459 'type ' => $ type ->identifier (),
@@ -82,7 +87,7 @@ public function update(
8287 $ media = $ this ->persistMedia ($ media , $ uploadedFile );
8388
8489 if ($ uploadedFile ) {
85- $ this ->deleteFile ($ media ->disk , $ originalPaths );
90+ $ this ->filesystem -> deleteFile ($ media ->disk , $ originalPaths );
8691 }
8792
8893 return $ media ;
@@ -114,7 +119,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
114119 {
115120 return DB ::transaction (function () use ($ media , $ uploadedFile ) {
116121 if ($ uploadedFile ) {
117- $ media ->file_name = $ uploadedFile ->getClientOriginalName ();
122+ $ media ->file_name = $ this -> nameGenerator -> fileName ( $ uploadedFile ->getClientOriginalName () );
118123 $ media ->preview = null ;
119124 }
120125
@@ -131,7 +136,11 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
131136 throw new UploadException ();
132137 }
133138
134- $ this ->dispatchJobs ($ media );
139+ if ($ this ->queueModifiers ) {
140+ RunModifiersJob::dispatch ($ media )->onQueue ($ this ->queueName );
141+ } else {
142+ $ media = $ this ->manipulator ->applyModifiers ($ media );
143+ }
135144
136145 AfterMediaUploaded::dispatch ($ media );
137146 }
@@ -142,26 +151,6 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
142151 });
143152 }
144153
145- /**
146- * Dispatches jobs for optimizing and generating preview.
147- */
148- protected function dispatchJobs (Media $ media ): void
149- {
150- $ jobs = [];
151-
152- if ($ media ->is_image ) {
153- $ jobs [] = new OptimizeImage ($ media );
154- }
155-
156- if ($ this ->config ->generatePreview && ($ media ->is_image || $ media ->is_pdf )) {
157- $ jobs [] = new GeneratePreview ($ media );
158- }
159-
160- if ($ jobs ) {
161- Bus::chain ($ jobs )->onQueue ($ this ->config ->queueName )->dispatch ();
162- }
163- }
164-
165154 /**
166155 * @throws \Imahmood\FileStorage\Exceptions\DeleteDirectoryException
167156 */
@@ -172,41 +161,9 @@ public function delete(Media $media): bool
172161 return false ;
173162 }
174163
175- $ this ->deleteDirectory ($ media ->disk , $ media ->dir_relative_path );
164+ $ this ->filesystem -> deleteDirectory ($ media ->disk , $ media ->dir_relative_path );
176165
177166 return true ;
178167 });
179168 }
180-
181- /**
182- * @throws \Imahmood\FileStorage\Exceptions\DeleteDirectoryException
183- */
184- protected function deleteDirectory (string $ disk , string $ dir ): void
185- {
186- $ isDeleted = Storage::disk ($ disk )->deleteDirectory ($ dir );
187- if (! $ isDeleted ) {
188- throw new DeleteDirectoryException (sprintf (
189- '[FileStorage] Disk: %s, Directory: %s ' ,
190- $ disk ,
191- $ dir ,
192- ));
193- }
194- }
195-
196- /**
197- * @throws \Imahmood\FileStorage\Exceptions\DeleteFileException
198- */
199- protected function deleteFile (string $ disk , array |string $ paths ): void
200- {
201- $ isDeleted = Storage::disk ($ disk )->delete ($ paths );
202- if (! $ isDeleted ) {
203- $ paths = is_array ($ paths ) ? implode (', ' , $ paths ) : $ paths ;
204-
205- throw new DeleteFileException (sprintf (
206- '[FileStorage] Disk: %s, Paths: %s ' ,
207- $ disk ,
208- $ paths ,
209- ));
210- }
211- }
212169}
0 commit comments