Skip to content

Commit

Permalink
✨ Clean up unused profile pictures (#2887)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristian Stöckel <[email protected]>
  • Loading branch information
HerrLevin and MrKrisKrisu authored Sep 12, 2024
1 parent c184e41 commit 4298b96
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 19 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ clover.xml

# Profile pictures
public/uploads/avatars/*
!public/uploads/avatars/user.jpg

# vendor-views
!resources/views/vendor
Expand Down
26 changes: 26 additions & 0 deletions app/Console/Commands/CleanUpProfilePictures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class CleanUpProfilePictures extends Command
{

protected $signature = 'app:clean-up-profilepictures';
protected $description = 'Clean up profile pictures that somehow didn\'t get deleted when the user was deleted';

public function handle(): void {
$usedPictures = User::select('avatar')->distinct()->get()->pluck('avatar')->filter()->toArray();

$profilePictures = new \FilesystemIterator(public_path("uploads/avatars"));
foreach ($profilePictures as $profilePicture) {
if (!in_array(basename($profilePicture), $usedPictures)) {
File::delete($profilePicture);
$this->info('Deleted profile picture ' . basename($profilePicture));
}
}
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Console\Commands\CacheLeaderboard;
use App\Console\Commands\CacheYearInReview;
use App\Console\Commands\CleanUpProfilePictures;
use App\Console\Commands\DatabaseCleaner\DatabaseCleaner;
use App\Console\Commands\DatabaseCleaner\MastodonServers;
use App\Console\Commands\HideStatus;
Expand Down Expand Up @@ -42,6 +43,7 @@ protected function schedule(Schedule $schedule): void {

//daily tasks
$schedule->command(DatabaseCleaner::class)->daily();
$schedule->command(CleanUpProfilePictures::class)->daily();

//weekly tasks
$schedule->command(MastodonServers::class)->weekly();
Expand Down
28 changes: 10 additions & 18 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enum\StatusVisibility;
use App\Enum\User\FriendCheckinSetting;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;

class UserFactory extends Factory
Expand Down Expand Up @@ -35,23 +36,14 @@ private function getAvatar(): ?string {
//sometimes we wanna users without avatar - so we can test this case too.
return null;
}
return $this->faker->randomElement([
'stock_146ic.png',
'stock_146me.png',
'stock_218.png',
'stock_424.png',
'stock_avg_et.png',
'stock_cantus.png',
'stock_enno.png',
'stock_eurobahn.png',
'stock_ic2.png',
'stock_ice.png',
'stock_sncf2.png',
'stock_ssb.png',
'stock_uestra.png',
'stock_vy.png',
'stock_wikopf_sandwich.png',
'stock_schienenbus.png',
]);
$image = $this->getAvatarImage();

File::copy($image, public_path('uploads/avatars/' . basename($image)));
return basename($image);
}

private function getAvatarImage(): string {
$files = glob(base_path('tests/static/avatars/*'));
return $files[array_rand($files)];
}
}
Empty file added public/uploads/avatars/.gitkeep
Empty file.
Binary file removed public/uploads/avatars/user.jpg
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 4298b96

Please sign in to comment.