1
1
<?php
2
2
3
- declare (strict_types=1 );
4
-
5
3
namespace BookStack \Console \Commands ;
6
4
7
5
use BookStack \Users \Models \User ;
6
+ use Exception ;
8
7
use Illuminate \Console \Command ;
9
8
use BookStack \Uploads \UserAvatars ;
10
- use Illuminate \Database \Eloquent \Collection ;
11
9
12
- final class RefreshAvatarCommand extends Command
10
+ class RefreshAvatarCommand extends Command
13
11
{
12
+ use HandlesSingleUser;
13
+
14
14
/**
15
15
* The name and signature of the console command.
16
16
*
@@ -20,128 +20,89 @@ final class RefreshAvatarCommand extends Command
20
20
{--id= : Numeric ID of the user to refresh avatar for}
21
21
{--email= : Email address of the user to refresh avatar for}
22
22
{--users-without-avatars : Refresh avatars for users that currently have no avatar}
23
- {--a|all : Refresh all user avatars }
24
- {--f|force : Actually run the update for --users-without-avatars , Defaults to a dry-run} ' ;
23
+ {--a|all : Refresh avatars for all users }
24
+ {--f|force : Actually run the update, Defaults to a dry-run} ' ;
25
25
26
26
/**
27
27
* The console command description.
28
28
*
29
29
* @var string
30
30
*/
31
- protected $ description = 'Refresh avatar for given user or users ' ;
31
+ protected $ description = 'Refresh avatar for the given user(s) ' ;
32
32
33
33
public function handle (UserAvatars $ userAvatar ): int
34
34
{
35
- $ dryRun = !$ this ->option ('force ' );
35
+ if (!$ userAvatar ->avatarFetchEnabled ()) {
36
+ $ this ->error ("Avatar fetching is disabled on this instance. " );
37
+ return self ::FAILURE ;
38
+ }
36
39
37
40
if ($ this ->option ('users-without-avatars ' )) {
38
- return $ this ->handleUpdateWithoutAvatars ( $ userAvatar , $ dryRun );
41
+ return $ this ->processUsers (User:: query ()-> whereDoesntHave ( ' avatar ' )-> get ()-> all () , $ userAvatar );
39
42
}
40
43
41
44
if ($ this ->option ('all ' )) {
42
- return $ this ->handleUpdateAllAvatars ( $ userAvatar , $ dryRun );
45
+ return $ this ->processUsers (User:: query ()-> get ()-> all () , $ userAvatar );
43
46
}
44
47
45
- return $ this ->handleSingleUserUpdate ($ userAvatar );
48
+ try {
49
+ $ user = $ this ->fetchProvidedUser ();
50
+ return $ this ->processUsers ([$ user ], $ userAvatar );
51
+ } catch (Exception $ exception ) {
52
+ $ this ->error ($ exception ->getMessage ());
53
+ return self ::FAILURE ;
54
+ }
46
55
}
47
56
48
- private function handleUpdateWithoutAvatars (UserAvatars $ userAvatar , bool $ dryRun ): int
57
+ /**
58
+ * @param User[] $users
59
+ */
60
+ private function processUsers (array $ users , UserAvatars $ userAvatar ): int
49
61
{
50
- $ users = User:: query ()-> where ( ' image_id ' , ' = ' , 0 )-> get ( );
51
- $ this ->info (count ($ users ) . ' user(s) found without avatars. ' );
62
+ $ dryRun = ! $ this -> option ( ' force ' );
63
+ $ this ->info (count ($ users ) . " user(s) found to update avatars for. " );
52
64
53
- if (!$ dryRun ) {
54
- $ proceed = !$ this ->input ->isInteractive () || $ this ->confirm ('Are you sure you want to refresh avatars of users that do not have one? ' );
55
- if (!$ proceed ) {
56
- return self ::SUCCESS ;
57
- }
65
+ if (count ($ users ) === 0 ) {
66
+ return self ::SUCCESS ;
58
67
}
59
68
60
- return $ this ->processUsers ($ users , $ userAvatar , $ dryRun );
61
- }
62
-
63
- private function handleUpdateAllAvatars (UserAvatars $ userAvatar , bool $ dryRun ): int
64
- {
65
- $ users = User::query ()->get ();
66
- $ this ->info (count ($ users ) . ' user(s) found. ' );
67
-
68
69
if (!$ dryRun ) {
69
- $ proceed = !$ this ->input ->isInteractive () || $ this ->confirm ('Are you sure you want to refresh avatars for ALL USERS? ' );
70
+ $ fetchHost = parse_url ($ userAvatar ->getAvatarUrl (), PHP_URL_HOST );
71
+ $ this ->warn ("This will destroy any existing avatar images these users have, and attempt to fetch new avatar images from {$ fetchHost }. " );
72
+ $ proceed = !$ this ->input ->isInteractive () || $ this ->confirm ('Are you sure you want to proceed? ' );
70
73
if (!$ proceed ) {
71
74
return self ::SUCCESS ;
72
75
}
73
76
}
74
77
75
- return $ this ->processUsers ($ users , $ userAvatar , $ dryRun );
76
- }
78
+ $ this ->info ("" );
77
79
78
- private function processUsers (Collection $ users , UserAvatars $ userAvatar , bool $ dryRun ): int
79
- {
80
80
$ exitCode = self ::SUCCESS ;
81
81
foreach ($ users as $ user ) {
82
- $ this -> getOutput ()-> write ( " ID {$ user ->id } - " , false ) ;
82
+ $ linePrefix = " [ID: {$ user ->id }] $ user -> email - " ;
83
83
84
84
if ($ dryRun ) {
85
- $ this ->warn (' Not updated ' );
85
+ $ this ->warn ("{ $ linePrefix } Not updated" );
86
86
continue ;
87
87
}
88
88
89
89
if ($ this ->fetchAvatar ($ userAvatar , $ user )) {
90
- $ this ->info (' Updated ' );
90
+ $ this ->info ("{ $ linePrefix } Updated" );
91
91
} else {
92
- $ this ->error (' Not updated ' );
92
+ $ this ->error ("{ $ linePrefix } Not updated" );
93
93
$ exitCode = self ::FAILURE ;
94
94
}
95
95
}
96
96
97
- $ this ->getOutput ()->newLine ();
98
97
if ($ dryRun ) {
99
- $ this ->comment ('Dry run, no avatars have been updated ' );
100
- $ this ->comment ('Run with -f or --force to perform the update ' );
98
+ $ this ->comment ("" );
99
+ $ this ->comment ("Dry run, no avatars were updated. " );
100
+ $ this ->comment ('Run with -f or --force to perform the update. ' );
101
101
}
102
102
103
103
return $ exitCode ;
104
104
}
105
105
106
-
107
- private function handleSingleUserUpdate (UserAvatars $ userAvatar ): int
108
- {
109
- $ id = $ this ->option ('id ' );
110
- $ email = $ this ->option ('email ' );
111
- if (!$ id && !$ email ) {
112
- $ this ->error ('Either a --id=<number> or --email=<email> option must be provided. ' );
113
- $ this ->error ('Run with `--help` to more options ' );
114
-
115
- return self ::FAILURE ;
116
- }
117
-
118
- $ field = $ id ? 'id ' : 'email ' ;
119
- $ value = $ id ?: $ email ;
120
-
121
- $ user = User::query ()
122
- ->where ($ field , '= ' , $ value )
123
- ->first ();
124
-
125
- if (!$ user ) {
126
- $ this ->error ("A user where {$ field }= {$ value } could not be found. " );
127
-
128
- return self ::FAILURE ;
129
- }
130
-
131
- $ this ->info ("This will refresh the avatar for user: \n- ID: {$ user ->id }\n- Name: {$ user ->name }\n- Email: {$ user ->email }\n" );
132
- $ confirm = $ this ->confirm ('Are you sure you want to proceed? ' );
133
- if ($ confirm ) {
134
- if ($ this ->fetchAvatar ($ userAvatar , $ user )) {
135
- $ this ->info ('User avatar has been updated. ' );
136
- return self ::SUCCESS ;
137
- }
138
-
139
- $ this ->info ('Could not update avatar please review logs. ' );
140
- }
141
-
142
- return self ::FAILURE ;
143
- }
144
-
145
106
private function fetchAvatar (UserAvatars $ userAvatar , User $ user ): bool
146
107
{
147
108
$ oldId = $ user ->avatar ->id ?? 0 ;
0 commit comments