mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-04 10:42:23 +00:00
Persist terminal uploads, add server upload UI and import selection flow, and schedule cleanup for expired or stale files. Tighten upload security, replace ad hoc file cleanup with tracked deletion logic, and cover the new behavior with feature and unit tests.
105 lines
2.8 KiB
PHP
105 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\TerminalUploadedFileStatus;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class TerminalUploadedFile extends BaseModel
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'team_id',
|
|
'server_id',
|
|
'upload_token',
|
|
'container_uuid',
|
|
'original_name',
|
|
'stored_filename',
|
|
'mime_type',
|
|
'size_bytes',
|
|
'local_path',
|
|
'server_path',
|
|
'container_path',
|
|
'status',
|
|
'uploaded_at',
|
|
'expires_at',
|
|
'finalized_at',
|
|
'deleted_at',
|
|
'cleanup_attempts',
|
|
'last_cleanup_error',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => TerminalUploadedFileStatus::class,
|
|
'size_bytes' => 'integer',
|
|
'cleanup_attempts' => 'integer',
|
|
'uploaded_at' => 'datetime',
|
|
'expires_at' => 'datetime',
|
|
'finalized_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
public function server(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Server::class);
|
|
}
|
|
|
|
public function scopeOwnedByCurrentUserAndTeam(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->where('user_id', auth()->id())
|
|
->where('team_id', data_get(auth()->user()?->currentTeam(), 'id'));
|
|
}
|
|
|
|
public function scopeVisible(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->whereNull('deleted_at')
|
|
->whereNotNull('finalized_at')
|
|
->whereIn('status', [
|
|
TerminalUploadedFileStatus::Active,
|
|
TerminalUploadedFileStatus::DeleteFailed,
|
|
TerminalUploadedFileStatus::Deleting,
|
|
]);
|
|
}
|
|
|
|
public function scopeExpiredForCleanup(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->whereNull('deleted_at')
|
|
->whereNotNull('finalized_at')
|
|
->whereNotNull('expires_at')
|
|
->where('expires_at', '<=', now())
|
|
->whereIn('status', [
|
|
TerminalUploadedFileStatus::Active,
|
|
TerminalUploadedFileStatus::DeleteFailed,
|
|
]);
|
|
}
|
|
|
|
public function scopePendingForCleanup(Builder $query, int $hours): Builder
|
|
{
|
|
return $query
|
|
->whereNull('deleted_at')
|
|
->whereNull('finalized_at')
|
|
->where('uploaded_at', '<=', now()->subHours($hours))
|
|
->whereIn('status', [
|
|
TerminalUploadedFileStatus::Pending,
|
|
TerminalUploadedFileStatus::DeleteFailed,
|
|
]);
|
|
}
|
|
}
|