-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageOffload.php
More file actions
162 lines (140 loc) · 3.74 KB
/
Copy pathStorageOffload.php
File metadata and controls
162 lines (140 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
* Core PHP Framework
*
* Licensed under the European Union Public Licence (EUPL) v1.2.
* See LICENSE file for details.
*/
declare(strict_types=1);
namespace Core\Cdn\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* Tracks files that have been offloaded to remote storage.
*
* @property int $id
* @property string $local_path Original local file path
* @property string $remote_path Path in remote storage
* @property string $disk Laravel disk name
* @property string|null $hash SHA-256 hash of file contents
* @property int|null $file_size File size in bytes
* @property string|null $mime_type MIME type
* @property string|null $category Category for path prefixing
* @property array|null $metadata Additional metadata
* @property Carbon|null $offloaded_at When file was offloaded
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
class StorageOffload extends Model
{
use HasFactory;
protected $table = 'storage_offloads';
protected $fillable = [
'local_path',
'remote_path',
'disk',
'hash',
'file_size',
'mime_type',
'category',
'metadata',
'offloaded_at',
];
protected $casts = [
'metadata' => 'array',
'file_size' => 'integer',
'offloaded_at' => 'datetime',
];
/**
* Get the category.
*/
public function getCategory(): ?string
{
return $this->category;
}
/**
* Get metadata value by key.
*/
public function getMetadata(?string $key = null): mixed
{
if ($key === null) {
return $this->metadata;
}
return $this->metadata[$key] ?? null;
}
/**
* Get the original filename from metadata.
*/
public function getOriginalName(): ?string
{
return $this->getMetadata('original_name');
}
/**
* Check if this offload is for a specific category.
*/
public function isCategory(string $category): bool
{
return $this->getCategory() === $category;
}
/**
* Check if the file is an image.
*/
public function isImage(): bool
{
return $this->mime_type && str_starts_with($this->mime_type, 'image/');
}
/**
* Check if the file is a video.
*/
public function isVideo(): bool
{
return $this->mime_type && str_starts_with($this->mime_type, 'video/');
}
/**
* Check if the file is audio.
*/
public function isAudio(): bool
{
return $this->mime_type && str_starts_with($this->mime_type, 'audio/');
}
/**
* Scope to filter by category.
*/
public function scopeCategory($query, string $category)
{
return $query->where('category', $category);
}
/**
* Alias for scopeCategory - filter by category.
*/
public function scopeInCategory($query, string $category)
{
return $this->scopeCategory($query, $category);
}
/**
* Scope to filter by disk.
*/
public function scopeDisk($query, string $disk)
{
return $query->where('disk', $disk);
}
/**
* Alias for scopeDisk - filter by disk.
*/
public function scopeForDisk($query, string $disk)
{
return $this->scopeDisk($query, $disk);
}
/**
* Get human-readable file size.
*/
protected function getFileSizeHumanAttribute(): string
{
$bytes = $this->file_size ?? 0;
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$power = $bytes > 0 ? floor(log($bytes, 1024)) : 0;
$power = min($power, count($units) - 1);
return round($bytes / (1024 ** $power), 2).' '.$units[$power];
}
}