AvatarService.php 3.1 KB

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