feat: get header image upload/persistence working
This commit is contained in:
parent
18e8c2c634
commit
c8aae69ffb
|
|
@ -7,6 +7,7 @@ use Illuminate\Http\Request;
|
||||||
use Illuminate\Contracts\View\View;
|
use Illuminate\Contracts\View\View;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Validation\Rules\File;
|
||||||
|
|
||||||
class WriterController extends Controller
|
class WriterController extends Controller
|
||||||
{
|
{
|
||||||
|
|
@ -51,18 +52,20 @@ class WriterController extends Controller
|
||||||
|
|
||||||
public function livePreview(Request $request): View
|
public function livePreview(Request $request): View
|
||||||
{
|
{
|
||||||
|
//$validated = $request->validate([
|
||||||
|
// 'header-image' => [File::types(['png', 'jpg', 'jpeg', 'webp'])->max(2 * 1024)],
|
||||||
|
//]);
|
||||||
|
|
||||||
// Upload the file
|
// Upload the file
|
||||||
//logger('hey', ['stuff' => request()->all()]);
|
$header_image_path = $this->storeHeaderImage($request);
|
||||||
//sleep(2);
|
|
||||||
$header_image_path = $request->headerImage?->storeAs('header-image-previews');
|
|
||||||
$article_empty = new Article();
|
$article_empty = new Article();
|
||||||
$article_empty->markdown_body = $request->get('markdown-body');
|
$article_empty->markdown_body = $request->get('markdown-body');
|
||||||
$article_empty->convertMarkdownToMarkup();
|
$article_empty->convertMarkdownToMarkup();
|
||||||
|
|
||||||
$article = [
|
$article = [
|
||||||
'title' => $request->title ?? null,
|
'title' => $request->title ?? '[Title]',
|
||||||
'header_image_url' => $header_image_path ?? null,
|
'header_image_url' => $header_image_path ? asset("storage/$header_image_path") : null,
|
||||||
'subtitle' => $request->subtitle ?? null,
|
'subtitle' => $request->subtitle ?? '[Sub Title]',
|
||||||
'author' => [
|
'author' => [
|
||||||
'display_name' => auth()->user()->author?->displayName,
|
'display_name' => auth()->user()->author?->displayName,
|
||||||
],
|
],
|
||||||
|
|
@ -70,7 +73,7 @@ class WriterController extends Controller
|
||||||
'primary_link' => $request->primary_link ?? null,
|
'primary_link' => $request->primary_link ?? null,
|
||||||
'secondary_link' => $request->secondary_link ?? null,
|
'secondary_link' => $request->secondary_link ?? null,
|
||||||
'mastodon_handle' => $request->mastodon_handle ?? null,
|
'mastodon_handle' => $request->mastodon_handle ?? null,
|
||||||
'body' => $article_empty->body,
|
'body' => $article_empty->body ?? '[Post Body]',
|
||||||
];
|
];
|
||||||
|
|
||||||
return view('shared.single-article', ['article' => $article]);
|
return view('shared.single-article', ['article' => $article]);
|
||||||
|
|
@ -78,7 +81,7 @@ class WriterController extends Controller
|
||||||
|
|
||||||
public function saveArticle(Request $request): RedirectResponse
|
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 = new Article();
|
||||||
$article_empty->markdown_body = $request->get('markdown-body');
|
$article_empty->markdown_body = $request->get('markdown-body');
|
||||||
$article_empty->convertMarkdownToMarkup();
|
$article_empty->convertMarkdownToMarkup();
|
||||||
|
|
@ -90,10 +93,19 @@ class WriterController extends Controller
|
||||||
'subtitle' => $request->subtitle ?? null,
|
'subtitle' => $request->subtitle ?? null,
|
||||||
'body' => $article_empty->body,
|
'body' => $article_empty->body,
|
||||||
'published_at' => $request->get('publication-date') ?? null,
|
'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'),
|
'markdown_body' => $request->get('markdown-body'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return redirect(route('dashboard'));
|
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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,8 @@ class Article extends Model
|
||||||
|
|
||||||
public function convertMarkdownToMarkup()
|
public function convertMarkdownToMarkup()
|
||||||
{
|
{
|
||||||
|
if (is_null($this->markdown_body)) return null;
|
||||||
|
|
||||||
$this->body = Str::of($this->markdown_body)->markdown([
|
$this->body = Str::of($this->markdown_body)->markdown([
|
||||||
'html_input' => 'strip',
|
'html_input' => 'strip',
|
||||||
'allow_unsafe_links' => false,
|
'allow_unsafe_links' => false,
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ document.addEventListener("DOMContentLoaded", (event) => {
|
||||||
|
|
||||||
<div id="page-body-container">
|
<div id="page-body-container">
|
||||||
<section id="new-article-form-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
|
@csrf
|
||||||
<label for="publication-date">Publication Date</label>
|
<label for="publication-date">Publication Date</label>
|
||||||
<input id="publication-date" name="publication-date" type="datetime-local" class="affects-preview" />
|
<input id="publication-date" name="publication-date" type="datetime-local" class="affects-preview" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue