MediaDeletePipeline.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Jobs\MediaPipeline;
  3. use App\Media;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Storage;
  11. use App\Services\Media\MediaHlsService;
  12. use Illuminate\Queue\Middleware\WithoutOverlapping;
  13. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  14. class MediaDeletePipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $media;
  18. public $timeout = 300;
  19. public $tries = 3;
  20. public $maxExceptions = 1;
  21. public $failOnTimeout = true;
  22. public $deleteWhenMissingModels = true;
  23. /**
  24. * The number of seconds after which the job's unique lock will be released.
  25. *
  26. * @var int
  27. */
  28. public $uniqueFor = 3600;
  29. /**
  30. * Get the unique ID for the job.
  31. */
  32. public function uniqueId(): string
  33. {
  34. return 'media:purge-job:id-' . $this->media->id;
  35. }
  36. /**
  37. * Get the middleware the job should pass through.
  38. *
  39. * @return array<int, object>
  40. */
  41. public function middleware(): array
  42. {
  43. return [(new WithoutOverlapping("media:purge-job:id-{$this->media->id}"))->shared()->dontRelease()];
  44. }
  45. public function __construct(Media $media)
  46. {
  47. $this->media = $media;
  48. }
  49. public function handle()
  50. {
  51. $media = $this->media;
  52. $path = $media->media_path;
  53. $thumb = $media->thumbnail_path;
  54. if(!$path) {
  55. return 1;
  56. }
  57. $e = explode('/', $path);
  58. array_pop($e);
  59. $i = implode('/', $e);
  60. if(config_cache('pixelfed.cloud_storage') == true) {
  61. $disk = Storage::disk(config('filesystems.cloud'));
  62. if($path && $disk->exists($path)) {
  63. $disk->delete($path);
  64. }
  65. if($thumb && $disk->exists($thumb)) {
  66. $disk->delete($thumb);
  67. }
  68. }
  69. $disk = Storage::disk(config('filesystems.local'));
  70. if($path && $disk->exists($path)) {
  71. $disk->delete($path);
  72. }
  73. if($thumb && $disk->exists($thumb)) {
  74. $disk->delete($thumb);
  75. }
  76. if($media->hls_path != null) {
  77. $files = MediaHlsService::allFiles($media);
  78. if($files && count($files)) {
  79. foreach($files as $file) {
  80. $disk->delete($file);
  81. }
  82. }
  83. }
  84. $media->delete();
  85. return 1;
  86. }
  87. }