Files
laravel-manager/src/Models/Media.php
T
Matthijs Iriks 60ad85a621
PR CI / ledger (pull_request) Successful in 3s
PR CI / flow (pull_request) Successful in 4s
PR CI / ui (pull_request) Successful in 17s
PR CI / test (lowest) (pull_request) Successful in 45s
PR CI / test (stable) (pull_request) Successful in 48s
PR CI / security (pull_request) Successful in 54s
PR CI / pr-ci (pull_request) Successful in 1s
PR CI / release-automerge (pull_request) Successful in 3s
Let route names and tables move, and add manager:import
2026-12-04 05:08:08 +01:00

93 lines
2.9 KiB
PHP

<?php
namespace IriksIT\Manager\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use IriksIT\Manager\Support\Names;
/**
* 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 Names::table('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,
];
}
}