RemoteAvatarFetch.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Jobs\AvatarPipeline;
  3. use App\Avatar;
  4. use App\Profile;
  5. use App\Services\MediaStorageService;
  6. use App\Util\ActivityPub\Helpers;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. class RemoteAvatarFetch implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $profile;
  16. /**
  17. * Delete the job if its models no longer exist.
  18. *
  19. * @var bool
  20. */
  21. public $deleteWhenMissingModels = true;
  22. /**
  23. * The number of times the job may be attempted.
  24. *
  25. * @var int
  26. */
  27. public $tries = 1;
  28. public $timeout = 300;
  29. public $maxExceptions = 1;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct(Profile $profile)
  36. {
  37. $this->profile = $profile;
  38. }
  39. /**
  40. * Execute the job.
  41. *
  42. * @return void
  43. */
  44. public function handle()
  45. {
  46. $profile = $this->profile;
  47. if ((bool) config_cache('pixelfed.cloud_storage') == false && (bool) config_cache('federation.avatars.store_local') == false) {
  48. return 1;
  49. }
  50. if ($profile->domain == null || $profile->private_key) {
  51. return 1;
  52. }
  53. $avatar = Avatar::whereProfileId($profile->id)->first();
  54. if (! $avatar) {
  55. $avatar = new Avatar;
  56. $avatar->profile_id = $profile->id;
  57. $avatar->save();
  58. }
  59. if ($avatar->media_path == null && $avatar->remote_url == null) {
  60. $avatar->media_path = 'public/avatars/default.jpg';
  61. $avatar->is_remote = true;
  62. $avatar->save();
  63. }
  64. $person = Helpers::fetchFromUrl($profile->remote_url);
  65. if (! $person || ! isset($person['@context'])) {
  66. return 1;
  67. }
  68. if (! isset($person['icon']) ||
  69. ! isset($person['icon']['type']) ||
  70. ! isset($person['icon']['url'])
  71. ) {
  72. return 1;
  73. }
  74. if ($person['icon']['type'] !== 'Image') {
  75. return 1;
  76. }
  77. if (! Helpers::validateUrl($person['icon']['url'])) {
  78. return 1;
  79. }
  80. $icon = $person['icon'];
  81. $avatar->remote_url = $icon['url'];
  82. $avatar->save();
  83. MediaStorageService::avatar($avatar, (bool) config_cache('pixelfed.cloud_storage') == false, true);
  84. return 1;
  85. }
  86. }