Media module, users hardening, README and token ledger

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:27:04 +01:00
1 parent f86f5aea28
commit 9f2a21fa1f
22 files changed
+827 -24

No files matched your search

+80
View File
@@ -2,10 +2,90 @@
namespace IriksIT\Manager\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* One file in the media library. The row is the bookkeeping; the file itself is stored on the
* filesystem disk `disk` under `location`.
*/
class Media extends Model
{
protected $hidden = ['disk', 'location'];
public function getTable(): string
{
return config('manager.table_prefix').'media';
}
protected function casts(): array
{
return ['size' => 'integer', 'width' => 'integer', 'height' => 'integer', 'focus_x' => 'float', 'focus_y' => 'float'];
}
public function uploader(): BelongsTo
{
return $this->belongsTo(config('manager.user_model'), 'uploaded_by');
}
public function isImage(): bool
{
return str_starts_with($this->mime_type, 'image/');
}
/**
* The start of this file's address. It holds the id, so it stays the same when the file is
* renamed, and is what content is searched for to see where a file is used.
*/
public function urlPrefix(): string
{
return '/media-files/'.$this->id.'/';
}
/**
* The address visitors load the file from. It ends in /view on purpose: web servers tend to
* answer anything that ends in .jpg themselves, as a file on disk, and never ask the app.
*/
public function url(): string
{
return $this->urlPrefix().rawurlencode($this->filename).'/view';
}
/**
* The focus point of the image behind an address from the library, as a CSS `object-position`
* value ("30% 65%"). Null for any other address, or when no focus point was set.
*/
public static function focusFor(?string $url): ?string
{
if ($url === null || preg_match('#^/media-files/(\d+)/#', $url, $match) !== 1) {
return null;
}
$media = static::query()->whereKey((int) $match[1])->whereNotNull('focus_x')->whereNotNull('focus_y')->first(['id', 'focus_x', 'focus_y']);
return $media instanceof self
? round((float) $media->focus_x * 100, 2).'% '.round((float) $media->focus_y * 100, 2).'%'
: null;
}
/** What the manager shows. Never includes where the file is stored. */
public function toLibraryItem(int $usageCount = 0): array
{
return [
'id' => $this->id,
'name' => $this->name,
'filename' => $this->filename,
'url' => $this->url(),
'mime_type' => $this->mime_type,
'is_image' => $this->isImage(),
'size' => $this->size,
'width' => $this->width,
'height' => $this->height,
'alt' => $this->alt,
'focus_x' => $this->focus_x,
'focus_y' => $this->focus_y,
'uploaded_by' => $this->uploader?->name,
'created_at' => $this->created_at?->toIso8601String(),
'usage_count' => $usageCount,
];
}
}