Skip to content

Commit 1777032

Browse files
authored
fix(user): delete avatar file from disk when user account is deleted (#4485)
AvatarUploader::remove() was never called during user deletion, leaving avatar files as orphans on the flarum-avatars disk. Every deleted user with a custom avatar accumulated a stranded file indefinitely. Hook into the existing deleting observer (pre-delete, so the avatar_url attribute is still readable) and delete the file directly from the filesystem. Uses getRawOriginal() to get the stored path rather than the accessor, which would return a full URL. Fixes #4459
1 parent 30943d7 commit 1777032

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

framework/core/src/User/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ public static function boot()
151151
if ($user->id == 1) {
152152
throw new DomainException('Cannot delete the root admin');
153153
}
154+
155+
$avatarPath = $user->getRawOriginal('avatar_url');
156+
157+
if ($avatarPath) {
158+
$disk = resolve(Factory::class)->disk('flarum-avatars');
159+
160+
if ($disk->exists($avatarPath)) {
161+
$disk->delete($avatarPath);
162+
}
163+
}
154164
});
155165

156166
static::deleted(function (self $user) {

0 commit comments

Comments
 (0)