better image uploading & user roles
Some checks failed
Docker / build (push) Has been cancelled

This commit is contained in:
yuriko 🦊 2025-08-09 23:01:27 -04:00
parent 21e59d775a
commit f60ae41bf6
26 changed files with 741 additions and 70 deletions

46
app/Livewire/App/Role.php Normal file
View file

@ -0,0 +1,46 @@
<?php
namespace App\Livewire\App;
use App\Enums\RolesEnum;
use App\Models\User;
use Livewire\Component;
class Role extends Component
{
public User $user;
public string $size = 'small';
protected string $variant = 'neutral';
protected string $name = '';
public function mount(User $user)
{
$this->user = $user;
if ($user->hasRole(RolesEnum::RESTRICTED))
{
$this->variant = RolesEnum::RESTRICTED->variant();
$this->name = RolesEnum::RESTRICTED->label();
}
if ($user->hasRole(RolesEnum::MEMBER))
{
$this->variant = RolesEnum::MEMBER->variant();
$this->name = RolesEnum::MEMBER->label();
}
if ($user->hasRole(RolesEnum::MODERATOR))
{
$this->variant = RolesEnum::MODERATOR->variant();
$this->name = RolesEnum::MODERATOR->label();
}
if ($user->hasRole(RolesEnum::ADMIN))
{
$this->variant = RolesEnum::ADMIN->variant();
$this->name = RolesEnum::ADMIN->label();
}
}
public function render()
{
return view('livewire.app.role');
}
}

View file

@ -15,8 +15,8 @@ class Upload extends Component
{
use WithFileUploads;
#[Validate('image|max:65536')]
public $file;
#[Validate(['files.*' => 'required|image|max:65536'])]
public $files = [];
#[Validate('required|in:safe,suggestive,explicit')]
public $rating = 'safe';
@ -32,33 +32,35 @@ class Upload extends Component
$this->validate();
$author = Auth::user();
if ($this->file)
foreach ($this->files as $file)
{
$post = Post::create([
'extension' => $this->file->getClientOriginalExtension(),
'rating' => $this->rating,
]);
if ($post)
if ($file)
{
$author->posts()->save($post);
$post = Post::create([
'extension' => $file->getClientOriginalExtension(),
'rating' => $this->rating,
'hash' => $file->hashName(),
]);
// Save the full image
$this->file->storeAs("posts/$post->id", 'full');
$fullImg = Storage::get("posts/$post->id/full");
if ($post)
{
$author->posts()->save($post);
// Create thumbnail preview
$thumb = Image::read($fullImg)->cover(width: 512, height: 512);
Storage::put("posts/$post->id/thumb", $thumb->encodeByMediaType());
// Save the full image
$file->storeAs("posts/$post->id", 'full');
$fullImg = Storage::get("posts/$post->id/full");
// Create smaller preview image
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
Storage::put("posts/$post->id/preview", $preview->encodeByMediaType());
// Create thumbnail preview
$thumb = Image::read($fullImg)->cover(width: 512, height: 512);
Storage::put("posts/$post->id/thumb", $thumb->encodeByMediaType());
return $this->redirect("/posts/$post->id");
// Create smaller preview image
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
Storage::put("posts/$post->id/preview", $preview->encodeByMediaType());
}
}
}
return $this->redirect('/upload');
return $this->redirect('/posts');
}
}

View file

@ -8,6 +8,7 @@ use Livewire\Component;
class PostFeature extends Component
{
public ?Post $post = null;
public $tags = null;
public function mount()
{
@ -18,6 +19,7 @@ class PostFeature extends Component
['$sample' => ['size' => 1]]
]);
})->first();
$this->tags = $this->post->tags()->take(5)->get();
}
public function placeholder()
@ -36,7 +38,6 @@ HTML;
{
if ($this->post == null)
{
$href = route('posts.home');
return view('livewire.post-feature-empty');
}
return view('livewire.post-feature');

View file

@ -3,9 +3,7 @@
namespace App\Livewire\Posts;
use App\Models\Post;
use App\Models\Tag;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
@ -13,11 +11,20 @@ class Index extends Component
{
use WithPagination;
public $posts = [];
public function mount()
{
$this->posts = Post::orderBy('created_at', 'desc')->get();
}
#[Title('Posts')]
public function render()
{
return view('livewire.posts.index', [
'posts' => Post::orderBy('created_at', 'desc')->paginate(25),
]);
if ($this->posts->count() == 0)
{
return view('livewire.posts.index-empty');
}
return view('livewire.posts.index');
}
}

View file

@ -2,10 +2,28 @@
namespace App\Livewire\Tags;
use App\Models\Post;
use App\Models\Tag;
use Livewire\Component;
class View extends Component
{
public ?Tag $tag = null;
public $posts = [];
public function mount(?Tag $tag)
{
$this->tag = $tag;
if ($tag)
{
$this->posts = $tag->posts;
}
else
{
$this->posts = Post::doesntHave('tags')->get();
}
}
public function render()
{
return view('livewire.tags.view');