5
5
use Elegantly \Media \Models \Media ;
6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Database \Eloquent \Builder ;
8
- use Illuminate \ Database \ Eloquent \ Collection ;
8
+ use Laravel \ Prompts \ Progress ;
9
9
10
10
use function Laravel \Prompts \confirm ;
11
11
@@ -22,50 +22,59 @@ public function handle(): int
22
22
$ pretend = (bool ) $ this ->option ('pretend ' );
23
23
/** @var string[] $conversions */
24
24
$ conversions = (array ) $ this ->option ('conversions ' );
25
+ /** @var string[] $models */
25
26
$ models = (array ) $ this ->option ('models ' );
27
+ /** @var string[] $collections */
26
28
$ collections = (array ) $ this ->option ('collections ' );
27
29
28
30
/**
29
31
* @var class-string<Media> $model
30
32
*/
31
33
$ model = config ('media.model ' );
32
34
33
- /** @var Collection<int, Media> */
34
- $ media = $ model ::query ()
35
+ $ query = $ model ::query ()
35
36
->with (['model ' , 'conversions ' ])
36
37
->when (! empty ($ ids ), fn (Builder $ query ) => $ query ->whereIn ('id ' , $ ids ))
37
38
->when (! empty ($ models ), fn (Builder $ query ) => $ query ->whereIn ('model_type ' , $ models ))
38
- ->when (! empty ($ collections ), fn (Builder $ query ) => $ query ->whereIn ('collection_name ' , $ collections ))
39
- ->get ();
40
-
41
- $ mediaByModel = $ media ->countBy ('model_type ' );
42
-
43
- $ this ->table (
44
- ['Model ' , 'Count ' ],
45
- $ mediaByModel ->map (function (int $ count , ?string $ model_type ) {
46
- return [
47
- $ model_type ,
48
- $ count ,
49
- ];
50
- })
51
- );
52
-
53
- if ($ pretend || ! confirm ('Continue? ' )) {
39
+ ->when (! empty ($ collections ), fn (Builder $ query ) => $ query ->whereIn ('collection_name ' , $ collections ));
40
+
41
+ $ count = $ query ->count ();
42
+
43
+ if ($ pretend || ! confirm ("{$ count } Media found. Continue? " )) {
54
44
return self ::SUCCESS ;
55
45
}
56
46
57
- $ this ->withProgressBar ($ media , function (Media $ media ) use ($ conversions , $ force ) {
47
+ $ progress = new Progress ('Dispatching Media conversions ' , $ count );
48
+
49
+ $ query ->chunkById (5_000 , function ($ items ) use ($ progress , $ force , $ conversions ) {
58
50
59
- $ conversions = empty ( $ conversions ) ? array_keys ( $ media-> getConversionsDefinitions ()) : $ conversions ;
51
+ foreach ( $ items as $ media) {
60
52
61
- foreach ($ conversions as $ name ) {
62
- $ conversion = $ media ->getConversion ((string ) $ name );
53
+ $ media ->dispatchConversions (
54
+ queued: true ,
55
+ filter: function ($ definition ) use ($ media , $ force , $ conversions ) {
63
56
64
- if ($ force || ! $ conversion ) {
65
- $ media ->dispatchConversion ($ name );
66
- }
57
+ if (
58
+ ! empty ($ conversions ) &&
59
+ ! in_array ($ definition ->name , $ conversions )
60
+ ) {
61
+ return false ;
62
+ }
67
63
64
+ if (
65
+ ! $ force &&
66
+ $ media ->hasConversion ($ definition ->name )
67
+ ) {
68
+ return false ;
69
+ }
70
+
71
+ return true ;
72
+ }
73
+ );
74
+
75
+ $ progress ->advance ();
68
76
}
77
+
69
78
});
70
79
71
80
return self ::SUCCESS ;
0 commit comments