feat: get header image upload/persistence working

This commit is contained in:
Jonathan Nielson 2025-03-23 18:12:42 -05:00
parent 18e8c2c634
commit c8aae69ffb
3 changed files with 24 additions and 10 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rules\File;
class WriterController extends Controller
{
@ -51,18 +52,20 @@ class WriterController extends Controller
public function livePreview(Request $request): View
{
//$validated = $request->validate([
// 'header-image' => [File::types(['png', 'jpg', 'jpeg', 'webp'])->max(2 * 1024)],
//]);
// Upload the file
//logger('hey', ['stuff' => request()->all()]);
//sleep(2);
$header_image_path = $request->headerImage?->storeAs('header-image-previews');
$header_image_path = $this->storeHeaderImage($request);
$article_empty = new Article();
$article_empty->markdown_body = $request->get('markdown-body');
$article_empty->convertMarkdownToMarkup();
$article = [
'title' => $request->title ?? null,
'header_image_url' => $header_image_path ?? null,
'subtitle' => $request->subtitle ?? null,
'title' => $request->title ?? '[Title]',
'header_image_url' => $header_image_path ? asset("storage/$header_image_path") : null,
'subtitle' => $request->subtitle ?? '[Sub Title]',
'author' => [
'display_name' => auth()->user()->author?->displayName,
],
@ -70,7 +73,7 @@ class WriterController extends Controller
'primary_link' => $request->primary_link ?? null,
'secondary_link' => $request->secondary_link ?? null,
'mastodon_handle' => $request->mastodon_handle ?? null,
'body' => $article_empty->body,
'body' => $article_empty->body ?? '[Post Body]',
];
return view('shared.single-article', ['article' => $article]);
@ -78,7 +81,7 @@ class WriterController extends Controller
public function saveArticle(Request $request): RedirectResponse
{
$header_image_path = $request->headerImage?->storeAs('header-image-previews');
$header_image_path = $this->storeHeaderImage($request);
$article_empty = new Article();
$article_empty->markdown_body = $request->get('markdown-body');
$article_empty->convertMarkdownToMarkup();
@ -90,10 +93,19 @@ class WriterController extends Controller
'subtitle' => $request->subtitle ?? null,
'body' => $article_empty->body,
'published_at' => $request->get('publication-date') ?? null,
'header_image_url' => $header_image_path ?? null,
'header_image_url' => $header_image_path ? asset("storage/$header_image_path") : null,
'markdown_body' => $request->get('markdown-body'),
]);
return redirect(route('dashboard'));
}
private function storeHeaderImage(Request $request): ?string
{
if(! $request->hasFile('header-image')) return null;
$header_image = $request->file('header-image');
return $header_image->storeAs('article-header-images', $header_image->getClientOriginalName(), 'public');
}
}

View File

@ -35,6 +35,8 @@ class Article extends Model
public function convertMarkdownToMarkup()
{
if (is_null($this->markdown_body)) return null;
$this->body = Str::of($this->markdown_body)->markdown([
'html_input' => 'strip',
'allow_unsafe_links' => false,

View File

@ -74,7 +74,7 @@ document.addEventListener("DOMContentLoaded", (event) => {
<div id="page-body-container">
<section id="new-article-form-container">
<form id="new-article-form" method="POST" action="{{route('save_article')}}">
<form id="new-article-form" method="POST" action="{{route('save_article')}}" enctype="multipart/form-data" hx-encoding="multipart/form-data">
@csrf
<label for="publication-date">Publication Date</label>
<input id="publication-date" name="publication-date" type="datetime-local" class="affects-preview" />