nyaabooru/app/Livewire/Pages/Upload.php
Jaiden f60ae41bf6
Some checks failed
Docker / build (push) Has been cancelled
better image uploading & user roles
2025-08-09 23:01:27 -04:00

66 lines
1.5 KiB
PHP

<?php
namespace App\Livewire\Pages;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image;
use Livewire\Attributes\Title;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;
class Upload extends Component
{
use WithFileUploads;
#[Validate(['files.*' => 'required|image|max:65536'])]
public $files = [];
#[Validate('required|in:safe,suggestive,explicit')]
public $rating = 'safe';
#[Title('Upload')]
public function render()
{
return view('livewire.pages.upload');
}
public function createPost()
{
$this->validate();
$author = Auth::user();
foreach ($this->files as $file)
{
if ($file)
{
$post = Post::create([
'extension' => $file->getClientOriginalExtension(),
'rating' => $this->rating,
'hash' => $file->hashName(),
]);
if ($post)
{
$author->posts()->save($post);
// Save the full image
$file->storeAs("posts/$post->id", 'full');
$fullImg = Storage::get("posts/$post->id/full");
// Create thumbnail preview
$thumb = Image::read($fullImg)->cover(width: 512, height: 512);
Storage::put("posts/$post->id/thumb", $thumb->encodeByMediaType());
// Create smaller preview image
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
Storage::put("posts/$post->id/preview", $preview->encodeByMediaType());
}
}
}
return $this->redirect('/posts');
}
}