added attchments
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\BbcodeFormatter;
|
||||
use App\Models\Post;
|
||||
use App\Models\Thread;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -16,6 +17,8 @@ class PostController extends Controller
|
||||
'user' => fn ($query) => $query
|
||||
->withCount(['posts', 'threads', 'thanksGiven', 'thanksReceived'])
|
||||
->with(['rank', 'roles']),
|
||||
'attachments.extension',
|
||||
'attachments.group',
|
||||
]);
|
||||
|
||||
$threadParam = $request->query('thread');
|
||||
@@ -54,6 +57,8 @@ class PostController extends Controller
|
||||
'user' => fn ($query) => $query
|
||||
->withCount(['posts', 'threads', 'thanksGiven', 'thanksReceived'])
|
||||
->with(['rank', 'roles']),
|
||||
'attachments.extension',
|
||||
'attachments.group',
|
||||
]);
|
||||
|
||||
return response()->json($this->serializePost($post), 201);
|
||||
@@ -87,9 +92,12 @@ class PostController extends Controller
|
||||
|
||||
private function serializePost(Post $post): array
|
||||
{
|
||||
$attachments = $post->relationLoaded('attachments') ? $post->attachments : collect();
|
||||
$bodyHtml = $this->renderBody($post->body, $attachments);
|
||||
return [
|
||||
'id' => $post->id,
|
||||
'body' => $post->body,
|
||||
'body_html' => $bodyHtml,
|
||||
'thread' => "/api/threads/{$post->thread_id}",
|
||||
'user_id' => $post->user_id,
|
||||
'user_name' => $post->user?->name,
|
||||
@@ -111,9 +119,69 @@ class PostController extends Controller
|
||||
'user_group_color' => $this->resolveGroupColor($post->user),
|
||||
'created_at' => $post->created_at?->toIso8601String(),
|
||||
'updated_at' => $post->updated_at?->toIso8601String(),
|
||||
'attachments' => $post->relationLoaded('attachments')
|
||||
? $attachments
|
||||
->map(fn ($attachment) => [
|
||||
'id' => $attachment->id,
|
||||
'group' => $attachment->group ? [
|
||||
'id' => $attachment->group->id,
|
||||
'name' => $attachment->group->name,
|
||||
] : null,
|
||||
'original_name' => $attachment->original_name,
|
||||
'extension' => $attachment->extension,
|
||||
'mime_type' => $attachment->mime_type,
|
||||
'size_bytes' => $attachment->size_bytes,
|
||||
'download_url' => "/api/attachments/{$attachment->id}/download",
|
||||
'created_at' => $attachment->created_at?->toIso8601String(),
|
||||
])
|
||||
->values()
|
||||
: [],
|
||||
];
|
||||
}
|
||||
|
||||
private function renderBody(string $body, $attachments): string
|
||||
{
|
||||
$replaced = $this->replaceAttachmentTags($body, $attachments);
|
||||
return BbcodeFormatter::format($replaced);
|
||||
}
|
||||
|
||||
private function replaceAttachmentTags(string $body, $attachments): string
|
||||
{
|
||||
if (!$attachments || count($attachments) === 0) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
$map = [];
|
||||
foreach ($attachments as $attachment) {
|
||||
$name = strtolower($attachment->original_name ?? '');
|
||||
if ($name !== '') {
|
||||
$map[$name] = [
|
||||
'url' => "/api/attachments/{$attachment->id}/download",
|
||||
'mime' => $attachment->mime_type ?? '',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$map) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
return preg_replace_callback('/\\[attachment\\](.+?)\\[\\/attachment\\]/i', function ($matches) use ($map) {
|
||||
$rawName = trim($matches[1]);
|
||||
$key = strtolower($rawName);
|
||||
if (!array_key_exists($key, $map)) {
|
||||
return $matches[0];
|
||||
}
|
||||
$entry = $map[$key];
|
||||
$url = $entry['url'];
|
||||
$mime = $entry['mime'] ?? '';
|
||||
if (str_starts_with($mime, 'image/')) {
|
||||
return "[img]{$url}[/img]";
|
||||
}
|
||||
return "[url={$url}]{$rawName}[/url]";
|
||||
}, $body) ?? $body;
|
||||
}
|
||||
|
||||
private function resolveGroupColor(?\App\Models\User $user): ?string
|
||||
{
|
||||
if (!$user) {
|
||||
|
||||
Reference in New Issue
Block a user