mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-28 19:10:40 +00:00
Limitation API skelet with can upload tests
This commit is contained in:
@@ -202,7 +202,7 @@ class SetupDevEnvironment extends Command
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
Share::factory(Share::class)
|
||||
Share::factory()
|
||||
->create([
|
||||
'type' => 'folder',
|
||||
'item_id' => $shared_folder->id,
|
||||
@@ -337,7 +337,7 @@ class SetupDevEnvironment extends Command
|
||||
'created_at' => now()->subMinutes(4),
|
||||
]);
|
||||
|
||||
Share::factory(Share::class)
|
||||
Share::factory()
|
||||
->create([
|
||||
'type' => 'folder',
|
||||
'item_id' => $documents->id,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace App\Limitations\Engines;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Limitations\LimitationEngine;
|
||||
|
||||
class DefaultLimitationEngine implements LimitationEngine
|
||||
{
|
||||
public function canUpload(User $user, int $fileSize = 0): bool
|
||||
{
|
||||
// 1. If storage limitations is set to false, then allow upload
|
||||
if (! get_settings('storage_limitation')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get used storage percentage
|
||||
$usedPercentage = get_storage_percentage(
|
||||
used: $user->usedCapacity + $fileSize,
|
||||
maxAmount: $user->limitations->max_storage_amount,
|
||||
);
|
||||
|
||||
// 2. Check if storage usage exceed predefined capacity
|
||||
return ! ($usedPercentage >= 100)
|
||||
;
|
||||
}
|
||||
|
||||
public function canDownload(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canCreateFolder(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace App\Limitations\Engines;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Limitations\LimitationEngine;
|
||||
|
||||
class FixedBillingLimitationEngine implements LimitationEngine
|
||||
{
|
||||
public function canUpload(User $user, int $fileSize = 0): bool
|
||||
{
|
||||
// Get used capacity
|
||||
$usedPercentage = get_storage_percentage(
|
||||
used: $user->usedCapacity + $fileSize,
|
||||
maxAmount: $user->limitations->max_storage_amount,
|
||||
);
|
||||
|
||||
// Check if storage usage exceed predefined capacity
|
||||
return ! ($usedPercentage >= 100)
|
||||
;
|
||||
}
|
||||
|
||||
public function canDownload(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canCreateFolder(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace App\Limitations\Engines;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Limitations\LimitationEngine;
|
||||
|
||||
class MeteredBillingLimitationEngine implements LimitationEngine
|
||||
{
|
||||
public function canUpload(User $user, int $fileSize = 0): bool
|
||||
{
|
||||
// Disable upload when user has more than 3 failed payments
|
||||
return ! ($user->failedPayments()->count() >= 3)
|
||||
;
|
||||
}
|
||||
|
||||
public function canDownload(User $user): bool
|
||||
{
|
||||
// Disable upload when user has more than 3 failed payments
|
||||
return ! ($user->failedPayments()->count() >= 3)
|
||||
;
|
||||
}
|
||||
|
||||
public function canCreateFolder(User $user): bool
|
||||
{
|
||||
// Disable upload when user has more than 3 failed payments
|
||||
return ! ($user->failedPayments()->count() >= 3)
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace App\Limitations;
|
||||
|
||||
use App\Users\Models\User;
|
||||
|
||||
interface LimitationEngine
|
||||
{
|
||||
public function canUpload(User $user, int $fileSize = 0): bool;
|
||||
|
||||
public function canDownload(User $user): bool;
|
||||
|
||||
public function canCreateFolder(User $user): bool;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace App\Limitations;
|
||||
|
||||
use Illuminate\Support\Manager;
|
||||
use App\Limitations\Engines\DefaultLimitationEngine;
|
||||
use App\Limitations\Engines\FixedBillingLimitationEngine;
|
||||
use App\Limitations\Engines\MeteredBillingLimitationEngine;
|
||||
|
||||
class LimitationManager extends Manager
|
||||
{
|
||||
public function getDefaultDriver(): string
|
||||
{
|
||||
return get_limitation_driver();
|
||||
}
|
||||
|
||||
public function createDefaultDriver(): DefaultLimitationEngine
|
||||
{
|
||||
return new DefaultLimitationEngine();
|
||||
}
|
||||
|
||||
public function createFixedDriver(): FixedBillingLimitationEngine
|
||||
{
|
||||
return new FixedBillingLimitationEngine();
|
||||
}
|
||||
|
||||
public function createMeteredDriver(): MeteredBillingLimitationEngine
|
||||
{
|
||||
return new MeteredBillingLimitationEngine();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
namespace App\Users\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class CheckStorageCapacityAction
|
||||
{
|
||||
/**
|
||||
* Check if user has enough space to upload file
|
||||
*/
|
||||
public function __invoke(
|
||||
string $user_id,
|
||||
int $file_size,
|
||||
string $temp_filename,
|
||||
): void {
|
||||
// Get user storage percentage and get storage_limitation setting
|
||||
$user_storage_used = user_storage_percentage($user_id, $file_size);
|
||||
|
||||
// Check if user can upload
|
||||
if (get_settings('storage_limitation') && $user_storage_used >= 100) {
|
||||
// Delete file
|
||||
Storage::disk('local')
|
||||
->delete("chunks/$temp_filename");
|
||||
|
||||
// Abort uploading
|
||||
abort(423, 'You exceed your storage limit!');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Actions;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
@@ -14,7 +13,7 @@ class FormatUsageEstimatesAction
|
||||
$usage = match ($estimate['feature']) {
|
||||
'bandwidth', 'storage' => Metric::megabytes($estimate['usage'])->format(),
|
||||
'flatFee' => intval($estimate['usage']) . ' ' . __('Pcs.'),
|
||||
'member' => intval($estimate['usage']) . ' ' . __('Mem.'),
|
||||
'member' => intval($estimate['usage']) . ' ' . __('Mem.'),
|
||||
};
|
||||
|
||||
// Normalize units
|
||||
@@ -29,7 +28,7 @@ class FormatUsageEstimatesAction
|
||||
'amount' => $amount,
|
||||
'cost' => format_currency($amount, $currency),
|
||||
'usage' => $usage,
|
||||
]
|
||||
],
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use Illuminate\Support\Facades\DB;
|
||||
use Database\Factories\UserFactory;
|
||||
use Domain\Settings\Models\Setting;
|
||||
use Kyslik\ColumnSortable\Sortable;
|
||||
use App\Limitations\LimitationManager;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use App\Users\Notifications\ResetPassword;
|
||||
@@ -87,6 +88,17 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return UserFactory::new();
|
||||
}
|
||||
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (str_starts_with($method, 'can')) {
|
||||
return resolve(LimitationManager::class)
|
||||
->driver()
|
||||
->$method($this, ...$parameters);
|
||||
}
|
||||
|
||||
return parent::__call($method, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user used storage details
|
||||
*/
|
||||
@@ -102,8 +114,8 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
}
|
||||
|
||||
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) . '%',
|
||||
'used' => (float) get_storage_percentage($this->usedCapacity, $this->limitations->max_storage_amount),
|
||||
'used_formatted' => get_storage_percentage($this->usedCapacity, $this->limitations->max_storage_amount) . '%',
|
||||
'capacity' => $this->limitations->max_storage_amount,
|
||||
'capacity_formatted' => format_gigabytes($this->limitations->max_storage_amount),
|
||||
];
|
||||
|
||||
@@ -60,7 +60,7 @@ class UserLimitation extends Model
|
||||
return [
|
||||
'use' => Metric::bytes($userCapacity)->format(),
|
||||
'total' => format_gigabytes($this->max_storage_amount),
|
||||
'percentage' => get_storage_fill_percentage($userCapacity, $this->max_storage_amount),
|
||||
'percentage' => get_storage_percentage($userCapacity, $this->max_storage_amount),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,12 @@ namespace App\Users\Resources;
|
||||
use Domain\Folders\Resources\FolderCollection;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use App\Users\Actions\FormatUsageEstimatesAction;
|
||||
use VueFileManager\Subscription\Domain\CreditCards\Resources\CreditCardCollection;
|
||||
use VueFileManager\Subscription\Domain\Credits\Resources\BalanceResource;
|
||||
use VueFileManager\Subscription\Domain\CreditCards\Resources\CreditCardCollection;
|
||||
use VueFileManager\Subscription\Domain\BillingAlerts\Resources\BillingAlertResource;
|
||||
use VueFileManager\Subscription\Domain\FailedPayments\Resources\FailedPaymentsCollection;
|
||||
use VueFileManager\Subscription\Domain\Subscriptions\Resources\SubscriptionResource;
|
||||
use VueFileManager\Subscription\Domain\Usage\Actions\SumUsageForCurrentPeriodAction;
|
||||
use VueFileManager\Subscription\Domain\FailedPayments\Resources\FailedPaymentsCollection;
|
||||
|
||||
class UserResource extends JsonResource
|
||||
{
|
||||
@@ -41,8 +41,8 @@ class UserResource extends JsonResource
|
||||
'updated_at' => format_date($this->updated_at, '%d. %B. %Y'),
|
||||
],
|
||||
'relationships' => [
|
||||
'settings' => new SettingsResource($this->settings),
|
||||
'favourites' => new FolderCollection($this->favouriteFolders),
|
||||
'settings' => new SettingsResource($this->settings),
|
||||
'favourites' => new FolderCollection($this->favouriteFolders),
|
||||
'creditCards' => new CreditCardCollection($this->creditCards),
|
||||
$this->mergeWhen($this->hasSubscription(), fn () => [
|
||||
'subscription' => new SubscriptionResource($this->subscription),
|
||||
|
||||
@@ -31,7 +31,7 @@ class UserStorageResource extends JsonResource
|
||||
'attributes' => [
|
||||
'used' => Metric::bytes($this->usedCapacity)->format(),
|
||||
'capacity' => format_gigabytes($totalCapacity),
|
||||
'percentage' => (float) get_storage_fill_percentage($this->usedCapacity, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($this->usedCapacity, $totalCapacity),
|
||||
],
|
||||
'meta' => [
|
||||
'traffic' => [
|
||||
@@ -44,23 +44,23 @@ class UserStorageResource extends JsonResource
|
||||
],
|
||||
'images' => [
|
||||
'used' => Metric::bytes($images)->format(),
|
||||
'percentage' => (float) get_storage_fill_percentage($images, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($images, $totalCapacity),
|
||||
],
|
||||
'audios' => [
|
||||
'used' => Metric::bytes($audios)->format(),
|
||||
'percentage' => (float) get_storage_fill_percentage($audios, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($audios, $totalCapacity),
|
||||
],
|
||||
'videos' => [
|
||||
'used' => Metric::bytes($videos)->format(),
|
||||
'percentage' => (float) get_storage_fill_percentage($videos, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($videos, $totalCapacity),
|
||||
],
|
||||
'documents' => [
|
||||
'used' => Metric::bytes($documents)->format(),
|
||||
'percentage' => (float) get_storage_fill_percentage($documents, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($documents, $totalCapacity),
|
||||
],
|
||||
'others' => [
|
||||
'used' => Metric::bytes($others)->format(),
|
||||
'percentage' => (float) get_storage_fill_percentage($others, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($others, $totalCapacity),
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user