2021-02-06 04:00:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\MediaPipeline;
|
|
|
|
|
|
|
|
use App\Media;
|
2024-03-12 07:03:33 +00:00
|
|
|
use App\Services\Media\MediaHlsService;
|
2021-02-06 04:00:29 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2024-03-12 07:03:33 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
|
2021-02-06 04:00:29 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2024-03-12 07:03:33 +00:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2021-02-06 04:00:29 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
class MediaDeletePipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
|
2021-02-06 04:00:29 +00:00
|
|
|
{
|
2024-03-12 07:03:33 +00:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
protected $media;
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2022-12-24 09:01:30 +00:00
|
|
|
public $timeout = 300;
|
2024-03-12 07:03:33 +00:00
|
|
|
|
2022-12-24 09:01:30 +00:00
|
|
|
public $tries = 3;
|
2024-03-12 07:03:33 +00:00
|
|
|
|
2022-12-24 09:01:30 +00:00
|
|
|
public $maxExceptions = 1;
|
2024-03-12 07:03:33 +00:00
|
|
|
|
2023-10-23 07:09:16 +00:00
|
|
|
public $failOnTimeout = true;
|
2024-03-12 07:03:33 +00:00
|
|
|
|
2022-12-24 11:30:02 +00:00
|
|
|
public $deleteWhenMissingModels = true;
|
2022-12-24 09:01:30 +00:00
|
|
|
|
2023-10-23 07:09:16 +00:00
|
|
|
/**
|
|
|
|
* The number of seconds after which the job's unique lock will be released.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $uniqueFor = 3600;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the unique ID for the job.
|
|
|
|
*/
|
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
2024-03-12 07:03:33 +00:00
|
|
|
return 'media:purge-job:id-'.$this->media->id;
|
2023-10-23 07:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the middleware the job should pass through.
|
|
|
|
*
|
|
|
|
* @return array<int, object>
|
|
|
|
*/
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
|
|
|
return [(new WithoutOverlapping("media:purge-job:id-{$this->media->id}"))->shared()->dontRelease()];
|
|
|
|
}
|
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
public function __construct(Media $media)
|
|
|
|
{
|
|
|
|
$this->media = $media;
|
|
|
|
}
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$media = $this->media;
|
|
|
|
$path = $media->media_path;
|
|
|
|
$thumb = $media->thumbnail_path;
|
2021-05-19 04:45:04 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if (! $path) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
$e = explode('/', $path);
|
|
|
|
array_pop($e);
|
|
|
|
$i = implode('/', $e);
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ((bool) config_cache('pixelfed.cloud_storage') == true) {
|
|
|
|
$disk = Storage::disk(config('filesystems.cloud'));
|
2022-11-14 08:47:53 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($path && $disk->exists($path)) {
|
|
|
|
$disk->delete($path);
|
|
|
|
}
|
2022-11-14 08:47:53 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($thumb && $disk->exists($thumb)) {
|
|
|
|
$disk->delete($thumb);
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
$disk = Storage::disk(config('filesystems.local'));
|
2022-12-24 11:30:02 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($path && $disk->exists($path)) {
|
|
|
|
$disk->delete($path);
|
|
|
|
}
|
2022-12-24 11:30:02 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($thumb && $disk->exists($thumb)) {
|
|
|
|
$disk->delete($thumb);
|
|
|
|
}
|
2021-02-06 04:00:29 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($media->hls_path != null) {
|
2023-10-23 07:09:16 +00:00
|
|
|
$files = MediaHlsService::allFiles($media);
|
2024-03-12 07:03:33 +00:00
|
|
|
if ($files && count($files)) {
|
|
|
|
foreach ($files as $file) {
|
2023-10-23 07:09:16 +00:00
|
|
|
$disk->delete($file);
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 07:03:33 +00:00
|
|
|
}
|
2023-10-23 07:09:16 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
$media->delete();
|
2022-06-19 12:52:06 +00:00
|
|
|
|
2024-03-12 07:03:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-05-19 04:45:04 +00:00
|
|
|
}
|