pixelfed/app/Jobs/MediaPipeline/MediaDeletePipeline.php

112 wiersze
2.6 KiB
PHP
Czysty Zwykły widok Historia

<?php
namespace App\Jobs\MediaPipeline;
use App\Media;
2024-03-12 07:03:33 +00:00
use App\Services\Media\MediaHlsService;
use Illuminate\Bus\Queueable;
2024-03-12 07:03:33 +00:00
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
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;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
2024-03-12 07:03:33 +00:00
class MediaDeletePipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
{
2024-03-12 07:03:33 +00:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2024-03-12 07:03:33 +00:00
protected $media;
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
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
/**
* 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;
}
/**
* 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;
}
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;
}
2024-03-12 07:03:33 +00:00
$e = explode('/', $path);
array_pop($e);
$i = implode('/', $e);
2024-03-12 07:03:33 +00:00
if ((bool) config_cache('pixelfed.cloud_storage') == true) {
$disk = Storage::disk(config('filesystems.cloud'));
2024-03-12 07:03:33 +00:00
if ($path && $disk->exists($path)) {
$disk->delete($path);
}
2024-03-12 07:03:33 +00:00
if ($thumb && $disk->exists($thumb)) {
$disk->delete($thumb);
}
}
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);
}
2024-03-12 07:03:33 +00:00
if ($media->hls_path != null) {
$files = MediaHlsService::allFiles($media);
2024-03-12 07:03:33 +00:00
if ($files && count($files)) {
foreach ($files as $file) {
$disk->delete($file);
}
}
2024-03-12 07:03:33 +00:00
}
2024-03-12 07:03:33 +00:00
$media->delete();
2024-03-12 07:03:33 +00:00
return 1;
}
2021-05-19 04:45:04 +00:00
}