1
0

RemoteAvatarFetchFromUrl.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Cache;
  15. use Storage;
  16. use Log;
  17. use Illuminate\Http\File;
  18. use App\Services\AccountService;
  19. use App\Services\MediaStorageService;
  20. use App\Services\ActivityPubFetchService;
  21. class RemoteAvatarFetchFromUrl implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $profile;
  25. protected $url;
  26. /**
  27. * Delete the job if its models no longer exist.
  28. *
  29. * @var bool
  30. */
  31. public $deleteWhenMissingModels = true;
  32. /**
  33. * The number of times the job may be attempted.
  34. *
  35. * @var int
  36. */
  37. public $tries = 1;
  38. public $timeout = 300;
  39. public $maxExceptions = 1;
  40. /**
  41. * Create a new job instance.
  42. *
  43. * @return void
  44. */
  45. public function __construct(Profile $profile, $url)
  46. {
  47. $this->profile = $profile;
  48. $this->url = $url;
  49. }
  50. /**
  51. * Execute the job.
  52. *
  53. * @return void
  54. */
  55. public function handle()
  56. {
  57. $profile = $this->profile;
  58. Cache::forget('avatar:' . $profile->id);
  59. AccountService::del($profile->id);
  60. if(boolval(config_cache('pixelfed.cloud_storage')) == false && boolval(config_cache('federation.avatars.store_local')) == false) {
  61. return 1;
  62. }
  63. if($profile->domain == null || $profile->private_key) {
  64. return 1;
  65. }
  66. $avatar = Avatar::whereProfileId($profile->id)->first();
  67. if(!$avatar) {
  68. $avatar = new Avatar;
  69. $avatar->profile_id = $profile->id;
  70. $avatar->is_remote = true;
  71. $avatar->remote_url = $this->url;
  72. $avatar->save();
  73. } else {
  74. $avatar->remote_url = $this->url;
  75. $avatar->is_remote = true;
  76. $avatar->save();
  77. }
  78. MediaStorageService::avatar($avatar, boolval(config_cache('pixelfed.cloud_storage')) == false, true);
  79. return 1;
  80. }
  81. }