mirror of https://github.com/pixelfed/pixelfed
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.7 KiB
PHP
164 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\MediaPipeline;
|
|
|
|
use App\Models\Media;
|
|
use App\Services\Media\MediaHlsService;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class MediaDeletePipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $media;
|
|
|
|
public $timeout = 300;
|
|
|
|
public $tries = 3;
|
|
|
|
public $maxExceptions = 1;
|
|
|
|
public $failOnTimeout = true;
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
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()];
|
|
}
|
|
|
|
public function __construct(Media $media)
|
|
{
|
|
$this->media = $media;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
$media = $this->media;
|
|
|
|
// Verify media exists
|
|
if (! $media) {
|
|
Log::info('MediaDeletePipeline: Media no longer exists, skipping job');
|
|
|
|
return 1;
|
|
}
|
|
|
|
// Verify media is still orphaned before deleting
|
|
if ($media->status_id !== null) {
|
|
Log::info('MediaDeletePipeline: Media is attached to a status, skipping deletion', [
|
|
'media_id' => $media->id,
|
|
'status_id' => $media->status_id,
|
|
'profile_id' => $media->profile_id,
|
|
'user_id' => $media->user_id,
|
|
'mime' => $media->mime,
|
|
'size' => $media->size,
|
|
'order' => $media->order,
|
|
'media_path' => $media->media_path,
|
|
'thumbnail_path' => $media->thumbnail_path,
|
|
'hls_path' => $media->hls_path,
|
|
'remote_media' => (bool) $media->remote_media,
|
|
'created_at' => $media->created_at?->toDateTimeString(),
|
|
'updated_at' => $media->updated_at?->toDateTimeString(),
|
|
]);
|
|
|
|
return 1;
|
|
}
|
|
|
|
$path = $media->media_path;
|
|
$thumb = $media->thumbnail_path;
|
|
|
|
if (! $path) {
|
|
Log::info('MediaDeletePipeline: Media has no path, skipping deletion', [
|
|
'media_id' => $media->id,
|
|
'status_id' => $media->status_id,
|
|
'profile_id' => $media->profile_id,
|
|
'user_id' => $media->user_id,
|
|
'mime' => $media->mime,
|
|
'thumbnail_path' => $media->thumbnail_path,
|
|
'hls_path' => $media->hls_path,
|
|
]);
|
|
|
|
return 1;
|
|
}
|
|
|
|
$e = explode('/', $path);
|
|
array_pop($e);
|
|
$i = implode('/', $e);
|
|
|
|
try {
|
|
if ((bool) config_cache('pixelfed.cloud_storage') == true) {
|
|
$disk = Storage::disk(config('filesystems.cloud'));
|
|
|
|
if ($path && $disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
if ($thumb && $disk->exists($thumb)) {
|
|
$disk->delete($thumb);
|
|
}
|
|
}
|
|
|
|
$disk = Storage::disk(config('filesystems.local'));
|
|
|
|
if ($path && $disk->exists($path)) {
|
|
$disk->delete($path);
|
|
}
|
|
|
|
if ($thumb && $disk->exists($thumb)) {
|
|
$disk->delete($thumb);
|
|
}
|
|
|
|
if ($media->hls_path != null) {
|
|
$files = MediaHlsService::allFiles($media);
|
|
if ($files && count($files)) {
|
|
foreach ($files as $file) {
|
|
$disk->delete($file);
|
|
}
|
|
}
|
|
}
|
|
|
|
$media->delete();
|
|
} catch (\Exception $e) {
|
|
Log::warning('MediaDeletePipeline: Failed to delete media', [
|
|
'media_id' => $media->id,
|
|
'status_id' => $media->status_id,
|
|
'media_path' => $path,
|
|
'thumbnail_path' => $thumb,
|
|
'hls_path' => $media->hls_path,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
throw $e;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
}
|