Changeset View
Changeset View
Standalone View
Standalone View
src/app/Fs/File.php
- This file was added.
<?php | |||||
namespace App\Fs; | |||||
use App\User; | |||||
use App\Traits\UuidStrKeyTrait; | |||||
use Illuminate\Database\Eloquent\Casts\Attribute; | |||||
use Illuminate\Database\Eloquent\Model; | |||||
/** | |||||
* The eloquent definition of a File. | |||||
* | |||||
* @property string $id File identifier | |||||
* @property string $mimetype File content type | |||||
* @property string $name File name | |||||
* @property string $path File path (readonly) | |||||
* @property int $size File size (in bytes) | |||||
* @property int $user_id File owner | |||||
*/ | |||||
class File extends Model | |||||
{ | |||||
use UuidStrKeyTrait; | |||||
public const TYPE_INCOMPLETE = 'application/x-incomplete-file'; | |||||
public const TYPE_FOLDER = 'application/x-folder'; | |||||
/** @var array<int, string> The attributes that are mass assignable */ | |||||
protected $fillable = [ | |||||
'user_id', | |||||
'mimetype', | |||||
'name', | |||||
'size', | |||||
]; | |||||
/** @var array<string, string> The attributes that should be cast */ | |||||
protected $casts = [ | |||||
'created_at' => 'datetime:Y-m-d H:i:s', | |||||
'updated_at' => 'datetime:Y-m-d H:i:s', | |||||
]; | |||||
/** @var string Database table name */ | |||||
protected $table = 'fs_files'; | |||||
/** | |||||
* Interact with the file's mimetype. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Casts\Attribute | |||||
*/ | |||||
protected function mimetype(): Attribute | |||||
{ | |||||
return Attribute::make( | |||||
// get: fn ($value) => \strtolower($value), | |||||
set: function ($value) { | |||||
return \strtolower($value); | |||||
}, | |||||
); | |||||
} | |||||
/** | |||||
* Getter for the file path (without the filename) in the storage. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Casts\Attribute | |||||
*/ | |||||
protected function path(): Attribute | |||||
{ | |||||
return Attribute::make( | |||||
get: function ($value) { | |||||
if (empty($this->id)) { | |||||
throw new \Exception("Cannot get path for a file without ID"); | |||||
} | |||||
$id = substr($this->id, 0, 6); | |||||
return implode('/', str_split($id, 2)); | |||||
} | |||||
); | |||||
} | |||||
/** | |||||
* FIle parts (nodes) of this file. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function nodes() | |||||
{ | |||||
return $this->hasMany(Node::class); | |||||
} | |||||
/** | |||||
* Sharing permissions assigned to this file. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function permissions() | |||||
{ | |||||
return $this->hasMany(Permission::class); | |||||
} | |||||
/** | |||||
* The user to which this file belongs. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |||||
*/ | |||||
public function user() | |||||
{ | |||||
return $this->belongsTo(User::class, 'user_id', 'id'); | |||||
} | |||||
} |