add models, setup livewire, setup mongodb

This commit is contained in:
yuriko 🦊 2025-05-21 15:14:50 -04:00
parent c0590a3412
commit be4c848eee
Signed by: jaiden
SSH key fingerprint: SHA256:f8tvveBoXBrKZIQDWLLcpQrKbATUCGg98x2N4YzkDM8
27 changed files with 2508 additions and 0 deletions

109
app/Models/Post.php Normal file
View file

@ -0,0 +1,109 @@
<?php
namespace App\Models;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsTo;
use MongoDB\Laravel\Relations\BelongsToMany;
use Symfony\Component\HttpFoundation\StreamedResponse;
class Post extends Model
{
protected $fillable = [ 'rating', 'extension', 'featured' ];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);
}
protected function toBase64(string $path): string
{
$ext = $this->extension;
$file = Storage::get($path);
$data = base64_encode($file);
return "data:image/$ext;base64,$data";
}
public function getThumbUrl(): ?string
{
if (Storage::has("posts/$this->id/thumb"))
{
return $this->toBase64("posts/$this->id/thumb");
}
return $this->getPreviewUrl();
}
public function getPreviewUrl(): ?string
{
if (Storage::has("posts/$this->id/preview"))
{
return $this->toBase64("posts/$this->id/preview");
}
return $this->getFullUrl();
}
public function getFullUrl(): ?string
{
if (Storage::has("posts/$this->id/full"))
{
return $this->toBase64("posts/$this->id/full");
}
abort(404);
}
public function getMimeType(): ?string
{
return Storage::mimeType("posts/$this->id/full");
}
public function getDimensions(): false|array
{
return getimagesize($this->getFullUrl());
}
public function getAspectRatio(): string
{
list($width, $height) = $this->getDimensions();
$divisor = gmp_intval(gmp_gcd($width, $height));
$w = $width / $divisor;
$h = $height / $divisor;
return "aspect-ratio: $w/$h;";
}
public function download(): StreamedResponse
{
return Storage::download("posts/$this->id/full");
}
public function getNextPost(): ?Post
{
return Post::where('created_at', '>', $this->created_at)
->orderBy('created_at', 'asc')
->first();
}
public function getPreviousPost(): ?Post
{
return Post::where('created_at', '<', $this->created_at)
->orderBy('created_at', 'desc')
->first();
}
public function getRatingColor(): string
{
return match ($this->rating)
{
'safe' => 'success',
'suggestive' => 'warning',
'explicit' => 'danger',
default => 'default',
};
}
}