70 lines
1.5 KiB
PHP
Executable File
70 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Documentation\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $documentation_document_id
|
|
* @property string $disk
|
|
* @property string $path
|
|
* @property string $original_name
|
|
* @property string|null $mime_type
|
|
* @property int|null $size
|
|
*/
|
|
class DocumentationAttachment extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/** @var list<string> */
|
|
protected $fillable = [
|
|
'documentation_document_id',
|
|
'disk',
|
|
'path',
|
|
'original_name',
|
|
'mime_type',
|
|
'size',
|
|
'uploaded_by',
|
|
];
|
|
|
|
/** @var array<string, string> */
|
|
protected $casts = [
|
|
'size' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* Owning document.
|
|
*
|
|
* @return BelongsTo<DocumentationDocument, DocumentationAttachment>
|
|
*/
|
|
public function document(): BelongsTo
|
|
{
|
|
return $this->belongsTo(DocumentationDocument::class, 'documentation_document_id');
|
|
}
|
|
|
|
/**
|
|
* Return a readable file size label.
|
|
*/
|
|
public function getSizeLabelAttribute(): string
|
|
{
|
|
$bytes = (int) $this->size;
|
|
|
|
if ($bytes <= 0) {
|
|
return 'Unknown size';
|
|
}
|
|
|
|
if ($bytes < 1024) {
|
|
return $bytes . ' B';
|
|
}
|
|
|
|
if ($bytes < 1048576) {
|
|
return number_format($bytes / 1024, 1) . ' KB';
|
|
}
|
|
|
|
return number_format($bytes / 1048576, 1) . ' MB';
|
|
}
|
|
}
|