RemoteAvatarFetch.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public $timeout = 300;
  36. public $maxExceptions = 1;
  37. /**
  38. * Create a new job instance.
  39. *
  40. * @return void
  41. */
  42. public function __construct(Profile $profile)
  43. {
  44. $this->profile = $profile;
  45. }
  46. /**
  47. * Execute the job.
  48. *
  49. * @return void
  50. */
  51. public function handle()
  52. {
  53. $profile = $this->profile;
  54. if(boolval(config_cache('pixelfed.cloud_storage')) == false && boolval(config_cache('federation.avatars.store_local')) == false) {
  55. return 1;
  56. }
  57. if($profile->domain == null || $profile->private_key) {
  58. return 1;
  59. }
  60. $avatar = Avatar::whereProfileId($profile->id)->first();
  61. if(!$avatar) {
  62. $avatar = new Avatar;
  63. $avatar->profile_id = $profile->id;
  64. $avatar->save();
  65. }
  66. if($avatar->media_path == null && $avatar->remote_url == null) {
  67. $avatar->media_path = 'public/avatars/default.jpg';
  68. $avatar->is_remote = true;
  69. $avatar->save();
  70. }
  71. $person = Helpers::fetchFromUrl($profile->remote_url);
  72. if(!$person || !isset($person['@context'])) {
  73. return 1;
  74. }
  75. if( !isset($person['icon']) ||
  76. !isset($person['icon']['type']) ||
  77. !isset($person['icon']['url'])
  78. ) {
  79. return 1;
  80. }
  81. if($person['icon']['type'] !== 'Image') {
  82. return 1;
  83. }
  84. if(!Helpers::validateUrl($person['icon']['url'])) {
  85. return 1;
  86. }
  87. $icon = $person['icon'];
  88. $avatar->remote_url = $icon['url'];
  89. $avatar->save();
  90. MediaStorageService::avatar($avatar, boolval(config_cache('pixelfed.cloud_storage')) == false);
  91. return 1;
  92. }
  93. }