1
0

MediaDeletePipeline.php 2.6 KB

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