'string', 'email_verified_at' => 'datetime', ]; protected $appends = [ 'usedCapacity', 'storage', ]; public $sortable = [ 'id', 'name', 'role', 'created_at', ]; public $incrementing = false; protected $keyType = 'string'; protected static function newFactory(): UserFactory { return UserFactory::new(); } /** * Get user used storage details */ public function getStorageAttribute(): array { $is_storage_limit = get_settings('storage_limitation') ?? 1; if (! $is_storage_limit) { return [ 'used' => $this->usedCapacity, 'used_formatted' => Metric::bytes($this->usedCapacity)->format(), ]; } return [ 'used' => (float) get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount), 'used_formatted' => get_storage_fill_percentage($this->usedCapacity, $this->limitations->max_storage_amount) . '%', 'capacity' => $this->limitations->max_storage_amount, 'capacity_formatted' => format_gigabytes($this->limitations->max_storage_amount), ]; } /** * Get user used storage capacity in bytes */ public function getUsedCapacityAttribute(): int { return $this->filesWithTrashed ->map(fn ($item) => $item->getRawOriginal())->sum('filesize'); } /** * Get user full folder tree */ public function getFolderTreeAttribute(): Collection { return Folder::with(['folders.shared', 'shared:token,id,item_id,permission,is_protected,expire_in']) ->where('parent_id') ->where('team_folder', false) ->where('user_id', $this->id) ->sortable() ->get(); } public function settings(): HasOne { return $this->hasOne(UserSettings::class); } public function limitations(): HasOne { return $this->hasOne(UserLimitation::class); } /** * Get user favourites folder */ public function favouriteFolders(): BelongsToMany { return $this->belongsToMany(Folder::class, 'favourite_folder', 'user_id', 'parent_id', 'id', 'id') ->where('team_folder', false); } /** * Get all user files */ public function filesWithTrashed(): HasMany { return $this->hasMany(File::class) ->withTrashed(); } /** * Get 5 latest uploads */ public function latestUploads(): HasMany { return $this->hasMany(File::class) ->with([ 'parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in', ]) ->take(40); } /** * Get all user files */ public function files(): HasMany { return $this->hasMany(File::class); } /** * Send the password reset notification. */ public function sendPasswordResetNotification($token): void { $this->notify(new ResetPassword($token)); } protected static function boot() { parent::boot(); static::creating(function ($user) { $user->id = Str::uuid(); // Create default limitations $user->limitations()->create([ 'max_storage_amount' => get_settings('default_storage_amount') ?? 1, 'max_team_members' => 3, ]); // Create user directory for his files Storage::makeDirectory("files/$user->id"); }); } }