Update media_types, use config_cache

This commit is contained in:
Daniel Supernault 2024-03-12 01:28:08 -06:00
parent fecbe1897b
commit d670de175e
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
3 changed files with 147 additions and 148 deletions

View file

@ -2,11 +2,11 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Media; use App\Media;
use Illuminate\Support\Facades\Http;
use App\Services\MediaService; use App\Services\MediaService;
use App\Services\StatusService; use App\Services\StatusService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class FetchMissingMediaMimeType extends Command class FetchMissingMediaMimeType extends Command
{ {
@ -29,20 +29,20 @@ class FetchMissingMediaMimeType extends Command
*/ */
public function handle() public function handle()
{ {
foreach(Media::whereNotNull(['remote_url', 'status_id'])->whereNull('mime')->lazyByIdDesc(50, 'id') as $media) { foreach (Media::whereNotNull(['remote_url', 'status_id'])->whereNull('mime')->lazyByIdDesc(50, 'id') as $media) {
$res = Http::retry(2, 100, throw: false)->head($media->remote_url); $res = Http::retry(2, 100, throw: false)->head($media->remote_url);
if(!$res->successful()) { if (! $res->successful()) {
continue; continue;
} }
if(!in_array($res->header('content-type'), explode(',',config('pixelfed.media_types')))) { if (! in_array($res->header('content-type'), explode(',', config_cache('pixelfed.media_types')))) {
continue; continue;
} }
$media->mime = $res->header('content-type'); $media->mime = $res->header('content-type');
if($res->hasHeader('content-length')) { if ($res->hasHeader('content-length')) {
$media->size = $res->header('content-length'); $media->size = $res->header('content-length');
} }
@ -50,7 +50,7 @@ class FetchMissingMediaMimeType extends Command
MediaService::del($media->status_id); MediaService::del($media->status_id);
StatusService::del($media->status_id); StatusService::del($media->status_id);
$this->info('mid:'.$media->id . ' (' . $res->header('content-type') . ':' . $res->header('content-length') . ' bytes)'); $this->info('mid:'.$media->id.' ('.$res->header('content-type').':'.$res->header('content-length').' bytes)');
} }
} }
} }

View file

@ -1651,7 +1651,7 @@ class ApiV1Controller extends Controller
'media_attachments' => [ 'media_attachments' => [
'image_matrix_limit' => 16777216, 'image_matrix_limit' => 16777216,
'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024, 'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
'supported_mime_types' => explode(',', config('pixelfed.media_types')), 'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')),
'video_frame_rate_limit' => 120, 'video_frame_rate_limit' => 120,
'video_matrix_limit' => 2304000, 'video_matrix_limit' => 2304000,
'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024, 'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,

View file

@ -2,172 +2,171 @@
namespace App\Jobs\StatusPipeline; namespace App\Jobs\StatusPipeline;
use App\Media;
use App\Models\StatusEdit;
use App\ModLog;
use App\Profile;
use App\Services\StatusService;
use App\Status;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use App\Media;
use App\ModLog;
use App\Profile;
use App\Status;
use App\Models\StatusEdit;
use App\Services\StatusService;
use Purify;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Purify;
class StatusRemoteUpdatePipeline implements ShouldQueue class StatusRemoteUpdatePipeline implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $activity; public $activity;
/** /**
* Create a new job instance. * Create a new job instance.
*/ */
public function __construct($activity) public function __construct($activity)
{ {
$this->activity = $activity; $this->activity = $activity;
} }
/** /**
* Execute the job. * Execute the job.
*/ */
public function handle(): void public function handle(): void
{ {
$activity = $this->activity; $activity = $this->activity;
$status = Status::with('media')->whereObjectUrl($activity['id'])->first(); $status = Status::with('media')->whereObjectUrl($activity['id'])->first();
if(!$status) { if (! $status) {
return; return;
} }
$this->createPreviousEdit($status); $this->createPreviousEdit($status);
$this->updateMedia($status, $activity); $this->updateMedia($status, $activity);
$this->updateImmediateAttributes($status, $activity); $this->updateImmediateAttributes($status, $activity);
$this->createEdit($status, $activity); $this->createEdit($status, $activity);
} }
protected function createPreviousEdit($status) protected function createPreviousEdit($status)
{ {
if(!$status->edits()->count()) { if (! $status->edits()->count()) {
StatusEdit::create([ StatusEdit::create([
'status_id' => $status->id, 'status_id' => $status->id,
'profile_id' => $status->profile_id, 'profile_id' => $status->profile_id,
'caption' => $status->caption, 'caption' => $status->caption,
'spoiler_text' => $status->cw_summary, 'spoiler_text' => $status->cw_summary,
'is_nsfw' => $status->is_nsfw, 'is_nsfw' => $status->is_nsfw,
'ordered_media_attachment_ids' => $status->media()->orderBy('order')->pluck('id')->toArray(), 'ordered_media_attachment_ids' => $status->media()->orderBy('order')->pluck('id')->toArray(),
'created_at' => $status->created_at 'created_at' => $status->created_at,
]); ]);
} }
} }
protected function updateMedia($status, $activity) protected function updateMedia($status, $activity)
{ {
if(!isset($activity['attachment'])) { if (! isset($activity['attachment'])) {
return; return;
} }
$ogm = $status->media->count() ? $status->media()->orderBy('order')->get() : collect([]); $ogm = $status->media->count() ? $status->media()->orderBy('order')->get() : collect([]);
$nm = collect($activity['attachment'])->filter(function($nm) { $nm = collect($activity['attachment'])->filter(function ($nm) {
return isset( return isset(
$nm['type'], $nm['type'],
$nm['mediaType'], $nm['mediaType'],
$nm['url'] $nm['url']
) && ) &&
in_array($nm['type'], ['Document', 'Image', 'Video']) && in_array($nm['type'], ['Document', 'Image', 'Video']) &&
in_array($nm['mediaType'], explode(',', config('pixelfed.media_types'))); in_array($nm['mediaType'], explode(',', config_cache('pixelfed.media_types')));
}); });
// Skip when no media // Skip when no media
if(!$ogm->count() && !$nm->count()) { if (! $ogm->count() && ! $nm->count()) {
return; return;
} }
Media::whereProfileId($status->profile_id) Media::whereProfileId($status->profile_id)
->whereStatusId($status->id) ->whereStatusId($status->id)
->update([ ->update([
'status_id' => null 'status_id' => null,
]); ]);
$nm->each(function($n, $key) use($status) { $nm->each(function ($n, $key) use ($status) {
$res = Http::withOptions(['allow_redirects' => false])->retry(3, 100, throw: false)->head($n['url']); $res = Http::withOptions(['allow_redirects' => false])->retry(3, 100, throw: false)->head($n['url']);
if(!$res->successful()) { if (! $res->successful()) {
return; return;
} }
if(!in_array($res->header('content-type'), explode(',',config('pixelfed.media_types')))) { if (! in_array($res->header('content-type'), explode(',', config_cache('pixelfed.media_types')))) {
return; return;
} }
$m = new Media; $m = new Media;
$m->status_id = $status->id; $m->status_id = $status->id;
$m->profile_id = $status->profile_id; $m->profile_id = $status->profile_id;
$m->remote_media = true; $m->remote_media = true;
$m->media_path = $n['url']; $m->media_path = $n['url'];
$m->mime = $res->header('content-type'); $m->mime = $res->header('content-type');
$m->size = $res->hasHeader('content-length') ? $res->header('content-length') : null; $m->size = $res->hasHeader('content-length') ? $res->header('content-length') : null;
$m->caption = isset($n['name']) && !empty($n['name']) ? Purify::clean($n['name']) : null; $m->caption = isset($n['name']) && ! empty($n['name']) ? Purify::clean($n['name']) : null;
$m->remote_url = $n['url']; $m->remote_url = $n['url'];
$m->blurhash = isset($n['blurhash']) && (strlen($n['blurhash']) < 50) ? $n['blurhash'] : null; $m->blurhash = isset($n['blurhash']) && (strlen($n['blurhash']) < 50) ? $n['blurhash'] : null;
$m->width = isset($n['width']) && !empty($n['width']) ? $n['width'] : null; $m->width = isset($n['width']) && ! empty($n['width']) ? $n['width'] : null;
$m->height = isset($n['height']) && !empty($n['height']) ? $n['height'] : null; $m->height = isset($n['height']) && ! empty($n['height']) ? $n['height'] : null;
$m->skip_optimize = true; $m->skip_optimize = true;
$m->order = $key + 1; $m->order = $key + 1;
$m->save(); $m->save();
}); });
} }
protected function updateImmediateAttributes($status, $activity) protected function updateImmediateAttributes($status, $activity)
{ {
if(isset($activity['content'])) { if (isset($activity['content'])) {
$status->caption = strip_tags($activity['content']); $status->caption = strip_tags($activity['content']);
$status->rendered = Purify::clean($activity['content']); $status->rendered = Purify::clean($activity['content']);
} }
if(isset($activity['sensitive'])) { if (isset($activity['sensitive'])) {
if((bool) $activity['sensitive'] == false) { if ((bool) $activity['sensitive'] == false) {
$status->is_nsfw = false; $status->is_nsfw = false;
$exists = ModLog::whereObjectType('App\Status::class') $exists = ModLog::whereObjectType('App\Status::class')
->whereObjectId($status->id) ->whereObjectId($status->id)
->whereAction('admin.status.moderate') ->whereAction('admin.status.moderate')
->exists(); ->exists();
if($exists == true) { if ($exists == true) {
$status->is_nsfw = true; $status->is_nsfw = true;
} }
$profile = Profile::find($status->profile_id); $profile = Profile::find($status->profile_id);
if(!$profile || $profile->cw == true) { if (! $profile || $profile->cw == true) {
$status->is_nsfw = true; $status->is_nsfw = true;
} }
} else { } else {
$status->is_nsfw = true; $status->is_nsfw = true;
} }
} }
if(isset($activity['summary'])) { if (isset($activity['summary'])) {
$status->cw_summary = Purify::clean($activity['summary']); $status->cw_summary = Purify::clean($activity['summary']);
} else { } else {
$status->cw_summary = null; $status->cw_summary = null;
} }
$status->edited_at = now(); $status->edited_at = now();
$status->save(); $status->save();
StatusService::del($status->id); StatusService::del($status->id);
} }
protected function createEdit($status, $activity) protected function createEdit($status, $activity)
{ {
$cleaned = isset($activity['content']) ? Purify::clean($activity['content']) : null; $cleaned = isset($activity['content']) ? Purify::clean($activity['content']) : null;
$spoiler_text = isset($activity['summary']) ? Purify::clean($activity['summary']) : null; $spoiler_text = isset($activity['summary']) ? Purify::clean($activity['summary']) : null;
$sensitive = isset($activity['sensitive']) ? $activity['sensitive'] : null; $sensitive = isset($activity['sensitive']) ? $activity['sensitive'] : null;
$mids = $status->media()->count() ? $status->media()->orderBy('order')->pluck('id')->toArray() : null; $mids = $status->media()->count() ? $status->media()->orderBy('order')->pluck('id')->toArray() : null;
StatusEdit::create([ StatusEdit::create([
'status_id' => $status->id, 'status_id' => $status->id,
'profile_id' => $status->profile_id, 'profile_id' => $status->profile_id,
'caption' => $cleaned, 'caption' => $cleaned,
'spoiler_text' => $spoiler_text, 'spoiler_text' => $spoiler_text,
'is_nsfw' => $sensitive, 'is_nsfw' => $sensitive,
'ordered_media_attachment_ids' => $mids 'ordered_media_attachment_ids' => $mids,
]); ]);
} }
} }