<?php

namespace Kylin\Storage\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Kylin\Storage\Services\StorageService;

/**
 * 文件模型
 */
class File extends Model
{
    protected $table = 'kylin_files';

    protected $fillable = [
        'driver', 'path', 'original_name', 'mime_type', 'size',
        'hash', 'model_type', 'model_id', 'collection', 'metadata', 'user_id',
    ];

    protected $casts = ['metadata' => 'array'];

    protected $appends = ['url'];

    public function model(): MorphTo
    {
        return $this->morphTo();
    }

    public function getUrlAttribute(): string
    {
        return app(StorageService::class)->adapter($this->driver)->getUrl($this->path);
    }

    public function getSignedUrl(int $expires = 3600): string
    {
        return app(StorageService::class)->adapter($this->driver)->getSignedUrl($this->path, $expires);
    }
}
