116 lines
3.6 KiB
PHP
116 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* Datei-Anhang einer Angebotsversion.
|
|
*
|
|
* Bewusst API-kompatibel mit {@see BookingFile} gehalten (gleiche
|
|
* Method-Signaturen für `getURL()`, `getIconExt()`, `formatBytes()`,
|
|
* `getPath()`), damit Blade-Partials wie `booking/modal-new-booking-files`
|
|
* und die Dropzone-JS-Helfer direkt wiederverwendet werden können.
|
|
*
|
|
* Speichert in disk `offer` (Konvention: `storage/app/offer/YYYY/MM/…`).
|
|
* Die Spalten-Schreibweise `mine` (statt `mime`) wurde aus BookingFile
|
|
* übernommen, damit Frontend-Code identisch funktioniert.
|
|
*
|
|
* @property int $id
|
|
* @property int $offer_version_id
|
|
* @property string|null $identifier
|
|
* @property string $filename
|
|
* @property string $dir
|
|
* @property string $original_name
|
|
* @property string $ext
|
|
* @property string $mine
|
|
* @property int $size
|
|
* @property bool $include_in_pdf
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
* @property-read OfferVersion $offerVersion
|
|
*/
|
|
class OfferFile extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $connection = 'mysql';
|
|
protected $table = 'offer_files';
|
|
|
|
protected $fillable = [
|
|
'offer_version_id',
|
|
'identifier',
|
|
'filename',
|
|
'dir',
|
|
'original_name',
|
|
'ext',
|
|
'mine',
|
|
'size',
|
|
'include_in_pdf',
|
|
];
|
|
|
|
protected $casts = [
|
|
'offer_version_id' => 'int',
|
|
'size' => 'int',
|
|
'include_in_pdf' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* Icon-Klassen nach Extension (kompatibel zu BookingFile::$icon_ext).
|
|
* @var array<string, string>
|
|
*/
|
|
public static $icon_ext = [
|
|
'default' => 'fa fa-file',
|
|
'pdf' => 'fa fa-file-pdf',
|
|
'jpg' => 'fa fa-file-image',
|
|
'jpeg' => 'fa fa-file-image',
|
|
'png' => 'fa fa-file-image',
|
|
'doc' => 'fa fa-file-word',
|
|
'docx' => 'fa fa-file-word',
|
|
];
|
|
|
|
public function offerVersion(): BelongsTo
|
|
{
|
|
return $this->belongsTo(OfferVersion::class, 'offer_version_id');
|
|
}
|
|
|
|
public function getIconExt(): string
|
|
{
|
|
return self::$icon_ext[$this->ext] ?? self::$icon_ext['default'];
|
|
}
|
|
|
|
/**
|
|
* Download/Preview-URL. Der zweite Parameter der Storage-Route (`offer`)
|
|
* wird vom `storage_file`-Controller für Disk-Auswahl ausgewertet —
|
|
* muss serverseitig freigeschaltet sein (siehe Ticket B4 / StorageController).
|
|
*
|
|
* @param bool|string $download false = inline, 'download' = Attachment
|
|
*/
|
|
public function getURL($download = false): string
|
|
{
|
|
return route('storage_file', [$this->id, 'offer', $download]);
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
// gleiches Idiom wie BookingFile::getPath() — Intelephense sieht
|
|
// path() im Filesystem-Contract in diesem Kontext nicht zuverlässig,
|
|
// php -l ist sauber; Methode ist Teil der Laravel-Filesystem-API seit 9.x.
|
|
/** @phpstan-ignore-next-line */
|
|
return \Storage::disk('offer')->path($this->dir . $this->filename);
|
|
}
|
|
|
|
public function formatBytes(int $precision = 2): string
|
|
{
|
|
$size = (int) $this->size;
|
|
if ($size <= 0) {
|
|
return '0 bytes';
|
|
}
|
|
$base = log($size) / log(1024);
|
|
$suffixes = [' bytes', ' KB', ' MB', ' GB', ' TB'];
|
|
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
|
|
}
|
|
}
|