|
3 | 3 | namespace App\Services;
|
4 | 4 |
|
5 | 5 | use Cache;
|
| 6 | +use Storage; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use App\Avatar; |
6 | 9 | use App\Profile;
|
| 10 | +use App\Jobs\AvatarPipeline\AvatarStorageLargePurge; |
| 11 | +use League\Flysystem\UnableToCheckDirectoryExistence; |
| 12 | +use League\Flysystem\UnableToRetrieveMetadata; |
7 | 13 |
|
8 | 14 | class AvatarService
|
9 | 15 | {
|
10 |
| - public static function get($profile_id) |
11 |
| - { |
12 |
| - $exists = Cache::get('avatar:' . $profile_id); |
13 |
| - if($exists) { |
14 |
| - return $exists; |
15 |
| - } |
16 |
| - |
17 |
| - $profile = Profile::find($profile_id); |
18 |
| - if(!$profile) { |
19 |
| - return config('app.url') . '/storage/avatars/default.jpg'; |
20 |
| - } |
21 |
| - return $profile->avatarUrl(); |
22 |
| - } |
| 16 | + public static function get($profile_id) |
| 17 | + { |
| 18 | + $exists = Cache::get('avatar:' . $profile_id); |
| 19 | + if($exists) { |
| 20 | + return $exists; |
| 21 | + } |
| 22 | + |
| 23 | + $profile = Profile::find($profile_id); |
| 24 | + if(!$profile) { |
| 25 | + return config('app.url') . '/storage/avatars/default.jpg'; |
| 26 | + } |
| 27 | + return $profile->avatarUrl(); |
| 28 | + } |
| 29 | + |
| 30 | + public static function disk() |
| 31 | + { |
| 32 | + $storage = [ |
| 33 | + 'cloud' => boolval(config_cache('pixelfed.cloud_storage')), |
| 34 | + 'local' => boolval(config_cache('federation.avatars.store_local')) |
| 35 | + ]; |
| 36 | + |
| 37 | + if(!$storage['cloud'] && !$storage['local']) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + $driver = $storage['cloud'] == false ? 'local' : config('filesystems.cloud'); |
| 42 | + $disk = Storage::disk($driver); |
| 43 | + |
| 44 | + return $disk; |
| 45 | + } |
| 46 | + |
| 47 | + public static function storage(Avatar $avatar) |
| 48 | + { |
| 49 | + $disk = self::disk(); |
| 50 | + |
| 51 | + if(!$disk) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $storage = [ |
| 56 | + 'cloud' => boolval(config_cache('pixelfed.cloud_storage')), |
| 57 | + 'local' => boolval(config_cache('federation.avatars.store_local')) |
| 58 | + ]; |
| 59 | + |
| 60 | + $base = ($storage['cloud'] == false ? 'public/cache/' : 'cache/') . 'avatars/'; |
| 61 | + |
| 62 | + return $disk->allFiles($base . $avatar->profile_id); |
| 63 | + } |
| 64 | + |
| 65 | + public static function cleanup($avatar, $confirm = false) |
| 66 | + { |
| 67 | + if(!$avatar || !$confirm) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if($avatar->cdn_url == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $storage = [ |
| 76 | + 'cloud' => boolval(config_cache('pixelfed.cloud_storage')), |
| 77 | + 'local' => boolval(config_cache('federation.avatars.store_local')) |
| 78 | + ]; |
| 79 | + |
| 80 | + if(!$storage['cloud'] && !$storage['local']) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $disk = self::disk(); |
| 85 | + |
| 86 | + if(!$disk) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + $base = ($storage['cloud'] == false ? 'public/cache/' : 'cache/') . 'avatars/'; |
| 91 | + |
| 92 | + try { |
| 93 | + $exists = $disk->directoryExists($base . $avatar->profile_id); |
| 94 | + } catch ( |
| 95 | + UnableToRetrieveMetadata | |
| 96 | + UnableToCheckDirectoryExistence | |
| 97 | + Exception $e |
| 98 | + ) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if(!$exists) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $files = collect($disk->allFiles($base . $avatar->profile_id)); |
| 107 | + |
| 108 | + if(!$files || !$files->count() || $files->count() === 1) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if($files->count() > 5) { |
| 113 | + AvatarStorageLargePurge::dispatch($avatar)->onQueue('mmo'); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $curFile = Str::of($avatar->cdn_url)->explode('/')->last(); |
| 118 | + |
| 119 | + $files = $files->filter(function($f) use($curFile) { |
| 120 | + return !$curFile || !str_ends_with($f, $curFile); |
| 121 | + })->each(function($name) use($disk) { |
| 122 | + $disk->delete($name); |
| 123 | + }); |
| 124 | + |
| 125 | + return; |
| 126 | + } |
23 | 127 | }
|
0 commit comments