mirror of
https://github.com/NyaaStudios/nyaabooru.git
synced 2025-12-10 05:42:58 +00:00
add models, setup livewire, setup mongodb
This commit is contained in:
parent
c0590a3412
commit
be4c848eee
27 changed files with 2508 additions and 0 deletions
109
app/Models/Post.php
Normal file
109
app/Models/Post.php
Normal 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',
|
||||
};
|
||||
}
|
||||
}
|
||||
16
app/Models/Tag.php
Normal file
16
app/Models/Tag.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use MongoDB\Laravel\Eloquent\Model;
|
||||
use MongoDB\Laravel\Relations\BelongsToMany;
|
||||
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = [ 'name', 'slug', 'implies' ];
|
||||
|
||||
public function posts(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Post::class);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue