Files
x-server-mobile/backend/app/model/UploadedFiles.php
T
2026-03-05 17:10:29 +08:00

116 lines
2.9 KiB
PHP

<?php
namespace app\Model;
use support\Model;
/**
* 上传文件记录模型
*/
class UploadedFiles extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'uploaded_files';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
/**
* 可批量赋值的字段
*/
protected $fillable = [
'file_name',
'file_path',
'file_size',
'file_type',
'category',
'original_name',
'extension',
'partition',
'upload_ip',
'user_agent',
'user_id',
];
/**
* 上传分区配置
*/
public static array $uploadPartitions = [
'image' => [
'path' => 'uploads/images',
'max_size' => 10 * 1024 * 1024, // 10MB
'allowed_types' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
'extensions' => ['jpg', 'jpeg', 'png', 'gif', 'webp']
],
'avatar' => [
'path' => 'uploads/avatars',
'max_size' => 5 * 1024 * 1024, // 5MB
'allowed_types' => ['image/jpeg', 'image/png', 'image/webp'],
'extensions' => ['jpg', 'jpeg', 'png', 'webp']
],
'document' => [
'path' => 'uploads/documents',
'max_size' => 20 * 1024 * 1024, // 20MB
'allowed_types' => [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
],
'extensions' => ['pdf', 'doc', 'docx', 'xls', 'xlsx']
],
'usedcar' => [
'path' => 'uploads/usedcar',
'max_size' => 10 * 1024 * 1024, // 10MB
'allowed_types' => ['image/jpeg', 'image/png', 'image/webp', 'image/gif'],
'extensions' => ['jpg', 'jpeg', 'png', 'webp', 'gif']
],
'other' => [
'path' => 'uploads/others',
'max_size' => 5 * 1024 * 1024, // 5MB
'allowed_types' => [],
'extensions' => []
]
];
/**
* 关联用户
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
/**
* 获取分区配置
*/
public static function getPartitionConfig(string $partition): ?array
{
return self::$uploadPartitions[$partition] ?? self::$uploadPartitions['other'];
}
/**
* 获取所有分区配置
*/
public static function getAllPartitions(): array
{
return self::$uploadPartitions;
}
}