Limitation API skelet with can upload tests

This commit is contained in:
Čarodej
2022-01-05 11:20:57 +01:00
parent 49c6f25acb
commit c7c11fe5b9
42 changed files with 549 additions and 187 deletions
@@ -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,
]
],
];
});
}
+14 -2
View File
@@ -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),
];
+1 -1
View File
@@ -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 -4
View File
@@ -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),
],
],
],