RemoteAvatarFetch.php 2.2 KB

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